2016-06-04 9 views
-2
import java.util.*; 

public class Averages{ //name class as public 

    public static void main(String[] args){ 

    Scanner sc = new Scanner(System.in); //initialized string using 'new' 

     String Course; 
     int knowledgemark; 
     int communicationmark; 
     int thinkingmark; 
     int applicationmark; 
     int average; 
     int finalexam; 
     int average2; 
     String answer; 
     int maxK; 
     int maxC; 
     int maxA; 
     int maxT; 
     int knowledgeweight; 
     int communicationweight; 
     int thinkingweight; 
     int applicationweight; 
     String Assignment; 
     String Test; 
     int average3; 
     int weight; 
     int weightaverage; 
     int average4= 0; 

     System.out.println("What class are you calculating for?"); 
     Course = Scan.nextLine(); 

     System.out.println("What's your knowledge mark for this course?"); 
     knowledgemark = Scan.nextLine(); 

     System.out.println("What's your application mark for this course?"); 
     applicationmark = Scan.nextLine(); 

     System.out.println("What's your thinking mark for this course?"); 
     thinkingmark = Scan.nextLine(); 

     System.out.println("What's your communication mark for this course?"); 
     communicationmark = Scan.nextLine(); 

     average = ((knowledgemark + communicationmark + applicationmark + thinkingmark)/4); 
     average = Scan.nextInt(); 

     System.out.println("Your average for" + Course + "is" + average); 

     System.out.println("Do you want to calculate your exam with your final exam mark? (yes or no)"); 
     answer = Scan.next(); 

     if(answer.equals ("yes")){ 
     System.out.println("What is your final exam mark?"); 
      finalexam = scan.nextInt(); 
      average2 = (((average * 70) + finalexam * 30) /100); 
       System.out.println("Your final average in this course is" + average2); 
       } 

     else{ 
     System.out.println("You average for" + Course + "is" + average); 
      } 

      System.out.println("You can also calculate your marks with tests/assignments"); 
      Test = Scan.nextLine(); 

      System.out.println("What test/assignment are you calculating for?"); 
      Test = Scan.nextLine(); 

      System.out.println("What is the weighting for knowledge?"); 
      knowledgeweight = Scan.nextLine(); 

      System.out.println("What is your knowledge mark for the test/assignment?"); 
      maxK = Scan.nextLine(); 

      System.out.println("What is the weighting for communication?"); 
      communicationweight = Scan.nextLine(); 

      System.out.println("What is your communication mark for the test/assignment?"); 
      maxC = Scan.nextLine(); 

      System.out.println("What is the weighting for application?"); 
      applicationweight = Scan.nextLine(); 

      System.out.println("What is your application mark for the test/assignment?"); 
      maxA = Scan.nextLine(); 

      System.out.println("What is the weighting for knowledge?"); 
      thinkingweight = Scan.nextLine(); 

      System.out.println("What is your thinking mark for the test/assignment?"); 
      maxT = Scan.nextLine(); 

      average3 = (maxK * knowledgeweight + maxC * communicationweight + maxT * thinkingweight + maxA * applicationweight); 

      System.out.println("Your final average for this test/assignment is" + average3); 

      System.out.println("What is the weight of the test/assignment (10, 30, 50)?"); 
      weightaverage = Scan.nextInt; 

      weightaverage = (average3 * weightaverage); 

      System.out.println("Your assignment/test weighted is" + weightaverage); 

      int length; 

      Scanner scan = new Scanner(System.in); 

      System.out.println("How many classes do you have?"); 

      length = scan.nextInt(); 

      int[] firstArray = new int[length]; 

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

       System.out.println("What is your course mark for each class in order" + (1 + i) + "?"); 

       firstArray[i] = scan.nextInt[]; 

     } 
    } 
} 

Hier ist mein Code. Der Fehler befindet sich am unteren Rand der Seite. Danke im Voraus!Ich bekomme immer den Java: 122 Fehler: '.class' erwartet. Wie löse ich es?

Bitte hep mir dieses Problem beheben, wie ich weiß nicht, was

zu tun Es ist mein einziger Fehler zu kompilieren links.

Danke und sehr geschätzt!

Bearbeiten: Formatiert, um Code in einem Snippet anzuzeigen.

+1

Wow, erhalte ich 22 Fehler in diesem Code kompilieren. * "Fehler ist in der Nähe der Unterseite" * und * "nur Fehler übrig" * !! Ja, genau!!! – Andreas

+0

Wirklich, verwenden Sie den Code, den sie behoben haben und es funktioniert. – alfnks

Antwort

1

Sie fehlen () in Ihren Anruf nextInt() bei

weightaverage = Scan.nextInt; 

ändern es so etwas wie

weightaverage = Scan.nextInt(); 

und

firstArray[i] = scan.nextInt[]; 

sollte

firstArray[i] = scan.nextInt(); 

Und, sind Sie nicht konsistent mit Ihrem Scanner Namen.

Scanner sc = new Scanner(System.in); 

benötigt

Scanner Scan = new Scanner(System.in); 

