我對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");
}
}
}
內的包裹你代碼for循環用的次數將它需要循環例如'的for(int i = 1; I <= 10;我++) {}'循環10次..添加循環上方的掃描儀輸入即上面'掃描儀inText =新掃描儀(System.in);' –
您可以使用'while'循環爲您的目標。看看一些while循環文檔。 –