# Simple addition and subtraction using random integers

Page 1 of 1

## 1 Replies - 9485 Views - Last Post: 14 October 2010 - 08:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=195053&amp;s=8d9bf4b97b49bf3cd3dc6edf0c208e8e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 diotima

Reputation: 0
• 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 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
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:

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

Reputation: 38
• 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:

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

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }