1 Replies - 5437 Views - Last Post: 14 October 2010 - 08:15 PM Rate Topic: -----

#1 diotima  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-September 10

Simple addition and subtraction using random integers

Posted 14 October 2010 - 07:37 PM

I'm trying to make a program that gives the user simple addition and subtraction problems (#s 1-25; without negative answers) using randomly generated integers. I have made the structure of the program but I'm having problems figuring out how to state the actual math problems. Can anyone please help me out? I'm a nooblet.

Here is what I have so far:

import java.util.Scanner;
import java.util.Random;

public class MathGame

{
	public static void main(String [] args)

		Scanner scan = new Scanner (System.in);
		Random generator = new Random();
		
		char Addition = 'A';
		char Subtraction = 'S';
		char Exit = 'E';
		int n1 = (int)(25.0 * Math.random()) + 1;
		int n2 = (int)(25.0 * Math.random()) + 1;
		
	 int userSelectionCh; // variable to store the user's menu selection.
	 
	    do {
		    // display menu to the user
	    	System.out.println("\n\nARITHMETIC SKILLS BUILDER\n\n");
	        System.out.println("(A)ddition" );
	        System.out.println("(S)ubtraction" );
	        System.out.println("(E)xit" );
			System.out.print();
			
			// read and store the user's selection
	        System.out.print("Please enter your selection. Choose A, S, or E : " );
	        userSelectionCh = scan.nextInt();
			
			// process the user's choice
		    switch(userSelectionCh)
		    {
			    // If the user's selection is Addition 
	    		case Addition:
	    			System.out.print("Okay, you have chosen to solve an Addition problem.");
	    			System.out.print();
					System.out.print("Let me see...")
// here is where the addition problem should go?
	    		
	    			break;
			    // If the user's selection is Subtraction
	    		case Subtraction:
	    			System.out.print("Okay, you have chosen to solve a Subtraction problem.");
	    			System.out.print();
					System.out.print("Let me see...")
// here is where the subtraction problem should go? 					
	    			break;
				// If the user's selection is exit, then an exit message will print. 
				case Exit: 
				
					System.out.println("\nOkay, you have chosen to exit the menu.");
					System.out.print("Have a great day!");
					
				    break;
					
				// If the user's selection is invalid, then a message will notify them.
	    		default:
	    			System.out.println("\nInvalid Selection!\n");
			}		
					
					
}


Edited by Dogstopper: :code:

This post has been edited by Dogstopper: 14 October 2010 - 08:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Simple addition and subtraction using random integers

#2 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Simple addition and subtraction using random integers

Posted 14 October 2010 - 08:15 PM

userSelectionCh = scan.nextInt();


Is going to throw a InputMismatchException();
If you want to get A, S or E.
Use scanner.next() or scanner.nextLine();

If you want to show information to the user do it exactly how you have been use another System.out.println().

Something like
System.out.println("Add: "  n1 + " + " + n2);

Then get there input. Hint, you can use the same methods you have been using.

Take a look at:

http://download.orac...il/Scanner.html

That is what you have been using to get input in.

One last thing, please use " [ code ] " " [ /code]" tags, for all your source. Makes it easier to read.

This post has been edited by b0ng01: 14 October 2010 - 08:16 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1