[英]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.
我希望用户选择一个1到6的数字,并使其完成所选的操作。 Also if someone knows how to get the program to exit if they press 0 at the menu that would be fantastic.
另外,如果有人知道如何在菜单上按0退出程序,那将是很棒的选择。
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. 问题在于if条件。 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条件的正确方法。
if (selection1 == 1){
//your logic here
}
Also, note the last if condition for factorial. 此外,请注意阶乘的最后一个if条件。 It does not have braces for the code following it.
它后面的代码没有花括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.