Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with accumulating sum values in do while loop

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    29
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default 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.

    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!");
     
    	}}
    Last edited by steel55677; June 18th, 2011 at 06:04 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,143
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Help with accumulating sum values in do while loop

    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);

  3. The Following User Says Thank You to Norm For This Useful Post:

    steel55677 (June 18th, 2011)

  4. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    29
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default 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

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,512
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Help with accumulating sum values in do while loop

    There is no such thing as an if loop!

  6. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,512
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default 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.

Similar Threads

  1. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM
  2. MouseListener accumulating mouse clicks
    By mycallsevern in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 19th, 2011, 01:15 PM
  3. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM
  5. Java Loops - accumulating totals
    By iv3java in forum Loops & Control Statements
    Replies: 0
    Last Post: December 14th, 2009, 09:10 PM