简体   繁体   English

当用户可以输入int和double时添加两个数字

[英]Adding two numbers when the user can enter an int and double

I need the program to run when a user enter for example 4 and 6.5 or vice versa. 我需要程序在用户输入例如4和6.5时运行,反之亦然。 i have the if else running only when its either or and when it tries to run it for both at the same time i get a error code like this : 我有if else只在其中的任何一个或当它试图同时运行它时,我得到一个像这样的错误代码:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at class2.AddTwoNumbers.main(AddTwoNumbers.java:28)

Any assistance would be greatly appreciated 任何帮助将不胜感激

    public class AddTwoNumbers {

        public static void main(String[] args) {
    Scanner inputSource = new Scanner(System.in);
    Scanner runAgain = new Scanner(System.in);
    int input1, input2, result;
    double input3, input4, result2;

    String answer = null;

    do {

         System.out.println("Please enter two numbers: ");
    if(inputSource.hasNextInt()) {

      input1 = inputSource.nextInt();
      input2 = inputSource.nextInt();


      result = input1 + input2;
      System.out.println("The sum of " + input1 + " and " + input2 + " is " + result);

       } else if (!inputSource.hasNextInt()){

              input3 = inputSource.nextDouble();
              input4 = inputSource.nextDouble();

              result2 = input3 + input4;
              System.out.println("The sum of " + input3 + " and " + input4 + " is " + result2);

              } else if (inputSource.hasNextInt() ||   inputSource.hasNextDouble()){
                  input1 = inputSource.nextInt();
                  input3 = inputSource.nextDouble();

                  result2 = input1 + input3;
                  System.out.println("The sum of " + input1 + " and " + input3 + " is " + result2);
    }           
    System.out.println("Do you want to run this again? Enter Y for Yes or N for No: ");
             answer = runAgain.next();


           } while (answer != "N");

           inputSource.close();     
     }

}

In such case, it's makes more sense to read the inputs with double number = Double.parseDouble(inputSource.nextLine()); 在这种情况下,使用double number = Double.parseDouble(inputSource.nextLine());读取输入更有意义double number = Double.parseDouble(inputSource.nextLine()); . This would work for both int and double inputs. 这适用于intdouble输入。

Your code becomes as simple as : 您的代码变得如此简单:

double input1 = Double.parseDouble(inputSource.nextLine());
double input2 = Double.parseDouble(inputSource.nextLine());
double result = input1 + input2;

Beside that, change while (answer != "N"); 除此之外,改变while (answer != "N"); to while (!answer.equals("N")); to while (!answer.equals("N")); .

use nextLine() method instead of nextInt() and parse it into double which will work for both int and double type 使用nextLine()方法而不是nextInt()并将其解析为double,这将适用于intdouble类型

double result;
double input1=Double.parseDouble(inputSource.nextLine());
double input2=Double.parseDouble(inputSource.nextLine());
result=input1+input2;

and also change while (answer != "N"); 并且同时改变while (answer != "N"); with while (!answer.equals("N"));// since your are comparing string not character with while (!answer.equals("N"));// since your are comparing string not character

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

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