2017-04-21 203 views
0

我對Java很新。所以我創建了一個腳本來接收分數的輸入,然後根據這個分數給出一個標記作爲輸出。我的問題是我想重複代碼以允許輸入多個分數,但是我無法使其工作。循環問題

編輯:我已經嘗試過使用方法中的答案,但我不能正確的。有人可能爲我執行循環代碼到我的代碼中嗎? 這裏是我的代碼:

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

     { 

    String studentname; 
    int mark = 100; // listing maximum 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.print("The grade for " + studentname + " is F "); 
    else if(mark <65) System.out.print("The grade for " + studentname + " is P "); 
    else if(mark <75) System.out.print("The grade for " + studentname + " is C "); 
    else if(mark <85) System.out.print("The grade for " + studentname + " is D "); 
    else System.out.print("The grade for " + studentname + " is HD2"); 
     } 
    } 
} 
+0

內的包裹你代碼for循環用的次數將它需要循環例如'的for(int i = 1; I <= 10;我++) {}'循環10次..添加循環上方的掃描儀輸入即上面'掃描儀inText =新掃描儀(System.in);' –

+0

您可以使用'while'循環爲您的目標。看看一些while循環文檔。 –

回答

0

首先,讓我們重構的主要邏輯爲()稱爲calcGrade另一種方法:

public void calcGrade() { 

    String studentname; 
    int mark = 100; // listing maximum 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.print("The grade for " + studentname + " is F "); 
    else if(mark <65) System.out.print("The grade for " + studentname + " is P "); 
    else if(mark <75) System.out.print("The grade for " + studentname + " is C "); 
    else if(mark <85) System.out.print("The grade for " + studentname + " is D "); 
    else System.out.print("The grade for " + studentname + " is HD2"); 
} 

如果我們調用這個方法,它會加載一個新的學生姓名&得分從System.in,計算等級,然後打印它。

好的,接下來的部分將是循環。

Java中有3種類型的循環,用於/ while/do-while。

當你確切知道你想循環的次數時,你可以使用「for」。

E.g.你知道只有10名學生在你的類,你可以寫這樣的代碼:

for (int i = 0; i < 10; i++) { 
    calcGrade(); 
} 

如果你不知道的時候,但你知道有一個確切的條件來結束循環,你可以使用同時或做某事。 while和do-while之間的區別是,雖然可以先執行條件檢查,然後執行內部邏輯,然後執行內部邏輯,然後檢查條件。

E.g.當您從System.in中獲取字符串「是」時,您想要繼續循環。

System.out.println("Please input the first student info, YES or NO?"); 
Scanner inText = new Scanner(System.in); 
while ("YES".equals(inText.nextLine()) { 
    calcGrade(); 
    System.out.println("Continue input the next student info, YES or NO?"); 
} 

另外,如果您知道班級中至少有一個人,您可以使用do-while。

Scanner inText = new Scanner(System.in); 
do { 
    calcGrade(); 
    System.out.println("Continue input the next student info, YES or NO?"); 
} while ("YES".equals(inText.nextLine()); 

希望它能明顯你;)

0

我能想到的最簡單的WWAY是創建一個類名爲學生,如果你想有姓名,科目,分數等變量的getter和setter方法或者只是有一個構造函數,需要在這些投入。接下來有一個像computeGrade()這樣的方法。每次你想要一些東西的時候創建這個學生類的實例。

puclic class Student{ 

public String mName; 
public String mSub1; 
. 
public int m_scoreSub1; 
. 
. 
public computeScore(int m_score){ 
* your logic goes here (the if else one) 
} 
} 

現在只是實例化類!