嘿,人們我有一個問題要問關於Java。我遇到的問題是關於'else if'的錯誤。我不知道如何解決這個問題。如果我在終端或CMD中運行它會得到以下錯誤:'else without if'
但我不知道如何解決這個問題。我真的很感激任何幫助。需要幫助'如果沒有其他'
import java.io.*;
import java.util.*;
public class JonesInc {
public static void main(String[] args) {
Scanner kbReader = new Scanner(System. in);
System.out.println("1. Administration");
System.out.println("2. Check Stock Availability");
System.out.println("3. Purchase Textbooks");
System.out.println("4. Return Textbooks");
System.out.println("5. Quit");
System.out.println("please select your choice");
int choice = kbReader.nextInt();
int stock = 100;
if (choice == 1) {
Scanner password = new Scanner(System. in);
System.out.println("Please Enter Password");
String name = password.nextLine();
if (name.equals("Java")) {
System.out.println("A. Increase Stock Availabilty");
System.out.println("B. Reduce Stock Availability");
System.out.println("C. Quit");
System.out.println("Select your choice");
Scanner kbReader1 = new Scanner(System. in);
String choice1 = kbReader1.nextLine();
char ch = choice1.charAt(0);
System.out.println("");
switch (ch) {
case 'A':
case 'a':
System.out.println("How much do you wish to increase stock by");
break;
case 'B':
case 'b':
System.out.println("How much do you wish to decrease stock by");
break;
case 'C':
case 'c':
System.out.println("Dave is cool");
break;
} else {
System.out.println("Wrong Password Entered 3 times Please contact the help desk");
}
} else if (choice == 2) {
System.out.println("Check stock availability");
} else if (choice == 3) {
System.out.println("How many Textbooks do you want to purchase");
} else if (choice == 4) {
System.out.println("How many Textbooks do you want to return");
} else if (choice == 5) {
System.out.println("Quit");
enter code here
}
}
}
}
如果縮進代碼,你很可能會發現問題(貌似有一個括號switch語句後失蹤) –
仔細壓痕和良好的代碼格式是不是爲了美觀。相反,它可以讓您輕鬆調試您的程序。學習它。用它。 –
這段代碼亂七八糟。也就是說,預編輯它。現在這是一個很好的縮進混亂。 – aliteralmind