My homework was to create a program that prompts the user with questions.
They will get 5 attempts, each attempt that they guess incorrectly the program will deduct 5 points until they reach 0.
Each question is worth 20 points.
I have made the code as best as I can but for some reason I cannot total all of the scores for each question without getting the "incompatible type" error.
import java.util.Scanner; // Needed for the Scanner class /** This program demonstrates a case insensitive string comparison. */ public class FiveQuestionsII { public static void main(String[] args) { //QUESTION 1 int number1 = 20; String total; String ans1, ans2, ans3, ans4, ans5; Scanner keyboard = new Scanner(System.in); int counter = 0; System.out.print("1. What is each repetition of a loop known as?\nAnswer: "); ans1 = keyboard.nextLine(); while(counter < 4 && !ans1.equals("iteration") && !ans1.equals("Iteration")) { System.out.print("Incorrect, please try again " + "\nAnswer: "); counter++; number1 = 20 - (counter*5); ans1 = keyboard.nextLine(); } System.out.println("You scored " + number1 + " points out of 20 for that question.\n(Answer: iteration)"); //QUESTION 2 int number2 = 20; System.out.print("2. The while loop is this type of loop...\nAnswer: "); ans2 = keyboard.nextLine(); while(counter < 4 && !ans2.equals("pretest") && !ans2.equals("Pretest")) { System.out.print("Incorrect, please try again " + "\nAnswer: "); counter++; number2 = 20 - (counter*5); ans2 = keyboard.nextLine(); } System.out.println("You scored " + number2 + " points out of 20 for that question.\n(Answer: pretest)"); //QUESTIONS 3 int number3 = 20; System.out.print("3. This is a special value that signals when there are no more items from a ist of items to be processed...\nAnswer: "); ans3 = keyboard.nextLine(); while(counter < 4 && !ans3.equals("sentinel") && !ans3.equals("Sentinel")) { System.out.print("Incorrect, please try again " + "\nAnswer: "); counter++; number3 = 20 - (counter*5); ans3 = keyboard.nextLine(); } System.out.println("You scored " + number3 + " points out of 20 for that question.\n(Answer: sentinel)"); //QUESTION 4 int number4 = 20; System.out.print("4. This class allows you to read a line from a file... \nAnswer: "); ans4 = keyboard.nextLine(); while(counter < 4 && !ans4.equals("Scanner") && !ans4.equals("scanner")){ System.out.print("Incorrect, please try again " + "\nAnswer: "); counter++; number4 = 20 - (counter*5); ans4 = keyboard.nextLine(); } System.out.println("You scored " + number4 + " points out of 20 for that question.\n(Answer: scanner)"); //QUESTION 5 int number5 = 20; System.out.print("5. This type of loop has no way of ending and repeats until the program is interrupted... \nAnswer: "); ans5 = keyboard.nextLine(); while(counter < 4 && !ans5.equals("Infinite") && !ans5.equals("infinite")){ System.out.print("Incorrect, please try again " + "\nAnswer: "); counter++; number5 = 20 - (counter*5); ans5 = keyboard.nextLine(); } System.out.println("You scored " + number5 + " points out of 20 for that question.\n(Answer: infinite"); //TOTAL SCORING total = number1 + number2 + number3 + number4 + number5; System.out.println("\n\nYour total score:" + total); } }
ERROR:
FiveQuestionsII.java:98: incompatible types found : int required: java.lang.String total = number1 + number2 + number3 + number4 + number5; ^ 1 error
I'm a beginner, sorry if my code is sloppy.