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 4 of 4

Thread: Math Game? not sure whats wrong?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Math Game? not sure whats wrong?

    When it gets to the end, its supposed to ask the user if they want to play again, 1 for yes and 2 for no, it plays again regardless of what you want to do?

    import javax.swing.JOptionPane;
    import java.util.Random;
     
    public class Lab6
    {
    	public static void main(String args[])
    	{
    		int probType, lvl;
    		int opAnd1 = 0, opAnd2 = 0, cAwnser, sAwnser;
    		int correctA;
    		boolean pAgain;
    		int tempBoo;
     
    		Random rndm = new Random();
     
    		do
    		{
     
     
    			String s1 = JOptionPane.showInputDialog("Enter problem type: ");
    			probType = Integer.parseInt(s1);
    			while (probType < 0 || probType > 5)
    			{
    			s1 = JOptionPane.showInputDialog("Invalid number, please try again: ");
    				probType = Integer.parseInt(s1);
    			}
     
     
    			s1 = JOptionPane.showInputDialog("Enter desired level: ");
    		              lvl = Integer.parseInt(s1);
    			while (lvl < 0 ||   lvl > 3)
    			         {
    			          s1 = JOptionPane.showInputDialog("Invalid number, please try again: ");
    			           lvl = Integer.parseInt(s1);
    			            }
     
     
    			correctA = 0;
     
     
    			for(int i = 1; i <= 10; i++)
    			{
     
    				switch (lvl)
    				{
    				case 1:
     
    				opAnd1 = rndm.nextInt(10);
    				opAnd2 = rndm.nextInt(10);
     
    				break;
     
     
    				case 2:
    				opAnd1 = rndm.nextInt(100);
    				opAnd2 = rndm.nextInt(100);
     
    				break;
    				}
     
    			switch (probType)
    				{
    				case 1:
     
    				cAwnser = opAnd1 + opAnd2;
    				JOptionPane.showMessageDialog(null,(opAnd1 + " + " + opAnd2));
    				s1 = JOptionPane.showInputDialog("What is your awnser: ");
    				sAwnser = Integer.parseInt(s1);
    				if (cAwnser == sAwnser)
    					{
    					JOptionPane.showMessageDialog(null,"Good Job!");
    					correctA++;
    					}
    				else
    					{
    					JOptionPane.showMessageDialog(null,"Better luck next time!");
    					}
    						break;
     
     
     
    			case 2:
     
    			cAwnser = opAnd1 - opAnd2;
    			JOptionPane.showMessageDialog(null,(opAnd1 + " - " + opAnd2));
    			s1 = JOptionPane.showInputDialog("What is your awnser: ");
    			sAwnser = Integer.parseInt(s1);
    			if (cAwnser == sAwnser)
    				{
    				JOptionPane.showMessageDialog(null,"Fantastic!");
    				correctA++;
    				}
    			else
    				{
    			              JOptionPane.showMessageDialog(null,"Nice Try!");
    				}
    					break;
     
     
    			case 3:
    				cAwnser = opAnd1 * opAnd2;
    				JOptionPane.showMessageDialog(null,(opAnd1 + " * " + opAnd2));
    				s1 = JOptionPane.showInputDialog("What is your awnser: ");
    				sAwnser = Integer.parseInt(s1);
    				if (cAwnser == sAwnser)
    					{
    					JOptionPane.showMessageDialog(null,"Terrific!");
    					correctA++;
    					}
    					else
    					{
    					JOptionPane.showMessageDialog(null,"So Close!");
    					}
    					break;
     
     
    			case 4:
    				cAwnser = opAnd1 / opAnd2;
    				JOptionPane.showMessageDialog(null,(opAnd1 + " / " + opAnd2));
    				s1 = JOptionPane.showInputDialog("What is your awnser: ");
    				sAwnser = Integer.parseInt(s1);
    				if (cAwnser == sAwnser)
    					{
    					JOptionPane.showMessageDialog(null,"Sensational!");
    					correctA++;
    					}
    				else
    					{
    					JOptionPane.showMessageDialog(null,"You can do better!");
    					}
    					break;
    				}
    			}
    	JOptionPane.showMessageDialog(null,("You got " + correctA + " awnsers!"));
     
     
    	s1 = JOptionPane.showInputDialog("Do you wish to play again: ");
    	tempBoo= Integer.parseInt(s1);
    	while (tempBoo < 0 || tempBoo > 3)
    		{
    		s1 = JOptionPane.showInputDialog("Invalid number, please use 1 or 2 and try again: ");
    		tempBoo = Integer.parseInt(s1);
    		}
    	switch (tempBoo)
    		{
    		case 1:
    	                     	pAgain = true;
    			break;
    		case 2:
    			pAgain = false;
    			break;
    				}
     
     
    		} while (pAgain = true);
     
     
     
    	JOptionPane.showMessageDialog(null,("Thank You For Playing"));
     
     
     
     
    	}
    }
    Last edited by melinko928; October 7th, 2011 at 08:59 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Math Game? not sure whats wrong?

    You want to use ==, not =, in your while statement. One checks for equality, the other assigns a value.

    Although, if you're working with a boolean, you can omit "== true" altogether.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Math Game? not sure whats wrong?

    Quote Originally Posted by KevinWorkman View Post
    You want to use ==, not =, in your while statement. One checks for equality, the other assigns a value.

    Although, if you're working with a boolean, you can omit "== true" altogether.
    im not sure where you are refering to... none of my while loops have an equals statement or assignment operator, there all greater than or less than, i have assignment operators in my switch statement at the end, is this what you are talking about?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Math Game? not sure whats wrong?

    do... while(). What is in that while statement?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: August 19th, 2011, 03:37 PM
  2. Whats wrong here?
    By Java Sucks in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 8th, 2011, 08:33 PM
  3. whats wrong with this one....
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 6th, 2009, 10:08 AM
  4. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM
  5. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM