Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 循环问题_Java_Loops - Fatal编程技术网

Java 循环问题

Java 循环问题,java,loops,Java,Loops,我对Java非常陌生。所以我创建了一个脚本来接收分数的输入,然后根据这个分数给一个分数作为输出。我的问题是我希望代码重复以允许输入多个分数,但我无法让它工作 编辑:我试过使用答案中的方法,但我做不好。是否有人可以为我在代码中实现循环? 这是我的密码: import java.util.Scanner; public class week4 { public static void main(String[] args) { { String studentna

我对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" );
        }
    }
}
import java.util.Scanner;
公共课周4
{
公共静态void main(字符串[]args)
{
{
字符串studentname;
int mark=100;//列出最大标记
扫描仪inText=新扫描仪(System.in);
系统输出打印(“请输入学生姓名>>”;
studentname=inText.nextLine();
扫描仪inNumber=新扫描仪(System.in);
系统输出打印(“请为学生输入分数”+studentname+“满分100>>”;
mark=inText.nextInt();

如果(mark我能想到的最简单的方法是创建一个名为student的类,并为名称、主题、分数等设置变量。如果需要,可以使用setter和getter,或者只使用一个接受这些输入的构造函数。接下来使用一个类似computeGrade()的方法。每次需要时都创建这个student类的实例

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)
}
}

现在只需实例化该类!!!

首先,让我们将主逻辑重构为另一个名为calcGrade()的方法:

另外,如果你知道班上至少有一个人,你可以使用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());

希望你能明白;)

将你的代码包装在for循环中,并附上你需要它循环的次数,例如
for(int i=1;i你可以使用
while
loop实现你的目标。查看一些while循环文档。
for (int i = 0; i < 10; i++) {
    calcGrade();
}
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?");
}
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());