java - 问题与double,当用户输入字母/符号时,程序无法运行

时间:2015-11-16 17:17:05

标签: java

我是初学者,我正在尝试使用双变量制作一个简单的计算器程序,以便用户可以添加intdouble个数字进行计算。但只要用户输入String,程序就会失败。我已经尝试了很多来解决这个简单的问题。我希望有人可以帮助我。

请输入第一个号码:所以当用户输入25 / 25.5时一切正常,但当用户输入e / f / k等字母时,程序失败。我在&#34之后尝试了以下代码;请输入第一个数字":

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

以下是实际程序,没有上述代码:

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");
    }

  }
}

1 个答案:

答案 0 :(得分:5)

official documentation上,当下一个令牌(用户输入)与浮点数不匹配时,会提到.nextDouble() 抛出一个InputMismatchException(例如当用户输入一个字母时)。您可以使用 try catch 块来处理此类异常:

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

您可以阅读有关捕获和处理异常的更多信息here