Hi,
I can't seem to figure a way to add the totalUserscore and totalCompScore. What I mean is my program is a little game that adds the correct # of answer by user and computerAI. But every time the score will end up either 1 - 1 or 0 - 0, 1 - 0 , 0 - 1, and will not add to i.e. 2- 1 , 3- 2.
Please help. I'm a beginner in Java, and it's my second project.
import java.text.DecimalFormat; import java.util.Scanner; import java.util.Random; public class prime { private static final String Y = null; private static final String N = null; public static void main(String[] args) { String Quit = null; char finalUseranswer; char Q; Scanner keyboard = new Scanner(System.in); //Pretext System.out.println("ャャャャャャャャャャャャャャャャャャャャャャャャャャャャャ"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes, N = No, Q = Quit"); System.out.println("ャャャャャャャャャャャャャャャャャャャャャャャャャャャャャ"); //Starts the loop do{ //Declares randomization of prime numbers Random randomNumber = new Random(); int num = randomNumber.nextInt(1000); int i; //value to terminate loop char compAnswer = 0; //Prime calculation for (i=2; i < num ;i++ ){ int n = num%i; if (n==0){ compAnswer = 'N'; //compAnswer decides whether or not value is prime. Yes = Y, No = N. break; } } if(i == num){ compAnswer = 'Y'; } //ends //Randomization of numbers to decide if it's odd or even Random aiRandom = new Random(); //aiRandom generates from 0-51 int aiComp = aiRandom.nextInt(50)+1; char trueCompAnswer; //trueCompAnswer is answer by computer's AI. if (aiComp % 2 == 0) { trueCompAnswer = 'Y'; //If number is even, value is Y in unicode } else { trueCompAnswer = 'N'; //if number is odd, value is N in unicode } System.out.println(); //displays user's needed input System.out.println("Is " + num + " a prime number? "); System.out.print("User answers..."); String userAnswer1 = keyboard.nextLine(); //userAnswer1 is user's default input String userAnswer2 = userAnswer1.toUpperCase(); //userAnswer 2 is user's default input converted to all Uppercase finalUseranswer = userAnswer2.toUpperCase().charAt(0); //finalUseranswer is location of first letter of user's default input's converted answer. int Userscore = 0, compScore = 0; //initialization the scores { //Begins if statements. Compares user's answer with computerAi(computer player) answer with the real prime answer. Then executes whichever logical statements are true. //If User is correct, then user gets one point( accumlator) and if Computer is correct, then computer gets one point. If both answers wrong, both gets 0. if (trueCompAnswer == compAnswer && finalUseranswer == compAnswer &&( userAnswer2.equalsIgnoreCase("YES")|| userAnswer2.equalsIgnoreCase("NO"))|| finalUseranswer == 'Y' ||finalUseranswer =='N' ){ System.out.println("Computer answers..." +trueCompAnswer); System.out.println("Correct answer: " + compAnswer); ++Userscore; ++compScore; } else if (trueCompAnswer != compAnswer && finalUseranswer == compAnswer &&( userAnswer2.equalsIgnoreCase("YES")|| userAnswer2.equalsIgnoreCase("NO"))|| finalUseranswer == 'Y' ||finalUseranswer =='N') { System.out.println("Computer answers..." +trueCompAnswer); System.out.println("Correct answer: " + compAnswer); ++Userscore; } else if (trueCompAnswer == compAnswer && finalUseranswer != compAnswer &&( userAnswer2.equalsIgnoreCase("YES")|| userAnswer2.equalsIgnoreCase("NO"))|| finalUseranswer == 'Y' ||finalUseranswer =='N') { System.out.println("Computer answers..." +trueCompAnswer); System.out.println("Correct answer: " + compAnswer); ++compScore; } else if (trueCompAnswer != compAnswer && finalUseranswer != compAnswer &&( userAnswer2.equalsIgnoreCase("YES")|| userAnswer2.equalsIgnoreCase("NO"))|| finalUseranswer == 'Y' ||finalUseranswer =='N') { System.out.println("Computer answers..." +trueCompAnswer); System.out.println("Correct answer: " + compAnswer); } //End if statements else if(finalUseranswer =='Q') { } else{//Validation System.out.println("Please enter Yes, No, or Quit!"); System.out.println(); } //Calculation of total points int totalcompScore = 0, totaluserScore = 0; System.out.println("Computer Score: " + (totalcompScore= totalcompScore+compScore)); System.out.println("User Score: " + (totaluserScore= totaluserScore+Userscore)); }} while (finalUseranswer !='Q'); //Sentinel value of 'Q' which indicates termination of program System.out.println("Thank you for playing!"); }}