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

问题描述

在这里有这段代码,最后几行应该通过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,0);
            
        }

    }
    

解决方法

所以我已经更新了您的代码,直到选择= 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);
}
    }

}