2017-04-21 148 views
0

我對Java很新,whixh解釋了loq質量代碼。我正在計算我在我的代碼中輸入的分數的平均值,該分數在下面,但它只是獲得了我在其他代碼執行後輸入的數字的平均值。我怎樣才能找到我在代碼中使用這行代碼的平均分數:
System.out.printf(「%。2f」,average); 爲了清楚我想要在輸入星號之後輸入的數字的平均值。 感謝查找分數的平均值

import java.util.Scanner; 
public class week4 
{ 
    public static void main(String[] args) { 

int n =1; 

{ 
    do{ 
    n++; 
    String studentname; 

    int mark; 

    Scanner inText = new Scanner(System.in); 

    System.out.print("Please enter the name of the student >> "); 

    studentname = inText.nextLine(); 

    Scanner inNumber = new Scanner(System.in); 

    *System.out.print("Please enter mark for student " + studentname + " out of 100 >> ");* 


    mark = inText.nextInt(); 

    if(mark <50) System.out.println("The grade for " + studentname + " is F "); 

    else if(mark <65) System.out.println("The grade for " + studentname + " is P "); 

    else if(mark <75) System.out.println("The grade for " + studentname + " is C "); 

    else if(mark <85) System.out.println("The grade for " + studentname + " is D "); 

    else System.out.println("The grade for " + studentname + " is HD"); 

    } while(n <=3); 

    Scanner input = new Scanner(System.in); 

       int sum = 0; 
       int mark; 
       int average; 
       int counter = 1; 

       while (counter < 2){ 
        mark = input.nextInt(); 
        sum = sum + mark; 
        counter++; 
        average = sum/3; 
        System.out.printf("The aerage score is " + average ); 
       } 


     } 
    } 
} 
+0

你的平均值不應該是浮點數嗎? – 2017-04-21 11:26:59

+0

在我們繼續之前:你想把標記存儲在'mark'變量中,在第一個'do-while'循環中讀取它們,然後計算平均值,對吧? – gonczor

+0

在系統輸入中有很多'Scanner'。你在每個循環中使用相同的分頻器進行平均處理,但是條件不匹配,在停止循環之前只能讀取一個值 – AxelH

回答

0

你在最後這一點的同時代碼塊是不必要的:

    while (counter < 2){ 
        mark = input.nextInt(); 
        sum = sum + mark; 
        counter++; 
        average = sum/3; 
        System.out.printf("The average score is " + average ); 

您應該添加一個全局變量如總。當用戶輸入他們的標記時,將每個用戶分數存儲在總變量中。用你的代碼替換(用粗體突出顯示):

public class week4 { 

    public static void main(String[] args) { 
     int total = 0; 

     int n = 1; 

     { 
      do { 
       n++; 
       String studentname; 

       int mark; 

       Scanner inText = new Scanner(System.in); 

       System.out.print("Please enter the name of the student >> "); 

       studentname = inText.nextLine(); 

       Scanner inNumber = new Scanner(System.in); 

       //* 
       System.out.print("Please enter mark for student " + studentname + " out of 100 >> "); 

       mark = inText.nextInt(); 
       **total += mark;** 

       if (mark < 50) { 
        System.out.println("The grade for " + studentname + " is F "); 
       } else if (mark < 65) { 
        System.out.println("The grade for " + studentname + " is P "); 
       } else if (mark < 75) { 
        System.out.println("The grade for " + studentname + " is C "); 
       } else if (mark < 85) { 
        System.out.println("The grade for " + studentname + " is D "); 
       } else { 
        System.out.println("The grade for " + studentname + " is HD"); 
       } 

      } while (n <= 3); 

      Scanner input = new Scanner(System.in); 

      int sum = 0; 
      int mark; 
      int average; 
      int counter = 1; 

      **average = total/3; 
      System.out.printf("The aerage score is " + average);** 

     } 
    }