This is what I'm trying to do:
Write a Java program that reads a sequence of student records and prints the total score for each student. Each record has the student’s first and last name, followed by a sequence of test scores and a sentinel of –1
. The sequence is terminated by the word END
.
Here is a sample sequence:
Harry Morgan 94 71 86 95 -1
Sally Lin 99 98 100 95 90 -1
END
Please note: • Hint: you need nested loops • Validate the user’s entry as needed • Don’t use arrays, you don’t have to save your values.
Here is my code so far(when I run it, it infinitely loops, my "END" won't stop the loop):
import java.util.Scanner;
public class loopshelp {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Scanner input = new Scanner(System.in);
String name = "0";
while(name != "END") {
int sum = 0;
String f_name = "";
String l_name = "";
System.out.println("Enter student's first name[enter 'END' when done]");
f_name = input.next();
System.out.println("Enter student's last name[enter 'END' when done]");
l_name = input.next();
name = f_name;
int value = 0;
while (value != -1) {
System.out.println("Enter a value[enter -1 to stop]");
int num = keyboard.nextInt();
sum = sum + num;
value = num;
}//while value != -1
System.out.println(f_name + " " + l_name + ": "+ (sum + 1));
}//while f_name != end
keyboard.close();
input.close();
}
}