Java 未执行字符串输入

Java 未执行字符串输入,java,while-loop,Java,While Loop,我开发了一个代码来获取学生分数的输入细节,并将平均值作为输出打印出来。此操作将一直执行,直到用户给出一个“n”作为循环条件的输入。但是在我的代码中,用户无法给出条件输入,并且自动执行while循环 import java.util.Scanner; class Student { String reg; int maths,phy,che,cse,avg; void cal() { if(maths<50 | phy<50 | che<50 | cs

我开发了一个代码来获取学生分数的输入细节,并将平均值作为输出打印出来。此操作将一直执行,直到用户给出一个“n”作为循环条件的输入。但是在我的代码中,用户无法给出条件输入,并且自动执行while循环

import java.util.Scanner;


class Student

 {

   String reg;

  int maths,phy,che,cse,avg;

void cal()

{

if(maths<50 | phy<50 | che<50 | cse<50)

{

System.out.println("The grade of the student will not be computed");

}

else

{

avg=((maths+phy+che+cse)/4);

if(avg>=91)

{

System.out.println("S");

}

if(avg>=81 && avg<=90)

{

System.out.println("A");

}



if(avg>=71  && avg<=80)

{

System.out.println("B");

}



if(avg>=61  && avg<=70)

{

System.out.println("C");

}


if(avg>=50 && avg<=60)

{

System.out.println("D");

}



}


}

}







 class Main1    //3rd

{

        public static void main(String args[]) throws Exception {

            Scanner w = new Scanner(System.in);
            String r = "Y", a = "Y";

            while (r.contains(a)) {

                Student s = new Student();
                System.out.println("Enter the marks for maths,physics,chemistry,CSE along with your Reg.no first");
                s.reg = w.nextLine();
                s.maths = w.nextInt();
                s.phy = w.nextInt();
                s.che = w.nextInt();
                s.cse = w.nextInt();
                System.out.println("press 'Y' to continue and 'N' to exit");
                a = w.nextLine();
                s.cal();

            }

        }

    }
import java.util.Scanner;
班级学生
{
字符串注册表;
国际数学、物理、化学、化学、化学、平均;
void cal()
{

如果(数学请在
System.out.println之前添加
w.nextLine();
(“按“Y”继续,按“N”退出”);
那么主要方法应该如下所示:

public static void main(String args[]) throws Exception {
    Scanner w = new Scanner(System.in);
    String r = "Y", a = "Y";

    while (r.contains(a)) {

        Student s = new Student();
        System.out.println("Enter the marks for maths,physics,chemistry,CSE along with your Reg.no first");
        s.reg = w.nextLine();
        s.maths = w.nextInt();
        s.phy = w.nextInt();
        s.che = w.nextInt();
        s.cse = w.nextInt();
        w.nextLine(); //------> Added
        System.out.println("press 'Y' to continue and 'N' to exit");
        a = w.nextLine();
        s.cal();

尝试r.equals(a)而不是contains。更新a后,还要添加一个打印,以查看a的值。请按照复制
扫描仪中提到的方法放置学生类代码。nextInt
不注册换行符,因此当您按enter键时,它将跳过最后一个nextLine语句Try
Integer.parseInt(input.nextLine())
对于所有整数输入。我确实尝试了r.equals(a),但它仍然倾向于跳过值“a”的更新部分@KarthikeyanVaithilingam是的,你是对的……但我的不是整数输入,因为我使用字符串作为输入代码中有什么问题?nextLine不读取下一行字符,只读取输入的数字,这就是为什么w.nextLine(在a=w.nextLine();)是的,这里有更多信息