/* * To change this license header, choose License Headers in Project Properties
ID: 671901 • Letter: #
Question
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inclass05;
import java.util.Scanner;
public class InClass05 {
public static void main(String[] args) {
int qCount = 1;
int corrCount = 0;
int errCount = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of questions in the quiz? ");
int numOfQ = input.nextInt();
while (qCount <= numOfQ){
int num1 = (int)(Math.random() * 10);
int num2 = (int)(Math.random() * 10);
System.out.print("Question " + qCount + ": What is " + num1 + "*"+ num2 + "? ");
int answer = input.nextInt();
if(answer == num1*num2){
System.out.println("Correct! " );
corrCount++;
}
else {
System.out.println("Incorrect! ");
errCount++;
}
qCount++;
}
System.out.println("You got " + errCount + " wrong! Your score is " + (corrCount/numOfQ) + "!");
if (errCount == 0){
System.out.print(" Congradulations! ");
}
}
}
(corrCount/numOfQ) always equaling zero insead of a the correct answers / number of questions
Explanation / Answer
import java.util.Scanner;
public class InClass05 {
public static void main(String[] args) {
int qCount = 1;
int corrCount = 0;
int errCount = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of questions in the quiz? ");
int numOfQ = input.nextInt();
while (qCount <= numOfQ){
int num1 = (int)(Math.random() * 10);
int num2 = (int)(Math.random() * 10);
System.out.print("Question " + qCount + ": What is " + num1 + "*"+ num2 + "? ");
int answer = input.nextInt();
if(answer == num1*num2){
System.out.println("Correct! " );
corrCount++;
}
else {
System.out.println("Incorrect! ");
errCount++;
}
qCount++;
}
System.out.println("You got " + errCount + " wrong! Your score is " + (corrCount) + "!");
if (errCount == 0){
System.out.print(" Congradulations! ");
}
}
}
corrected to find the correct answer count you have defined corrCount to output number of correct answers its enough.so remove corrCount/numOfQ to corrCount.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.