Need to create simple calculator using do while loop & if, else if

Page 1 of 1

5 Replies - 34048 Views - Last Post: 30 June 2012 - 11:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=284383&amp;s=fd799d34d69d97dd019be0f99612da1a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 jillmasonmarquez

Reputation: 0
• Posts: 14
• Joined: 30-June 12

Need to create simple calculator using do while loop & if, else if

Posted 30 June 2012 - 09:36 AM

This does happen to be my homework problem, however i dont want anyone to complete it for me i just need help understanding what i should be doing. The language is Java. The assignment states that i need to create a menu to add subtract multiply divide then quit utilizing a do while loop, then perform those operations with an if.. else if.. else statement.
I do have some code however when compiled it has 20+ error messages. I am brand new to programming and am at a loss.

```import java.util.Scanner;
public class SimpleCalculator{
public static void main(String[] args){

do
{
System.out.println("Pick two integers and enter them here: ");
Scanner keyboard = new Scanner(System.in);
String x, y;
x = keyboard.next();
y = keyboard.next();
System.out.println("2: Subtract");
System.out.println("3: Multiply");
System.out.println("4: Divide");
System.out.println("0: Quit");
Double one, two, three, four, zero;
one = x + y;
two = x - y;
three = x * y;
four = x / y;
zero = quit;
selection = 0;
next = keyboard.nextInt();
while (selection <= 1)
{
selection = selection + next;
selection++;
next = keyboard.nextInt();
}

if (selection == 1)
{
System.out.println(x + y);
}
else if (selection == 2)
{
System.out.println(x - y);
}
else if (selection == 3)
{
System.out.println(x * y);
}
else if (selection == 4)
{
System.out.println(x / y);
}
else
{
System.out.println("0: Quit");
System.exit(0);
}
}while (system.exit(0));
}
}

```

I would rather have an explanation than the written code, thank you so much.

This post has been edited by pbl: 30 June 2012 - 11:20 AM
Reason for edit:: Fixed code tags

Is This A Good Question/Topic? 0

Replies To: Need to create simple calculator using do while loop & if, else if

#2 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

Re: Need to create simple calculator using do while loop & if, else if

Posted 30 June 2012 - 11:24 AM

Guessing at what you're seeing, here's a start:

The program asks the user to enter two integers, but the inputs are stored in the two String variables, x and y. The program then tries to do the required calculations, storing the results in the five Double variables, zero through four.

All of the problems to this point are due to attempting to do operations with variables that can't be done. For example, two Strings can't be added together and stored in a Double variable. Two Strings can't be subtracted, multiplied, or divided.

Three new variables are then introduced without being declared: quit, selection, and next. Variables must be declared and initialized before they can be used.

See if you can fix those errors, and come back if/when you're ready for more help.

#3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Need to create simple calculator using do while loop & if, else if

Posted 30 June 2012 - 11:25 AM

```      String x, y;
x = keyboard.next();
y = keyboard.next();
System.out.println("2: Subtract");
System.out.println("3: Multiply");
System.out.println("4: Divide");
System.out.println("0: Quit");
Double one, two, three, four, zero;
one = x + y;    // one is double x and y are String you cannot  double = String + String
two = x - y;    // you cannot String - String
three = x * y;  // you cannot String * String
four = x / y;   // you cannot String / String
zero = quit;    // quit is not defined
selection = 0;  // selection is not defined
next = keyboard.nextInt();  // next is not defined
while (selection <= 1)
{
selection = selection + next;
selection++;
next = keyboard.nextInt();
}
// again you cannot perform any operation but "+" between String
// and selection is not declared
if (selection == 1)
{
System.out.println(x + y);
}

```

#4 jillmasonmarquez

Reputation: 0
• Posts: 14
• Joined: 30-June 12

Re: Need to create simple calculator using do while loop & if, else if

Posted 30 June 2012 - 11:36 AM

I reconfigured my program:
```import java.util.Scanner;
public class SimpleCalculator
{
public static void main(String[] args){
int num1, num2, choice;
boolean check = false;
Scanner keyboard = new Scanner(System.in);
do{

System.out.println("Enter two integers: ");
//Scanner keyboard = new Scanner(system.in);
num1 = keyboard.nextInt();
num2 = keyboard.nextInt();

System.out.println("2: Subtract");
System.out.println("3: Multiply");
System.out.println("4: Divide");
System.out.println("0: Quit");

System.out.println("Enter Choice: ");

choice = keyboard.nextInt();

if (choice == 1){
System.out.println(num1 + num2);
}
else if (choice == 2){
System.out.println(num1 - num2);
}
else if (choice == 3){
System.out.println(num1 * num2);
}
else if (choice == 4){
System.out.println(num1 / num2);
}
else if (choice == 0){
System.out.println("This way to the exit.");
System.exit(0);

}
else{
System.out.println("Error, entered wrong number.");
}
while (!check);
System.out.print("Finished.");
}while (choice >= 5);
}
}

```

But know i have created an infinite loop

now*

#5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12269
• Posts: 45,363
• Joined: 27-December 08

Re: Need to create simple calculator using do while loop & if, else if

Posted 30 June 2012 - 11:42 AM

You don't set check to true anywhere in your loop. Hence, !check always evaluates to true, so the loop keeps repeating.

#6 jillmasonmarquez

Reputation: 0
• Posts: 14
• Joined: 30-June 12

Re: Need to create simple calculator using do while loop & if, else if

Posted 30 June 2012 - 11:50 AM

Thank you all so much.