当总和超过100并仍然预先形成总和和平均值时,如何让此代码停止输入?

我已经在这几个小时!!!! 对赋值的更新声明我们需要在它们的值超过100时停止用户输入。如果不重写整个内容,我该如何“循环”它? 我知道代码很重,但这是分配的要求。 我想我的大脑只是java汤! 任何帮助都是极好的 :)

public static void main(String[] args) { //Use Main Method for gathering input float input = 1; // Declare variable for sum float theSum = 0; // Declare variable for average float average = 0; // Declare variable for counting the number of user inputs int counter = 0; /* Initialize the while loop using an input of 0 as a sentinel value * to exit the loop*/ while (input != 0) { // Use JOptionPane method to accept input from user input = Float.parseFloat( JOptionPane.showInputDialog( null, "Please enter a number. Enter 0 to quit: ")); // Invoke sum method and pass input and summation to sum method theSum = (sum(input, theSum)); // Invoke avg method and pass summation and counter to avg average = (avg(theSum, counter)); // Increment the counter variable counter++; } // Invoke display method and pass summation, average, and counter variables to it display(theSum, average, counter); } public static float sum(float num1, float sum) { //Add the user's input number to the sum variable sum += num1; //Return value of sum variable as new summation variable return sum; } public static float avg(float num1, int num2) { //Declare and initialize variable for average float average = 0; //Calculate average average = num1 / num2; //Return value of average variable return average; } public static void display(float sum, float average, int counter) { /* I am subtracting 1 from variable counter so as not to include the sentinel value * of 0 that the user had to enter to exit the input loop in the overall count*/ // Display the count, sum, and average to the user JOptionPane.showMessageDialog(null, "Count = " + (counter - 1) + ", Sum = " + sum + ", Average = " + average); // Test to see if sum is greater than 100 and if so, display alert to user if (sum > 100) { JOptionPane.showMessageDialog(null, "The sum of your numbers is greater than 100!"); } } 

}

如果我在计数器后输入这个++用户在第一次输入后收到此消息…我做错了什么!!!!!!

 if (theSum > 100) break; JOptionPane.showMessageDialog(null, "The sum of your numbers is greater than 100!"); 

rest的问题是“JOptionPane.showMessageDialog(null,”你的数字之和大于100!);“ 一段代码在rest后立即放置时会导致一些输出问题。 现在麻烦的是,总和> 100的平均值输出的是和,而不是平均值。 总和<= 100 … WTH可以正常工作吗?!?! 🙂

现在是代码:

 public static void main(String[] args) { //Use Main Method for gathering input float input = 1; // Declare variable for sum float theSum = 0; // Declare variable for average float average = 0; // Declare variable for counting the number of user inputs int counter = 0; /* Initialize the while loop using an input of 0 as a sentinel value * to exit the loop*/ while (input != 0) { // Use JOptionPane method to accept input from user input = Float.parseFloat( JOptionPane.showInputDialog( null, "Please enter a number. Enter 0 to quit: ")); // Invoke sum method and pass input and summation to sum method theSum = (sum(input, theSum)); // Invoke avg method and pass summation and counter to avg average = (avg(theSum, counter)); // Increment the counter variable counter++; if (theSum > 100) break; } // Invoke display method and pass summation, average, and counter variables to it display(theSum, average, counter); } public static float sum(float num1, float sum) { //Add the user's input number to the sum variable sum += num1; //Return value of sum variable as new summation variable return sum; } public static float avg(float num1, int num2) { //Declare and initialize variable for average float average = 0; //Calculate average average = num1 / num2; //Return value of average variable return average; } public static void display(float sum, float average, int counter) { /* I am subtracting 1 from variable counter so as not to include the sentinel value * of 0 that the user had to enter to exit the input loop in the overall count*/ // Display the count, sum, and average to the user if (sum >100) JOptionPane.showMessageDialog(null, "Count = " + (counter) + ", Sum = " + sum + ", Average = " + average); if (sum <= 100) JOptionPane.showMessageDialog(null, "Count = " + (counter-1) + ", Sum = " + sum + ", Average = " + average); } } 

while循环中使用带有if条件的break语句来检查thesum > 100 。 希望这一点提示能帮助你获得理想的行为。 由于你还需要计算平均值,所以把断开逻辑放在avg后面:

  while (input != 0) { // Use JOptionPane method to accept input from user input = Float.parseFloat( JOptionPane.showInputDialog( null, "Please enter a number. Enter 0 to quit: ")); // Invoke sum method and pass input and summation to sum method theSum = (sum(input, theSum)); // Invoke avg method and pass summation and counter to avg average = (avg(theSum, counter)); // Increment the counter variable counter++; if (theSum > 100) break; } // Invoke display method and pass summation, average, and counter variables to it display(theSum, average, counter); 

将循环更改为

 do { ... } while (input != 0 && sum < 100); 

在循环停止之前,您无需计算平均值。