为什么我的Java代码出错?

时间:2017-04-26 08:53:53

标签: java

所以我收到了这个错误:

  

线程“main”中的异常java.lang.Error:未解决的编译问题:   在KidsToys.main(KidsToys.java:24)

它与我认为的花括号有关但我不太确定,有人知道吗?可能是一个简单的语法错误?。

    /*
     * class KidsToys skeleton code
     * 
     * This code is incomplete. It implements the data storage 
     * and functional requirements for a program that manages the
     * inventory in a toy store. However, the helper methods 
     * are not implemented, these are for you to do so!
     */

     import java.util.*;

     public class KidsToys {


     // you can refer to the array and Scanner object here anywhere
     // within this class, even within helper methods if you choose to
    // implement them

    private static final Toy [] inventory = new Toy [8];
    private static final Scanner sc = new Scanner(System.in);



    public static void main(String[] args)
    {

   String userInput;


    inventory[0] = new Toy("SOC-001","Socker Bopper Body Ball", 99.95, 2);
    inventory[1] = new Toy("MEC-301", "Meccano M. A. X. Robot", 279.00, 8);
    inventory[2] = new Toy("PWR-504", "Power Rangers Movie 17cm Red Ranger", 28.99, 18);
    inventory[3] = new Toy("NER-020", "Nerf Zombie Strike Outbreaker Bow", 34.99, 16);
    inventory[4] = new Toy("PWR-505", "Power Rangers Movie 17cm Pink Ranger", 28.99, 11);
    inventory[5] = new Toy("LEG-710", "Lego Batman Movie - Arkham Asylum", 249.95, 5);
    inventory[6] = new Toy("LEG-640", "Lego City High Speed Chase", 124.99, 0);
    inventory[7] = new Toy("TST-750", "Toy Story Buzz and Woody Walkie Talkie", 139.99, 4);


  // initialise selection variable to ascii nul to keep compiler happy
  char selection = '\0';  

  // creating and storing specified set of Toy objects in the array
  do
  {

     // print menu to screen
     System.out.println("Toy Store Inventory Program");
     System.out.println("---------------------------");
     System.out.println();

     System.out.printf("%-25s%s\n", "Display Inventory", "A");
     System.out.printf("%-25s%s\n", "Print Reorder List", "B");
     System.out.printf("%-25s%s\n", "Product Range Search", "C");
     System.out.printf("%-25s%s\n", "Accept Delivery", "D");
     System.out.printf("%-25s%s\n", "Make Sale", "E");
     System.out.printf("%-25s%s\n", "Exit Program", "X");
     System.out.println();

     // prompt user to enter selection
     System.out.print("Enter selection: ");
     userInput = sc.nextLine();

     System.out.println();

     // validate selection input length
     if (userInput.length() != 1)
     {
        System.out.println("Error - invalid selection!");
     }
     else
     {
        // make selection "case insensitive"
        selection = Character.toUpperCase(userInput.charAt(0));

        // process user's selection
        switch (selection)
        {
           case 'A':
           displayInventory();
              break;

           case 'B':
           printReorderList();
              break;

           case 'C':
              // productRangeSearch();
              break;

           case 'D':
              // acceptDelivery();
              break;

           case 'E':
              // makeSale();
              break;

           case 'X':
              System.out.println("Exiting the program...");
              break;

           default:
              System.out.println("Error - invalid selection!");
          }
       }
       System.out.println();

    } while (selection != 'X');
 }
      public static void displayInventory()

      {
      for(int i = 0; i < inventory.length; i++) {

            inventory[i].printDetails();
  }
  }
  public static void printReorderList()

  {
      System.out.println("----------------------------------------------------------------------");
      System.out.printf("%-1s%-12s%-1s%-38s%-1s%", "Product Code", "|", "Description", "|","Unit Price", 
             "|", "Stock Level", "|");




      }
   }

}
}

对不起,如果它有点乱,第一次使用它。

1 个答案:

答案 0 :(得分:0)

我认为你发布的代码存在以下两个问题:

  1. 代码中的Toy类有无导入。添加该import语句,以便java知道如何处理它。
  2. 最后还有两个额外的结束花括号。
相关问题