So, I guess I have a stupid mistake here someplace
I'm sorry, but I'm pulling my hair out here just trying to find out where I went wrong. I had to create a loop (I used while) at the end of this program so that the teacher using it can grade multiple quizzes on the same key. It compiles and runs fine until the very end where it asks if I would like to continue with another quiz. It prints out the message, but doesn't give me a chance to respond and instead automatically ends the program. I even looked up a very similar example in my text book, and as far as I can tell, they did it exactly like I did. Any help would be greatly appreciated. Thank you.
import java.util.Scanner;
public class Grades
{
public static void main (String[] args)
{
int questions;
int counter = 0;
int average;
String another = "y";
Scanner scan = new Scanner(System.in);
System.out.print("Please enter the number of questions on the quiz: ");
questions = scan.nextInt();
int[] key = new int [questions + 1];
int[] answer = new int [questions + 1];
for (int i=1; i<key.length; i++)
{
System.out.print("Please enter the answer to question " + i + ": ");
key = scan.nextInt();
}
while(another.equalsIgnoreCase("y"))
{
for (int i=1; i><key.length; i++)
{
System.out.print("Please enter the answer the student gave for question " + i + ": ");
answer = scan.nextInt();
if(key == answer)
counter++;
}
average = 100 * counter/questions;
System.out.println("The student had a total of " + counter + " correct answers.");
System.out.println("The student's average on the quiz: " + average + "%");
System.out.println();
System.out.print("Grade another quiz (y/n)? ");
another = scan.nextLine();
}
}
}>
Yeah, it makes it all the way down to the final print statement, but I guess the scanner class isn't letting me input a response. I can input information on everything else though.
Let me try posting the code one more time, see if this helps any.
import java.util.Scanner;
public class Grades
{
public static void main (String[] args)
{
int questions;
int counter = 0;
int average;
String another = "y";
Scanner scan = new Scanner(System.in);
System.out.print("Please enter the number of questions on the quiz: ");
questions = scan.nextInt();
int[] key = new int [questions + 1];
int[] answer = new int [questions + 1];
for (int i=1; i<key.length; i++)
{
System.out.print("Please enter the answer to question " + i + ": ");
key[i] = scan.nextInt();
}
while(another.equalsIgnoreCase("y"))
{
for (int i=1; i><key.length; i++)
{
System.out.print("Please enter the answer the student gave for question " + i + ": ");
answer[i] = scan.nextInt();
if(key[i] == answer[i])
counter++;
}
average = 100 * counter/questions;
System.out.println("The student had a total of " + counter + " correct answers.");
System.out.println("The student's average on the quiz: " + average + "%");
System.out.println();
System.out.print("Grade another quiz (y/n)? ");
another = scan.nextLine();
}
}
}
Message was edited by:
kwyjibo348
Message was edited by:
kwyjibo348>
When you do answer[i] = scan.nextInt();
and the user enters the number 6 followed by the ENTER key, the string "6\r\n" (Windows) or "6\n" (*nix) will be created. The nextInt() method only reads the number 6 and leaves the line break in the Scanner. So when you do another nextInt() call on the Scanner, you'll receive a type-mismatch error/exception.
I suggest, when reading numbers and String from the console, to read one line at a time and convert the line to an integer like this:answer[i] = Integer.parseInt(scan.nextLine());
Good luck.