2017-04-12 29 views
0

我必須創建一個程序來計算每個學生的平均分數。我設法做到了這一點,但是我怎樣才能將分數限制在0到100之間?我已經搜索了其他問題和許多節目,以便在聲明期間放置。問題是我不知道該在哪裏添加。所以這裏是代碼:如何在for循環中設置限制?

import java.util.Scanner; 

public class AverageScore { 
public static void main(String[] args) { 

    int x; // Number of students 
    int y; // Number of tests per student 
    int Score = 0; //Score of each test for each student 
    double Average = 0; //Average score 
    double Total = 0; //Total score 

    Scanner keyboard = new Scanner(System.in); 

    System.out.println("Please enter the number of students: "); 
    x = keyboard.nextInt(); 

    System.out.println("Please enter the amount of test scores per student: "); 
    y = keyboard.nextInt(); 

    for (int z = 0; z < x; z++) 
    {   
    System.out.println("Student " + (z + 1)); 
    System.out.println("------------------------"); 

    for (int g=0; g < y; g++) 
    { 
     System.out.print("Please enter score " + (g + 1) + ": "); 

     Total += new Scanner(System.in).nextInt(); 
     Total += Score; 
     Average = (Total/y); 
    }  

    System.out.println("The average score for student " + (z + 1) + " is " + Average); 
    System.out.println("      "); 

    Total= 0; 
    } 

    keyboard.close(); 
} 
} 

如果還有其他方式請做狀態。提前致謝。

+1

假設您需要驗證輸入的測試分數爲0-100。然後不需要時,只需將if else子句檢查輸入的分數是否在範圍內。 –

+0

是的,這是問題,我不知道如何把其他 – Farhan

+0

放在要求輸入分數之前。拋開煩惱,你爲什麼要分兩次?在第二個系統出來和g循環? – jace

回答

0
import java.util.Scanner; 

public class AverageScore { 
public static void main(String[] args) { 

    int x; // Number of students 
    int y; // Number of tests per student 
    int Score = 0; //Score of each test for each student 
    double Average = 0; //Average score 
    double Total = 0; //Total score 
    double Input = 0; **//Add this in your variable** 
    boolean Valid = false; **//Add this in your variable** 

     Scanner keyboard = new Scanner(System.in); 

     System.out.println("Please enter the number of students: "); 
     x = keyboard.nextInt(); 

     System.out.println("Please enter the amount of test scores per student: "); 
     y = keyboard.nextInt(); 

     for (int z = 0; z < x; z++) 
     { 
      System.out.println("Student " + (z + 1)); 
      System.out.println("------------------------"); 

      for (int g=0; g < y; g++) 
      { 
       System.out.print("Please enter score " + (g + 1) + ": "); 
       Input = new Scanner(System.in).nextInt(); 

       //validation of your input from 0 to 100 
       if(Input>=0&&Input<=100) 
       { 
        Valid = true; 
       } 

       //enter while loop if not valid 
       while(!Valid) 
       { 
        System.out.println(""); 
        System.out.print("Please enter a valid score " + (g + 1) + ": "); 
        Input = new Scanner(System.in).nextInt(); 
        if(Input>=0&&Input<=100) 
        { 
         Valid = true; 
        } 
       } 

       Valid = false; //reset validation; 

       Total += Input; 
       Average = (Total/y); 
      }  

      System.out.println("The average score for student " + (z + 1) + " is " + Average); 
      System.out.println("      "); 

      Total= 0; 
     } 

     keyboard.close(); 
    } 
} 
+0

完美謝謝! – Farhan

+0

沒問題。 :)快樂編碼! – jace

1

一個簡單的方法去了解這將是把用戶輸入的提示while循環內,只有突破了,一旦你驗證過級是有效的:

Scanner scanner = new Scanner(System.in); 

int score; 

while (true) { 
    System.out.print("Please enter score " + (g + 1) + ": "); 

    score = scanner.nextInt(); 

    if (score >= 0 && score <= 100) { 
     break; 
    } 

    System.out.println("Please enter a valid score between 0 and 100!"); 
} 

Total += score; 

記住關閉你的Scanner s,以避免內存泄漏!

+0

@OusmaneMahyDiaw哦?如果分數無效,爲什麼我會跳出循環? –

+0

@OusmaneMahyDiaw條件與break語句正確,意思是在輸入正確時跳過 – jace

+1

@OusmaneMahyDiaw不要擔心;它發生在我們最好的人身上! –