简体   繁体   English

java-出现双精度问题,当用户输入字母/符号时,程序无法运行

[英]java - issue with double, when user enter letter/signs, the program fails to run

I am a beginner and I am trying to make a simple calculator program using double variables, so that it allows users to add both int and double numbers to calculate. 我是一个初学者,我正在尝试使用双变量制作一个简单的计算器程序,以便允许用户同时添加intdouble数来进行计算。 But whenever the user enters a String , the program fails. 但是,只要用户输入String ,程序就会失败。 I have tried a lot to resolve this simple issue. 我已经做了很多尝试来解决这个简单的问题。 I hope someone can help me. 我希望有一个人可以帮助我。

Please enter first number: So when the user enters 25/25.5 everything works, but when user enters a letter such as e/f/k etc, the program fails. 请输入第一个数字:因此,当用户输入25 / 25.5时,一切正常,但是当用户输入字母(例如e / f / k等)时,程序将失败。 I have tried the following code after "Please enter first number": 我在“请输入第一个数字”之后尝试了以下代码:

if (user1 == aswin.nextDouble())
{
    System.out.println("Continue");
}
else (user1 == Double.parseDouble(str))
{
    System.out.println("Invalid Entry - Please enter Only numbers");
}

Following are the actual program, without the above code: 下面是实际的程序,没有上面的代码:

import java.util.Scanner;
import java.text.NumberFormat;

public class NewTwo_Calculator_IF_Statement {


  public static void main (String [] args)
  {

    Scanner aswin = new Scanner (System.in);

    double user1 = 0, user2 = 0, mult, div, sub, ad;
    char user3;
    boolean run = true;
    String user;


    System.out.println ("Welcome to ASWINS calculator" + '\n');

    System.out.print ("Please enter First number - ");
    user1 = aswin.nextDouble();

    System.out.print("Please Enter Second number - ");
    user2 = aswin.nextDouble();

    System.out.println ('\n' + "Please choose following options" + '\n');
    System.out.println ("Type either A/a/+  for Addition ");
    System.out.println ("Type either S/s/-  for Substraction");
    System.out.println ("Type either D/d or /  for Division ");
    System.out.println ("Type M/m/*  for Multiplication");
    user3 = aswin.next().charAt(0);

    mult = user1*user2;
    ad = user1+user2;
    sub = user1-user2;
    div = user1/user2;

    if (user3 == ('*') || user3 == ('m') || user3 == ('M')) 
    {
        System.out.println('\t' + "Multiplication of " + user1 + " and " +user2 + " is = " + mult + '\n');
    } else if (user3 == ('+')  || user3 == ('a') || user3 == ('A'))
    {
        System.out.println ('\t' + "Addition of " + user1 + " and " +user2 + " is = " + ad + '\n');
    } else if(user3 == ('-')  || user3 == ('s') || user3 == ('S'))
    {
        System.out.println ('\t' + "Substraction of " + user1 + " and " +user2 + " is = " + sub + '\n');
    } else if (user3 == ('/')  || user3 == ('d') || user3 == ('D'))
    {
        System.out.println ('\t' + "Division of " + user1 + " and " +user2 + " is = " + div + '\n');
    } else
    {
        System.out.println('\t' + "Invalid Input");
    }       

    System.out.println ("Please Enter 'e' or type 'exit' to exit the program OR type anything else to stay on the page" + '\n');
    user = aswin.next();

    if (user.equalsIgnoreCase("e") || (user.equalsIgnoreCase("exit")))
    {
        System.out.println("Thanks for Entering -> " + user + " <-");
        System.out.println ("You QUIT the page, Thanks and Hope you will use my(Aswin's) Calculator again");
        System.exit(0);
    }
    else 
    {
        System.out.println("Thanks for Entering -> " + user + " <-");
        System.out.println (" You are STAYING on the page, Thanks " + '\n');
        System.out.println  ( "Thank you so much for using my calculator");
    }

  }
}

On the official documentation , it is mentioned that .nextDouble() throws a InputMismatchException when the next token (user input) does not match a float (such as when the user inputs a letter). 官方文档中 ,提到了.nextDouble()一个标记(用户输入)与浮点数不匹配时(例如,当用户输入字母时) .nextDouble() 引发 InputMismatchException You can handle such exception by using a try catch block: 您可以使用try catch块来处理此类异常:

try{
    user1 == aswin.nextDouble()
}catch(InputMismatchException e){
    // User input is invalid
    System.out.println("Invalid Entry - Please enter Only numbers");
}

You can read more about catching and handling exceptions here . 您可以在此处阅读有关捕获和处理异常的更多信息。

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

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