无法获取joptionpane以显示并行数组的结果

7gyucuyw  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(303)

我这里有一段代码,在最后几行应该通过joptionpane窗口返回我的结果,

for(int i = 0; i < 10; i++) {
                percent[i] = ((double)counts[i] / amount) * 100;
            }

            //create results
            // results starting out is just the numbers. Remove the code from the for loop above that does that,
            // and instead use a for loop and go through both arrays creating the output string results with
            // the count and percentage of each of the 10 numbers one per line.

            results = String.format("%-10s%-10s%-10s\n*****************************************\n", "Numbers:", "Count:","Percentage:");

            for(int i = 0; i < 10; i++) {
                int number = i + 1;
                results += String.format("%-10s%-10s%-10s\n", number, counts[i], percent[i]);
            }

            results = results + "\n\nRepeat this? Yes or No";

            // show results in a confirm dialog that will also control the loop
            choice = JOptionPane.showConfirmDialog(null, results, "Results", 0);

        }

但反过来只是重复初始窗口,请求输入随机数的数量。我是不是看多了问题?
以下是全部代码:

/* RandomCounts.java
 * 
 * generates a certain amount of random numbers between 1 and 10
 * 
 * Modify this code as follows:
 * 
 * As is, this code just outputs the random numbers to a dialog box.
 * 
 * Create two arrays where indicated: counts (int) and percents (double). Each of size 10
 * 
 * initialize counts where indicated
 * 
 * update counts where indicated, and remove code that updates results
 * 
 * calculate percentages using a for loop where indicated
 * 
 * output both arrays where indicated
 * 
 * Remember: arrays are zero based. Your arrays of size 10 will have indices 0-9, NOT 1-10
 */
import java.awt.Font;

import javax.swing.*;

public class RandomCountsStart {

    public static void main(String[] args) {

        UIManager.put("OptionPane.messageFont", new Font(Font.MONOSPACED, Font.BOLD, 24));

        int[] counts = new int[10];
        double[] percent = new double[10];// Declare the arrays counts and percents here

        int num = 0, amount = 0, choice=0;
        String results;

        while (choice == 0){

            for(int i = 0; i < 10; i++){
                counts[i] = 0;
            }// initialize counts here using a for loop

            String input = JOptionPane.showInputDialog(null, "Input the amount of random numbers to generate");
            amount = Integer.parseInt(input);
            results = "";

            // generate the numbers in a for loop
            for(int i = 1; i <= amount; i++){
                num = (int)(Math.random()*10) + 1;

                }
                // here is where you should update the array counts based on which num was generated
                // for example: if num is 3, update the array element that counts 3's
                if(num == 1) {
                    counts[0]++;
                }
                else if(num == 2) {
                    counts[1]++;
                }
                else if(num == 3) {
                    counts[2]++;
                }
                else if(num == 4) {
                    counts[3]++;
                }
                else if(num == 5) {
                    counts[4]++;
                }
                else if(num == 6) {
                    counts[5]++;
                }
                else if(num == 7) {
                    counts[6]++;
                }
                else if(num == 8) {
                    counts[7]++;
                }
                else if(num == 9) {
                    counts[8]++;
                }
                else if(num == 10) {
                    counts[9]++;
                }
        }

            // calculate percentages
            // go through the count array and determine the percentage of each value. Store the
            // percentage in the appropriate element in percents (parallel array example)
            for(int i = 0; i < 10; i++) {
                percent[i] = ((double)counts[i] / amount) * 100;
            }

            //create results
            // results starting out is just the numbers. Remove the code from the for loop above that does that,
            // and instead use a for loop and go through both arrays creating the output string results with
            // the count and percentage of each of the 10 numbers one per line.

            results = String.format("%-10s%-10s%-10s\n*****************************************\n", "Numbers:", "Count:","Percentage:");

            for(int i = 0; i < 10; i++) {
                int number = i + 1;
                results += String.format("%-10s%-10s%-10s\n", number, counts[i], percent[i]);
            }

            results = results + "\n\nRepeat this? Yes or No";

            // show results in a confirm dialog that will also control the loop
            choice = JOptionPane.showConfirmDialog(null, results, "Results", 0);

        }

    }
1cklez4t

1cklez4t1#

因此,我已经更新了您的代码,以包含直到choice=joptionpane.showconfirmdialog(null,results,“results”,0);在你的while循环中。

/* RandomCounts.java
 * 
 * generates a certain amount of random numbers between 1 and 10
 * 
 * Modify this code as follows:
 * 
 * As is, this code just outputs the random numbers to a dialog box.
 * 
 * Create two arrays where indicated: counts (int) and percents (double). Each of size 10
 * 
 * initialize counts where indicated
 * 
 * update counts where indicated, and remove code that updates results
 * 
 * calculate percentages using a for loop where indicated
 * 
 * output both arrays where indicated
 * 
 * Remember: arrays are zero based. Your arrays of size 10 will have indices 0-9, NOT 1-10
 */
 import java.awt.Font;

 import javax.swing.*;

 public class RandomCountsStart {

public static void main(String[] args) {

    UIManager.put("OptionPane.messageFont", new Font(Font.MONOSPACED, Font.BOLD, 24));

    int[] counts = new int[10];
    double[] percent = new double[10];// Declare the arrays counts and percents here

    int num = 0;
    int amount = 0;
    int choice=0;
    String results;

    while (choice == 0){

        for(int i = 0; i < 10; i++){
            counts[i] = 0;
        }// initialize counts here using a for loop

        String input = JOptionPane.showInputDialog(null, "Input the amount of random numbers to generate");
        amount = Integer.parseInt(input);
        results = "";

        // generate the numbers in a for loop
        for(int i = 1; i <= amount; i++){
            num = (int)(Math.random()*10) + 1;
        }
            // here is where you should update the array counts based on which num was generated
            // for example: if num is 3, update the array element that counts 3's
            if(num == 1) {
                counts[0]++;
            }
            else if(num == 2) {
                counts[1]++;
            }
            else if(num == 3) {
                counts[2]++;
            }
            else if(num == 4) {
                counts[3]++;
            }
            else if(num == 5) {
                counts[4]++;
            }
            else if(num == 6) {
                counts[5]++;
            }
            else if(num == 7) {
                counts[6]++;
            }
            else if(num == 8) {
                counts[7]++;
            }
            else if(num == 9) {
                counts[8]++;
            }
            else if(num == 10) {
                counts[9]++;
            }

        // calculate percentages
        // go through the count array and determine the percentage of each value. Store the
        // percentage in the appropriate element in percents (parallel array example)
        for(int i = 0; i < 10; i++) {
            percent[i] = ((double)counts[i] / amount) * 100;
        }

        //create results
        // results starting out is just the numbers. Remove the code from the for loop above that does that,
        // and instead use a for loop and go through both arrays creating the output string results with
        // the count and percentage of each of the 10 numbers one per line.

        results = String.format("%-10s%-10s%-10s\n*****************************************\n", "Numbers:", "Count:","Percentage:");

        for(int i = 0; i < 10; i++) {
            int number = i + 1;
            results += String.format("%-10s%-10s%-10s\n", number, counts[i], percent[i]);
        }

        results = results + "\n\nRepeat this? Yes or No";

        // show results in a confirm dialog that will also control the loop
        choice = JOptionPane.showConfirmDialog(null, results, "Results", 0);
}
    }

}

相关问题