繁体   English   中英

使用开关盒制作计算器

[英]Making a calculator using switch case

我得到的结果是:

Enter a number : 
5
Enter another number : 
4
What do you want to perform on these numbers? 
You have entered a wrong action, please try again 

我的代码在哪里出错?

import java.util.Scanner;

    public class App {

        public static void main(String[] args) {

            double num1, num2;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a number : ");
            num1 = sc.nextDouble();
            System.out.println("Enter another number : ");
            num2 = sc.nextDouble();

            System.out.println("What do you want to perform on these numbers? ");
            String word = sc.nextLine();

            sc.close();

            double result = 0;
            switch (word) {
            case "Addition":
                result = num1 + num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Subtraction":
                result = num1 - num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Multiplication":
                result = num1 * num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Division":
                result = num1 / num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            default:
                System.out.println("You have entered a wrong action, please try again ");
                break;

            }

        }

    }

您可以像下面那样更改代码:

        System.out.println("What do you want to perform on these numbers? ");
        sc.nextLine(); // ADD THIS LINE
        String word = sc.nextLine();

这里的问题是num2 = sc.nextDouble(); 不使用换行符。

下面是我使用的代码:

public static void main(String args[]) throws Exception {
      //  A1Pattern.printPattern(26);


        double num1, num2;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number : ");
        num1 = sc.nextDouble();
        System.out.println("Enter another number : ");
        num2 = sc.nextDouble();

        System.out.println("What do you want to perform on these numbers? ");
        sc.nextLine();
        String word = sc.nextLine();

        sc.close();

        double result = 0;
        switch (word) {
            case "Addition":
                result = num1 + num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Subtraction":
                result = num1 - num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Multiplication":
                result = num1 * num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Division":
                result = num1 / num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            default:
                System.out.println("You have entered a wrong action, please try again ");
                break;

        }

    }
  • 输出:

    输入一个数字:1输入另一个数字:2您想对这些数字执行什么操作? 减法1.0减法2.0:-1.0

代替在第15行上使用sc.nextline() ,而使用sc.nextline() sc.next() 程序现在将等待您的输入,然后继续。

java.util.Scanner.next()方法从此扫描器中查找并返回下一个完整的令牌。

使用scanner.next()而不是scanner.nextLine()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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