私は大学の新入生で、現在はコンピュータプログラミングコースの私の最初の学期に入っています。私たちは擬似コードとJavaで同時に作業することから始めました。Java:ブレークに関するエラーのコンパイル
私たちは、ユーザーが入力した一連の数字を合計して平均を作成するJavaアプリケーションの作成を任されていました。ユーザは、数字0が入力されるまで、希望する数の数字を入力することができ、その場合、プログラムは合計と平均を出力し、次いで、ユーザに開始または終了するように促す。
2つのwhileループを使用してこのタスクを達成しました。このうちの1つはネストされています。しかし、入れ子にされたループを中断しようとすると、エラー "エラー:スイッチまたはループの外にブレーク"が表示されます。その後、私はこのフォーラムをブラウズして問題に関する回答と情報を探していましたが、私の問題に関連していないようで、修正プログラムは機能しませんでした。リストの中には、ラベル付き改行と中括弧の修正がありました。これらはうまく動作していないと思われるので、問題がコード内に深く存在すると確信しています。
私のコースはオンラインであるため、教授や他の学生とタイムリーにやりとりすることは非常に難しいので、私はこのコミュニティに目を向けました!
以下は、教授が私たちのアプリケーションの基盤としたい疑似コードを添付します。私はコードがずさんまたは奇妙かもしれ理解し、私は唯一の中旬2月以来のJavaでの作業なので、任意のずさんな言い訳をしてきた私の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.");
}
}
を添付します以下
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
コードの書式設定または不正な使用。
私の訓練されていない目には、breakステートメントはループ内にあるようです。私はコンパイラーがなぜそうでないと示唆するのか、むしろ混乱しています。どんな助けでも大いに感謝します、ありがとうございます。
と解釈されている間。 –
'' ... ...だからこそフォーマットが間違っていて... "' - これをポストするのではなく、コードを正しくフォーマットする方法を学び、整形式のコードを投稿するのはなぜですか?それは学ぶのが難しいことではありません、そしてボランティアにあなたを助けるように頼んだら、あなたは**努力していますか? –
なぜここでも休憩をしていますか? –