wenn Sie Scan.nextInt() anrufen werden sein. Außerdem rufen Sie nextLine() (die eine String zurückgibt) und das Ergebnis zu int Variablen zuweisen.

+0

Es sagt immer noch 1 Fehler aus irgendeinem Grund – alfnks

+0

Scheint besser, aber jetzt führt es zu mehr Fehlern, die das gleiche sind. String kann nicht in int konvertiert werden. Wie repariere ich das? – alfnks

1

Hier ist der Code korrigiert mit Vorschlägen von Elliott Frisch.

Um es zu wiederholen:

  • Sie erklären Scanner mit dem Variablennamen sc, verwendete aber Scan.nextLine() im Rest des Codes. Die Korrektur ist sc.nextLine().
  • sc.nextLine() kann nicht in String konvertiert werden. Es gibt eine ganze Zahl zurück, so dass beispielsweise int knowledgemark = sc.nextInt(); korrekt ist.
  • Einige Codes verwenden sc.nextInt; oder sc.nextInt[];, die zu sc.nextInt(); korrigiert werden sollten.

    import java.util.*; 
    
    public class Averages { //name class as public 
    
        public static void main(String[] args) { 
    
         Scanner sc = new Scanner(System.in); //initialized string using 'new' 
    
         String Course; 
         int knowledgemark; 
         int communicationmark; 
         int thinkingmark; 
         int applicationmark; 
         int average; 
         int finalexam; 
         int average2; 
         String answer; 
         int maxK; 
         int maxC; 
         int maxA; 
         int maxT; 
         int knowledgeweight; 
         int communicationweight; 
         int thinkingweight; 
         int applicationweight; 
         String Assignment; 
         String Test; 
         int average3; 
         int weight; 
         int weightaverage; 
         int average4 = 0; 
    
         System.out.println("What class are you calculating for?"); 
         Course = sc.nextLine(); 
    
         System.out.println("What's your knowledge mark for this course?"); 
         knowledgemark = sc.nextInt(); 
    
         System.out.println("What's your application mark for this course?"); 
         applicationmark = sc.nextInt(); 
    
         System.out.println("What's your thinking mark for this course?"); 
         thinkingmark = sc.nextInt(); 
    
         System.out.println("What's your communication mark for this course?"); 
         communicationmark = sc.nextInt(); 
    
         average = ((knowledgemark + communicationmark + applicationmark + thinkingmark)/4); 
         average = sc.nextInt(); 
    
         System.out.println("Your average for" + Course + "is" + average); 
    
         System.out.println("Do you want to calculate your exam with your final exam mark? (yes or no)"); 
         answer = sc.next(); 
    
         if (answer.equals("yes")) { 
          System.out.println("What is your final exam mark?"); 
          finalexam = sc.nextInt(); 
          average2 = (((average * 70) + finalexam * 30)/100); 
          System.out.println("Your final average in this course is" + average2); 
         } else { 
          System.out.println("You average for" + Course + "is" + average); 
         } 
    
         System.out.println("You can also calculate your marks with tests/assignments"); 
         Test = sc.nextLine(); 
    
         System.out.println("What test/assignment are you calculating for?"); 
         Test = sc.nextLine(); 
    
         System.out.println("What is the weighting for knowledge?"); 
         knowledgeweight = sc.nextInt(); 
    
         System.out.println("What is your knowledge mark for the test/assignment?"); 
         maxK = sc.nextInt(); 
    
         System.out.println("What is the weighting for communication?"); 
         communicationweight = sc.nextInt(); 
    
         System.out.println("What is your communication mark for the test/assignment?"); 
         maxC = sc.nextInt(); 
    
         System.out.println("What is the weighting for application?"); 
         applicationweight = sc.nextInt(); 
    
         System.out.println("What is your application mark for the test/assignment?"); 
         maxA = sc.nextInt(); 
    
         System.out.println("What is the weighting for knowledge?"); 
         thinkingweight = sc.nextInt(); 
    
         System.out.println("What is your thinking mark for the test/assignment?"); 
         maxT = sc.nextInt(); 
    
         average3 = (maxK * knowledgeweight + maxC * communicationweight + maxT * thinkingweight + maxA * applicationweight); 
    
         System.out.println("Your final average for this test/assignment is" + average3); 
    
         System.out.println("What is the weight of the test/assignment (10, 30, 50)?"); 
         weightaverage = sc.nextInt(); 
    
         weightaverage = (average3 * weightaverage); 
    
         System.out.println("Your assignment/test weighted is" + weightaverage); 
    
         int length; 
    
         Scanner scan = new Scanner(System.in); 
    
         System.out.println("How many classes do you have?"); 
    
         length = scan.nextInt(); 
    
         int[] firstArray = new int[length]; 
    
         for (int i = 0; i < length; i++) { 
    
          System.out.println("What is your course mark for each class in order" + (1 + i) + "?"); 
    
          firstArray[i] = scan.nextInt(); 
    
         } 
        } 
    } 
    
+0

Wenn Ihnen meine Antwort oder der Aufwand von @ elliottt-frisch gefällt, bitte als Antwort markieren! Prost. –

Verwandte Themen