5 Replies - 9382 Views - Last Post: 30 June 2012 - 11:50 AM Rate Topic: -----

#1 jillmasonmarquez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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("1: Addition");
      System.out.println("2: Subtract");
      System.out.println("3: Multiply");
      System.out.println("4: Divide");
      System.out.println("0: Quit");
      System.out.println("Your Selection");
      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.

*Edited: please :code:

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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

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

Posted 30 June 2012 - 11:24 AM

Please post the errors when you're asking for help with them.

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.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • 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("1: Addition");
      System.out.println("2: Subtract");
      System.out.println("3: Multiply");
      System.out.println("4: Divide");
      System.out.println("0: Quit");
      System.out.println("Your Selection");
      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);
      }


Was This Post Helpful? 0
  • +
  • -

#4 jillmasonmarquez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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("1: Addition");
       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*

Edited by macosxnerd101: Please, :code:.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,635
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 jillmasonmarquez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1