Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/42.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_Intermediate Code - Fatal编程技术网

Java 如何使十进制输入有效?

Java 如何使十进制输入有效?,java,intermediate-code,Java,Intermediate Code,我已经写了这段代码,但是,每次我输入一个十进制值时,它都不起作用。即使我输入了一个十进制值,我如何使这段代码工作?例如,如果我输入值7.5,它应该显示“运费为9.45美元” import java.util.Scanner; 公共类IfElse{ 公共静态void main(字符串[]args){ int标记已保存; 扫描仪输入=新扫描仪(System.in); System.out.println(“请以磅为单位输入包装重量:”; marksobatined=input.nextInt();

我已经写了这段代码,但是,每次我输入一个十进制值时,它都不起作用。即使我输入了一个十进制值,我如何使这段代码工作?例如,如果我输入值7.5,它应该显示“运费为9.45美元”

import java.util.Scanner;
公共类IfElse{
公共静态void main(字符串[]args){
int标记已保存;
扫描仪输入=新扫描仪(System.in);
System.out.println(“请以磅为单位输入包装重量:”;
marksobatined=input.nextInt();
如果(标记保持>20)
{
System.out.println(“包装太重,无法装运”);
}
否则如果(标记保持>10)
{
System.out.println(“运费为12.50美元”);
}
否则,如果(标记保持>3)
{
System.out.println(“运费为9.45美元”);
}
否则如果(标记保持>1)
{
System.out.println(“运费为4.95美元”);
}
else if(标记保持>0)
{
System.out.println(“运费为2.95美元”);
}

else if(marks获得您可以使用
nextFloat
nextDouble

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

如果未输入
int

您可以使用
nextFloat
nextDouble

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

使用
nextInt
将期望输入一个int值,如果未输入
int
,将抛出一个
java.util.InputMismatchException
,查看用于读取输入的代码:

int marksObtained;`enter code here`
marksObtained = input.nextInt();
这里的关键是要理解,
int
只能表示整数值,不能表示小数。对于小数,需要使用双精度或浮点。例如:

double marksObtained = input.nextDouble();

我建议您回顾一下Java支持的基本数据类型。您还应该熟悉标准Java API的其他文档。

查看用于读取输入的代码:

int marksObtained;`enter code here`
marksObtained = input.nextInt();
这里的关键是要理解,
int
只能表示整数值,不能表示小数。对于小数,需要使用双精度或浮点。例如:

double marksObtained = input.nextDouble();

我建议您回顾一下Java支持的基本数据类型。您还应该熟悉标准Java API的其他文档。

nextout()
仅适用于整数。使用
nextDouble()
nextDouble()
仅适用于整数。使用
nextDouble()

使用
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");
    }
}
publicstaticvoidmain(字符串[]args){
双标记;
System.out.println(“请以磅为单位输入包装重量:”;
扫描仪输入=新扫描仪(System.in);
marksobatined=input.nextDouble();
input.close();
如果(标记保持>20){
System.out.println(“包装太重,无法装运”);
}否则如果(标记保持>10){
System.out.println(“运费为12.50美元”);
}否则,如果(标记保持>3){
System.out.println(“运费为9.45美元”);
}否则如果(标记保持>1){
System.out.println(“运费为4.95美元”);
}else if(标记保持>0){
System.out.println(“运费为2.95美元”);
}else if(标记保持<0){
System.out.println(“重量必须大于零”);
}
}

然后关闭扫描仪,这是一个很好的做法。

使用
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");
    }
}
publicstaticvoidmain(字符串[]args){
双标记;
System.out.println(“请以磅为单位输入包装重量:”;
扫描仪输入=新扫描仪(System.in);
marksobatined=input.nextDouble();
input.close();
如果(标记保持>20){
System.out.println(“包装太重,无法装运”);
}否则如果(标记保持>10){
System.out.println(“运费为12.50美元”);
}否则,如果(标记保持>3){
System.out.println(“运费为9.45美元”);
}否则如果(标记保持>1){
System.out.println(“运费为4.95美元”);
}else if(标记保持>0){
System.out.println(“运费为2.95美元”);
}else if(标记保持<0){
System.out.println(“重量必须大于零”);
}
}

关闭扫描仪,这是一个很好的做法。

提示:
nextInt()
获取浮点/双精度?如果您想读取十进制,请使用
nextDouble()
,并检查其他条件是否已对其排序。请向上投票并接受以下答案:
nextInt()
要获得浮点/双精度?如果您想读取十进制,请使用
nextDouble()
,并检查else if conditions是否已排序。请向上投票并接受下面的答案