2014-11-14 52 views
0

請幫忙幹活的時候,我試圖讓這個工作,但我不斷收到錯誤,主要是一些錯誤與陣列

C:\Users\Carter\OneDrive\Documents\Programs\Assignment7\Assignment7.java:26: error: '.class' expected 
     Quiz myQuiz = new Quiz(int count, String name); 
           ^
C:\Users\Carter\OneDrive\Documents\Programs\Assignment7\Assignment7.java:26: error: ';' expected 
     Quiz myQuiz = new Quiz(int count, String name); 
              ^
C:\Users\Carter\OneDrive\Documents\Programs\Assignment7\Assignment7.java:26: error: not a statement 
     Quiz myQuiz = new Quiz(int count, String name); 
               ^
C:\Users\Carter\OneDrive\Documents\Programs\Assignment7\Assignment7.java:26: error: ';' expected 
     Quiz myQuiz = new Quiz(int count, String name); 
                ^
4 errors 

任何幫助,將不勝感激!我發佈的只是驅動程序,如果需要,我也可以發佈類定義!提前致謝!

//Driver program 
import java.util.*; 

public class Assignment7 
{ 


    public static void main (String[] args) { 

     Quiz myQuiz = new Quiz(int count, String name); 

     Scanner console = new Scanner (System.in); 

     String choice; 
     char command; 



     // print the menu 
     printMenu(); 



     do 
     { 
      // ask a user to choose a command 
      System.out.println("\nPlease enter a command or type ?"); 
      choice = console.next().toLowerCase(); 
      command = choice.charAt(0); 

      switch (command) 
      { 
       case 'n': //asks and prints the quiz size and name of student 
         System.out.println("n [Create a new Quiz]"); 
         System.out.println("[Input the size of quizzes]: "); 
         scores.length = console.nextInt(); 
         System.out.print(scores.length); 
         System.out.println("[Input the name of student]: "); 
         name = console.nextString(); 
         System.out.print(name); 

         break; 
       case 'a': // adds a score 
         System.out.println("a [Add a score]: "); 
         numAdd = console.nextInt(); 
         System.out.print(numAdd); 

         break; 
       case 'd': // deletes a score 
         System.out.println("d [Delete a score]: "); 
         numDelete = console.nextInt(); 
         System.out.print(numDelete); 

        break; 

       case 'p': //prints the information 
        System.out.println("p [Print the information]: "); 
        System.out.println(name); 
        System.out.println(scores); 
         break; 


       case '?': 
         printMenu(); 
         break; 

       case 'q': 
         break; 


       default: 
         System.out.println("Invalid input"); 

      } 

     } while (command != 'q'); 

    } //end of the main method 


    public static void printMenu() 
    { 
    System.out.print("\nCommand Options\n" 
        + "-----------------------------------\n" 
        + "n: Create a new data\n" 
        + "a: Add a score\n" 
        + "d: Delete a score\n" 
        + "p: Print the information\n" 
        + "?: display the menu again\n" 
        + "q: Quit this program\n\n"); 

    } // end of the printMenu method 


} 
+1

'new Quiz(int count,String name);' - 你需要在基本的方法調用語法的教訓。 – August 2014-11-14 22:19:13

+0

那麼我在第一學期的java課,所以... 而且,這不是調用一個方法,即設置我將使用使用Quiz類的方法對象。 – Cdiehl77 2014-11-14 22:19:49

+0

如何定義「測驗」?你想讓它成爲你提到的數組嗎? – DSlomer64 2014-11-14 22:21:49

回答

0

當你調用一個方法(定義方法時只)不需要指定數據類型

例如

Quiz myQuiz = new Quiz(count, name); 
//vs 
Quiz myQuiz = new Quiz(int count, String name); 

由於數據類型是在定義方法時指定的,Java已經知道它們是什麼。你只需要傳遞值。

+0

嗯,我已經嘗試過,但我得到了10多個錯誤。 – Cdiehl77 2014-11-14 22:21:05

+0

其他錯誤可能有些不同。嘗試閱讀錯誤,看看你是否可以把它們弄清楚......或谷歌它。 – scunliffe 2014-11-14 22:22:48

+0

試過了,我有30分鐘來完成這個。我可以在這裏張貼他們,也許你可以幫忙嗎? – Cdiehl77 2014-11-14 22:24:03

1

我想你打電話

Quiz myQuiz = new Quiz(int count, String name); 

之前此外,構造一個測驗對象之前,你並不需要int和字符串標識符你應該定義的數量和名稱。

所以基本上,你的代碼應該像

int count = **; 
String name = "***"; 
Quiz myQuiz = new Quiz(count,name); 
0

這裏的東西,將編譯 - 只給你的語法和申報要求的想法 - 但它不會來接近做你想。祝你好運。

package javaapplication75; 

public class Quiz { 

int count; 
String name; 
int[] scores; 

public Quiz (int count, String name2) 
{ 
    count = 0;  // Not sure why 0... 
    name = name2; } 
} 


package javaapplication75; 

    import java.util.*; 

    public class Assignment7 
    { 
    private static int numAdd; // need to declare these two vars 
    private static int numDelete; 


     public static void main (String[] args) { 

      Quiz myQuiz = new Quiz(5, "D"); // doesn't do anything good, but compiles 

      Scanner console = new Scanner (System.in); 

      String choice; 
      char command; 

      // print the menu 
      printMenu(); 

      do 
      { 
       // ask a user to choose a command 
       System.out.println("\nPlease enter a command or type ?"); 
       choice = console.next().toLowerCase(); 
       command = choice.charAt(0); 

       switch (command) 
       { 
        case 'n': //asks and prints the quiz size and name of student 
          System.out.println("n [Create a new Quiz]"); 
          System.out.println("[Input the size of quizzes]: "); 
          myQuiz.count = console.nextInt() 
           ; 
          System.out.print(myQuiz.scores.length); 
          System.out.println("[Input the name of student]: "); 
          myQuiz.name = console.nextLine(); 
          System.out.print(myQuiz.name); 

          break; 
        case 'a': // adds a score 
          System.out.println("a [Add a score]: "); 
          numAdd = console.nextInt(); 
          System.out.print(numAdd); 

          break; 
        case 'd': // deletes a score 
          System.out.println("d [Delete a score]: "); 
          numDelete = console.nextInt(); 
          System.out.print(numDelete); 

         break; 

        case 'p': //prints the information 
         System.out.println("p [Print the information]: "); 
         System.out.println(myQuiz.name); 
         System.out.println(myQuiz.scores); 
          break; 


        case '?': 
          printMenu(); 
          break; 

        case 'q': 
          break; 


        default: 
          System.out.println("Invalid input"); 

       } 

      } while (command != 'q'); 

     } //end of the main method 


     public static void printMenu() 
     { 
     System.out.print("\nCommand Options\n" 
         + "-----------------------------------\n" 
         + "n: Create a new data\n" 
         + "a: Add a score\n" 
         + "d: Delete a score\n" 
         + "p: Print the information\n" 
         + "?: display the menu again\n" 
         + "q: Quit this program\n\n"); 

     } // end of the printMenu method 


    }