简体   繁体   中英

How would I let the user select an Item from this list and have it complete the function rather than parsing through all of them one by one?

I have implemented a calculator, however, I only want the user to be able to pick two options then close the program. My code is having the user cycle through each operation. I would like the user to pick a number 1 through 6 and have it complete the operation the selected. Also if someone knows how to get the program to exit if they press 0 at the menu that would be fantastic.

import java.util.*;
public class Calculator
{
 private int option = -1; // option is initially not 0 
 to 6
 private Scanner scan; // we’ll use scan to read input

 // constructor for class
 public Calculator()
 {
 System.out.println ("java Homework1");
 System.out.println ("Welcome to Math Calculator!");
 System.out.println ("Please choose an option:");
 System.out.println (" ");
 System.out.println ("1 - add two real numbers");
 System.out.println ("2 - subtract two real numbers");
 System.out.println ("3 - multiply two real numbers");
 System.out.println ("4 - divide two real numbers");
 System.out.println ("5 - get the factorial of an 
 number");
 System.out.println ("6 - menu");
 System.out.println ("0 - exit");
 scan = new Scanner(System.in); // creates scan
 }

 // entry point for class
 public void run()
 {
 // stick code for calculator in here...may want to 
 create
 // other functions to make code more readable
 int selection1;
 Scanner first = new Scanner(System.in);
 selection1 = first.nextInt();
 if (selection1 == 1);
 {
    System.out.println ("Enter 1st value: ");
    int firstnum = scan.nextInt();
    System.out.println ("Enter 2nd value: ");
    int secondnum = scan.nextInt();
    System.out.println ("your answer is: " + (firstnum 
    + secondnum));
 }
 if (selection1 == 2);
 {
    System.out.println ("Enter 1st value: ");
    int firstnum = scan.nextInt();
    System.out.println ("Enter 2nd value: ");
    int secondnum = scan.nextInt();
    System.out.println ("your answer is: " + (firstnum 
    - secondnum));
 }
 if (selection1 == 3);
 {
    System.out.println ("Enter 1st value: ");
    int firstnum = scan.nextInt();
    System.out.println ("Enter 2nd value: ");
    int secondnum = scan.nextInt();
    System.out.println ("your answer is: " + (firstnum 
    * secondnum));
 }
 if (selection1 == 4);
 {
    System.out.println ("Enter 1st value: ");
    int firstnum = scan.nextInt();
    System.out.println ("Enter 2nd value: ");
    int secondnum = scan.nextInt();
    System.out.println ("your answer is: " + (firstnum 
    / secondnum));
 }
 if (selection1 == 5);
    System.out.println ("Enter the number you would 
    like the factorial of: ");
    int factorialnum = scan.nextInt();
    int i,start = 1;
    for (i = 1; i <= factorialnum;i++)
    {    
      start = start*i;    
    }       
    System.out.println ("your answer is: " + start);    
}
}

The problem is with the if conditions. The following if condition is not correct. It will have no effect on the section following it, since it is ended with the semicolon (;)

if (selection1 == 1); //This condition will have no effect on the section following it

This is the correct way to write the if condition.

if (selection1 == 1){
//your logic here
}

Also, note the last if condition for factorial. It does not have braces for the code following it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM