简体   繁体   English

我将如何让用户从该列表中选择一个项目并使其完成功能,而不是一个个地解析所有项目?

[英]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.

相关问题 从输入数组中删除 item 的所有实例,而不仅仅是一个 - remove all instances of item from the input array rather than just one 如何使用循环来允许用户选择多个项目? - How do I use a loop to allow the user to select more than one item? 如何让用户只选择一个单选按钮? 以及我如何为每个人定价并让用户支付他选择的门票? - How can I let the user choose only one radiobutton? and how do I put a price for each one of them and let the user pay for the ticket he choosed? 如何从一个自定义 object 列表中创建一个 HashMap,它将 String 作为键,值将是另一个 HashMap? - How to create a HashMap that would have String as key and the value would be another HashMap from one list of custom object? 如何让用户通过双击从JOptionPane中选择一个项目 - How to let the user select an item from JOptionPane with a double-click 如何从下拉列表中选择所有列表选项循环通过它们选择每个选项 - How to select all list options from drop downlist loop through them select each option 为了让一个用户通过网站从其他用户计算机下载文件,我需要编码什么? - What would I need to code in order for one user to download files from another users computer, through a website? 我将如何更改此代码以允许在客户端和服务器之间发送多个用户输入消息 - How would i change this code to allow more than one user input message to be sent between the client and server 我如何让用户从图库中选择多个图像,并在选择它们时将它们按顺序放入队列中? - How can I have a user select multiple images from the gallery, and as they are selected have them put into a queue in order? 我如何一次从二维数组返回一个字符串并且在我使用整个列表之前没有任何重复输出? - How would I return one string at a time from a 2d array and not have any repeating ouputs until i have used the entire list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM