需要帮助'如果没有其他'

时间:2014-03-01 18:29:35

标签: java

嘿,我有一个关于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
            }
        }
    }
}

1 个答案:

答案 0 :(得分:4)

}之后没有if关闭switch

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