我程序的哪一部分处于错误循环中?爪哇

时间:2019-06-20 04:17:12

标签: java

该项目将创建一个程序,该程序将在JOption窗格中接受用户的输入,并检查数字是否为质数。该程序应该一直循环运行直到用户输入0,这会触发该程序计算最大值,最小值,总和,计数和平均值。

我已经完成了99%的作业,除了我输入的第一个数字不会像其他数字一样打印出来,但是仍然包含在计算中

import javax.swing.*;
import java.util.*;


public class Assignment4 {


    public static void main(String[] args) {

        // Main Method

        userInput(); 


    }


    public static void userInput() {

    int number;
    int sum;           
        int count;                          // declaring variables
        int max= 0;
        int min= 1;
        float average; 


        String userNumber;              // Number typed by user


        sum = 0;                        // start at 0 for sum
        count = 0;                      // start at 0 for counter



                // prompt user to enter a positive number


        userNumber = JOptionPane.showInputDialog("Enter a positive integer or 0 to quit");


                        // convert  to int  


            number = Integer.parseInt(userNumber);  


            // if the number entered is positive and not 0, the loop repeats


            while ( number != 0 && number > 0)  {  


                sum += number;           

                // starting count and sum at 0

                count++;  

                // repeating user input prompt unless 0 is entered

                // storing values for min and max as we go

                if (number > max)max=number;
                if (number < min & number != 0)min=number;
            userNumber = JOptionPane.showInputDialog("Enter a positive integer or 0 to quit");  
                number = Integer.parseInt( userNumber );

                // checking if number entered is prime or not

                int i,m=0,flag=0; 

                m=number/2;      

                if(number==0||number==1){  

                 System.out.println(number+" is not a prime number"); 

                }else{  

                 for(i=2;i<=m;i++){     

                  if(number%i==0){   

                   System.out.println(number+" is not a prime number");      

                   flag=1;      
                   break;      
                  }      
                 }      
                 if(flag==0){ System.out.println(number+" is a prime number"); }  
                }

            } 

            if ( count != 0 ) {  


           // as long as one number is entered, calculations are done below


                // calculate average of all numbers entered

                average = (float) sum / count;


                // printing out the results

                System.out.printf("The average is : %.3f\n", average);
                System.out.println("The sum is : "+sum);
                System.out.println("The count is : "+count);
                System.out.println("The max is : "+max);
                System.out.println("The min is : "+min);


           }






    }
}

我需要像剩下的一样打印第一个条目,请帮助我找到放入循环的位置

2 个答案:

答案 0 :(得分:0)

您能解释一下您需要什么吗?您给它什么输入,您看到什么输出?

我注意到您在呼叫JOptionPane之前要添加数字,您是否可能将count比实际数字大一?您的缩进非常糟糕,您应该清理它,我在阅读代码周期时遇到了麻烦。

// 1 START OF LOOP
        while ( number != 0 && number > 0)  {  

// 2 ADD NUMBER TO SUM
            sum += number;           

            // starting count and sum at 0

            count++;  

            // repeating user input prompt unless 0 is entered

            // storing values for min and max as we go

            if (number > max)max=number;
            if (number < min & number != 0)min=number;

// 3 THEN GET INPUT.  WHAT???

        userNumber = JOptionPane.showInputDialog("Enter a positive integer or 0 to quit"); 

答案 1 :(得分:0)

您的程序中有几个问题。永远不会考虑第一个数字的原因是您拥有

userNumber = JOptionPane.showInputDialog("Enter a positive integer or 0 to quit");  
number = Integer.parseInt( userNumber );

两次(在while循环之前和while循环中)。

我建议使用Integer.MAX_VALUE初始化数字:number = Integer.MAX_VALUE;

然后删除

userNumber = JOptionPane.showInputDialog("Enter a positive integer or 0 to quit");  
number = Integer.parseInt( userNumber );

在while循环之前。

&中缺少if (number < min & number != 0)min=number;

=>

if (number < min && number != 0) {
  min=number;
}

可以通过编写while ( number > 0) {来简化while循环中的条件,因为> 0也意味着!= 0

我还建议您编写一些更好的代码以提高可读性。始终为条件(if使用花括号,即使在条件为真的情况下仅执行一行时也是如此。

我希望这会有所帮助。让我知道您是否需要更多帮助,但您现在应该可以自己解决此任务:)