gpt4 book ai didi

Java:有关中断的编译错误

转载 作者:行者123 更新时间:2023-12-01 10:09:19 25 4
gpt4 key购买 nike

我是一名大学新生,目前正在学习计算机编程类(class)的第一学期;我们首先同时使用伪代码和 Java。

我们的任务是创建一个 Java 应用程序,对用户输入的一系列数字进行总计并创建平均值。用户可以输入任意数量的数字,直到输入数字0,此时程序输出总数和平均值,然后提示用户重新开始或退出程序。

我使用两个 while 循环完成了这项任务,其中一个是嵌套的。但是,当尝试打破嵌套循环时,我收到错误“错误:打破开关或循环之外”。之后,我花了很多时间浏览这个论坛寻找有关该问题的答案和信息,但似乎没有一个与我的问题相关,修复也不起作用。其中包括使用带标签的中断和纠正大括号;因为这些似乎不起作用,我确信问题出在代码的更深处。

因为我的类(class)是在线的,所以很难及时与教授或其他学生沟通,这就是我转向这个社区的原因!

下面我将附上教授希望我们作为应用程序基础的伪代码。

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.");
}
}

我知道代码可能很草率或奇怪,但我从二月中旬才开始使用 Java,所以请原谅任何草率的格式或不正确的代码使用。

在我未经训练的眼睛看来,break 语句似乎在循环内;我很困惑为什么我的编译器会建议它们不是。任何帮助将不胜感激,谢谢。

最佳答案

while(...);这是一个不好的做法:P。您的中断命令实际上都在 while 循环之外。

关于Java:有关中断的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com