I have developed a code to get the input details of the students marks and print the average as the output. This operation will be performed until the user gives an "n" as an input for the looping condition.But in my code, the user is unable to give the conditional input and the automatically performs the while loop
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();
}
}
}
ACTUAL OUTPUT :
C:\Program Files\Java\jdk-12.0.2\bin>java Main1
Enter the marks for maths,physics,chemistry,CSE along with your Reg.no first
100
100
100
100
100
press 'Y' to continue and 'N' to exit
S
Enter the marks for maths,physics,chemistry,CSE along with your Reg.no first