Help with accumulating sum values in do while loop
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.
Code :
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!");
}}
Re: Help with accumulating sum values in do while loop
Quote:
score will end up either 1 - 1 or 0 - 0, 1 - 0 , 0 - 1,
You must be resetting the score for it to not be greater than 1. Check your logic.
Make sure you init the variable outside of the loop.
If you can't see it, add println statements to print out the value of the variables as they change and the output should show you where the problem is.
Add an id when printing so you know which print generated the output:
System.out.println("Here var=" + var);
Re: Help with accumulating sum values in do while loop
Thanks! i just put the init values before the if loops...oh man, i spent two days tryin to fix this myself 8D
Re: Help with accumulating sum values in do while loop
There is no such thing as an if loop!
Re: Help with accumulating sum values in do while loop
Also, don't cram all your code into the main method. Try and break it down into smaller chunks. Then put each smaller chunk into its own method.