Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 输入不匹配异常未正确使用的问题_Java - Fatal编程技术网

Java 输入不匹配异常未正确使用的问题

Java 输入不匹配异常未正确使用的问题,java,Java,我试图创建一个代码,在这里我读取一个double并打印出它的平方,但我也希望它知道用户何时输入一个负数或非double常量,并让他们输入一个新的数字。我有输入不匹配异常的问题。我的代码不能正常工作,它可以编译,但编译器只能永远运行。任何建议都会有帮助 import java.util.*; class constants { public static void main(String[] args) { double constant = getConstant(); Sys

我试图创建一个代码,在这里我读取一个double并打印出它的平方,但我也希望它知道用户何时输入一个负数或非double常量,并让他们输入一个新的数字。我有输入不匹配异常的问题。我的代码不能正常工作,它可以编译,但编译器只能永远运行。任何建议都会有帮助

import java.util.*;
class constants 
{
public static void main(String[] args)
{
    double constant = getConstant();
    System.out.println("Square of " + constant + " = " + constant*constant);
}
//-------------------------------------------------------------------------------
public static double getConstant()
{
Scanner kb = new Scanner(System.in);

System.out.println("Enter non-negative double constant");

double constant = kb.nextDouble();
try { 
        double selection = kb.nextDouble();
    }


    catch (InputMismatchException e) // where there is error. 
        {
            System.out.println("Not a double constant. Re-enter");

    }
    return constant;
  }

}

我知道您希望用户输入诸如15.65145.95等值,但应拒绝-5.85(负值)和11(整数值)。事实上,在java中,任何整数都是双精度的

示例:

double x = 100;   // is correct
double y = -15.85 // is correct
因此,它们不会生成输入不匹配异常。为此,您必须明确地检查这些条件是否满足,并且还必须明确地抛出InputMismatchException

最好只定义一次scanner,例如作为全局静态变量(否则,如果在循环中使用call getConstant(),可能会遇到问题)

您不需要定义选择的双精度值。这是一个有效的例子

import java.util.InputMismatchException;
import java.util.Scanner;

class Constants 
{
    private static Scanner kb = new Scanner(System.in);
    public static void main(String[] args)
    {
        double constant = getConstant();
        if (constant >= 0) {
            System.out.println("Square of " + constant + " = " + constant*constant);
        }
    }
    //-------------------------------------------------------------------------------
    public static double getConstant()
    {

        System.out.println("Enter non-negative double constant");

        double constant=-1.0D;
        try { 
            constant = kb.nextDouble();
            // you don't want the negative value neither the integer value
            // so you reject them by throwing the InputMismatchException
            if (constant <0 || Math.floor(constant) == constant * 1.0D) {
                constant = -1.0D;
                throw new InputMismatchException("Not a double constant. Re-enter");

            }
        }

        catch (InputMismatchException e) // where there is error. 
            {
                System.out.println("Not a double constant. Re-enter");
        }
        return constant;
      }
}
import java.util.InputMismatchException;
导入java.util.Scanner;
类常数
{
专用静态扫描仪kb=新扫描仪(System.in);
公共静态void main(字符串[]args)
{
双常数=getConstant();
如果(常数>=0){
System.out.println(““+常数+”=“+常数*常数的平方”);
}
}
//-------------------------------------------------------------------------------
公共静态双getConstant()
{
System.out.println(“输入非负双常数”);
双常数=-1.0D;
试试{
常数=kb.nextDouble();
//您既不需要负值,也不需要整数值
//因此,您可以通过抛出InputMismatchException来拒绝它们

如果(constant这里是如何实现的,那么你需要捕捉的例外是NumberFormatException。尽管如此,负数仍然可以有平方,但它们不能有平方根

 public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
      try { 
        double temp = kb.nextDouble();
        //If the input is not a double, catch the number format exception
      } catch (NumberFormatException e) {
        e.printStackTrace();
      }
      //If the number is in proper format, (can be negative) print its square.
         System.out.println("Square of " + temp+ " = " + temp*temp);
   }
出于某种原因,如果您不想打印负数的平方,只需在打印结果之前检查该条件即可