【发布时间】:2022-06-11 00:12:56
【问题描述】:
此时,我的所有数学运算都正确,但我无法弄清楚为什么下面编写的代码不断出现此错误:
Objective9Lab6.java:8: error: illegal start of type
while (keepGoing) {
^
Objective9Lab6.java:8: error: <identifier> expected
while (keepGoing) {
^
import java.util.Scanner;
public class Objective9Lab6 {
public static void main(String[] args) {
printMenu();}
while (keepGoing) {
System.out.print("Which would you like to do? ");
choice = kb.nextInt();
double average = (num1 + num2) / 2;
double sum = num1 + num2;
double calcTax = sum * (8.31/100);
Scanner kb = new Scanner(System.in);
double num1, num2;
boolean keepGoing = true;
int choice;
double answer = 0.0;
System.out.print("Please give me a number: ");
num1 = kb.nextDouble();
System.out.print("Please give me another number: ");
num2 = kb.nextDouble();
switch (choice) {
case 1:
System.out.println(+num1 + " + " + num2 + " = " + sum + ".");
break;
case 2:
System.out.println("The average of " + num1 + "and" + num2 + "is" + average + ".");
break;
case 3:
System.out.println("The amount in tax to be collected from a purchace of " + num1 + " and " + num2 + " is " + calcTax + ".");
break;
case 4:
System.out.println("You've chosen to quit.");
return;
default:
System.out.println("Invalid entry. Please try again.");
}
}
public static void printMenu() {
System.out.println();
System.out.println("========= MENU =========");
System.out.println("| |");
System.out.println("| 1. Add Numbers |");
System.out.println("| 2. Find Average |");
System.out.println("| 3. Calculate Tax |");
System.out.println("| 4. Exit |");
System.out.println("| |");
System.out.println("========================");
System.out.println();
}
}
我觉得这与我声明布尔值的位置有关,但是在我移动该行的任何地方我都会收到相同的错误。
【问题讨论】:
-
错字。从
printMenu();}中删除} -
在
while循环之前声明keepGoing或使用while (true) -
错字问题被认为质量低下。
标签: java while-loop boolean