简体   繁体   English

如何使小数输入有效?

[英]How can you make a decimal input valid?

I have written this code, however, every time I input a decimal value, it doesn't work. 我已经编写了这段代码,但是,每当我输入一个十进制值时,它都不起作用。 How can I make this code work even if I input a decimal value? 即使输入十进制值,如何使此代码正常工作? For example, if I input a value of 7.5, it should display that "the shipping cost is $9.45" 例如,如果我输入值7.5,则它应显示“运输成本为$ 9.45”

    import java.util.Scanner;

public class IfElse {
  public static void main(String[] args) {
    int marksObtained;

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter a package weight in pounds:");

    marksObtained = input.nextInt();

    if (marksObtained>20)
        {
            System.out.println("The package is too heavy to be shipped");
        }
        else if (marksObtained>10)
        {
            System.out.println("The shipping cost is $12.50");
        }
            else if (marksObtained>3)
        {
            System.out.println("The shipping cost is $9.45");
        }
            else if (marksObtained>1)
        {
            System.out.println("The shipping cost is $4.95");
        }
            else if (marksObtained>0)
        {
            System.out.println("The shipping cost is $2.95");
        }
        else if (marksObtained<0)
        {
            System.out.println("The weight must be greater than zero");
        }
  }
}

You can use nextFloat or nextDouble 您可以使用nextFloatnextDouble

Scanner s = new Scanner (System.in);
float a = s.nextFloat ();
System.out.println(a);

Using nextInt will expect an int value to be entered and will throw a java.util.InputMismatchException if a int is not entered 使用nextInt将期望输入一个int值,如果未输入一个int则将抛出java.util.InputMismatchException

Look at the code that you use to read input: 查看用于读取输入的代码:

int marksObtained;`enter code here`
marksObtained = input.nextInt();

The key here is to understand that an int can only represent whole number values, not decimals. 这里的关键是要理解一个int只能代表整数值,不能代表小数。 For decimals, you need to use a double or a float. 对于小数,您需要使用双精度或浮点型。 For example: 例如:

double marksObtained = input.nextDouble();

I suggest you go back and review the basic data types which Java supports. 我建议您返回并回顾Java支持的基本数据类型。 You should also familiarize yourself with the documentation for the Scanner class as well as the rest of the documentation for the standard Java APIs. 您还应该熟悉Scanner类的文档以及标准Java API的其余文档。

nextInt() only works for integers. nextInt()仅适用于整数。 Use nextDouble() 使用nextDouble()

Use nextDouble method as below 使用nextDouble方法,如下所示

public static void main(String[] args) {
    double marksObtained;

    System.out.println("Please enter a package weight in pounds:");
    Scanner input = new Scanner(System.in);
    marksObtained = input.nextDouble();
    input.close();

    if (marksObtained > 20) {
        System.out.println("The package is too heavy to be shipped");
    } else if (marksObtained > 10) {
        System.out.println("The shipping cost is $12.50");
    } else if (marksObtained > 3) {
        System.out.println("The shipping cost is $9.45");
    } else if (marksObtained > 1) {
        System.out.println("The shipping cost is $4.95");
    } else if (marksObtained > 0) {
        System.out.println("The shipping cost is $2.95");
    } else if (marksObtained < 0) {
        System.out.println("The weight must be greater than zero");
    }
}

And close the scanner, it is good practice doing it. 关闭扫描仪,这是个好习惯。

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

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