我是大学新生,目前是我计算机程序设计课程的第一个学期;我们已经开始同时使用伪代码和Java。
我们的任务是创建一个java应用程序,该应用程序总计并创建一系列由用户输入的数字的平均值。用户可以输入任意数量的数字,直到输入数字0,在这种情况下,程序输出总数和平均值,然后提示用户重新开始或退出程序。
我使用两个while循环完成了这个任务,其中一个是嵌套的。但是,当试图打破嵌套循环时,我收到错误“错误:打开外部开关或循环”。之后,我花了很多时间浏览这个论坛,寻找有关问题的答案和信息,但似乎没有一个与我的问题相关,并且修复不起作用。其中包括使用带标签的断裂和修正花括号;因为这些似乎不起作用,我确信这个问题在代码中更深层次。
因为我的课程是在线的,所以很难及时与教授或其他学生沟通,这就是我转向这个社区的原因!
下面我将附上教授希望我们将应用程序作为基础的伪代码。
Start
Declarations
Num sumTotal = 0 // Initialize for clarity
Num numEntered
Num averageNum
Num loopCounter
Num answer
String endProgram = “Y” // Initialize so that outer loop will work
End declarations
// Greet the user
Output “Welcome to our calculator. “
Output “Enter as many numbers as you want.”
Output “When you are done entering numbers, enter 0 (zero) to display the sum.”
Output “Do you want to start the calculator? (Y/N): “ // Let the user decide to start
input endProgram
// Note: if the user enters anything but Y or y, the loop will not execute.
While endProgram <> “Y” OR endProgram <> “y” // Allows the user to perform multiple calculations
//Enter the first number (sentinel value)
Output “Please enter your first number.”
Input numEntered
While numEntered <> 0 // Allows the user to enter numbers for the current calculation
Output “You entered the number “ + numEntered // echo input
sumTotal = sumTotal + numEntered // Add number entered to total
loopCounter++ // Increment the number of entries
Output “Please enter the next number”
Input numEntered // If 0, the loop will end here
endWhile // the nested inner loop code stops here
// Output section
Output “The total numbers entered is: “ + loopCounter
Output “The total of the numbers entered is: “ + sumTotal
Output “The average of the numbers entered is: “ + averageNum
Output “Would you like to do a new set of calculations? Y/N
Input endProgram
End While // End outer While statement when endProgram = Y
Output “Thank you for using the calculator. The program will now end.”
Stop // Stop the program
下面我将附上我的Java代码
/* Module 4 Assignment 1 Part 1
* Aaron Field
* Submitted March 26, 2016
* Using DrJava */
import javax.swing.JOptionPane;
public class Calculator {
public static void main(String[] args) {
//variable declarations
int numEntered;
int sumTotal = 0;
int averageNum = sumTotal / numEntered;
int loopCounter;
String endProgram = "Y";
//end declarations
System.out.println("Welcome to the Totaling Calculator");
System.out.println("This program will accept integer inputs until 0 is entered" + '\n' + "When 0 is entered, a sum and an average will be displayed.");
endProgram = JOptionPane.showInputDialog(
"Do you want to start the calculator? (Y/N): ");
while(endProgram != "Y" || endProgram != "y"); {
numEntered = Integer.parseInt(JOptionPane.showInputDialog(
"Please enter your first number."));
while(numEntered != 0); {
System.out.println("You entered the number " + numEntered);
sumTotal = sumTotal + numEntered;
loopCounter++;
numEntered = Integer.parseInt(JOptionPane.showInputDialog(
"Please enter the next number"));
break;}
System.out.println("The total numbers entered is: " + loopCounter + '\n' + "The total of the numbers entered is: " + sumTotal + '\n' + "The average of the numbers entered is: " + averageNum);
endProgram = JOptionPane.showInputDialog(
"Would you like to do a new set of calculations? (Y/N): ");
break;
}
System.out.println("Thank you for using the calculator. The program will now end.");
}
}
我理解代码可能很草率或奇怪,但我从2月中旬开始只使用Java,所以请原谅任何草率的格式或错误使用代码。
对于我未经训练的眼睛,休息陈述似乎在循环中;我很困惑为什么我的编译器会建议它们不是。非常感谢任何帮助,谢谢。
答案 0 :(得分:1)
while(...); 这是一种不好的做法:P。你的break命令实际上都在while循环之外。
答案 1 :(得分:0)
在while语句后删除分号。
示例:更改
--rollStep
到
while(endProgram != "Y" || endProgram != “y”); {
//...
}
while语句后跟分号实际上被解释为此
while(endProgram != "Y" || endProgram != “y”) {
//...
}