Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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 当double作为int输入时,尝试输出不带小数的double_Java_Int_Double - Fatal编程技术网

Java 当double作为int输入时,尝试输出不带小数的double

Java 当double作为int输入时,尝试输出不带小数的double,java,int,double,Java,Int,Double,我试图打印出一个整数每当一个整数是我的双输入。e、 g.用户输入123456,输出123456;用户输入1.0,输出1.0。到目前为止,我的代码打印了一个双精度。这是我的fullCellText方法 我的代码: package textExcel; public class ValueCell extends RealCell { private boolean isInt; public ValueCell(String cell) { super(cell

我试图打印出一个整数每当一个整数是我的双输入。e、 g.用户输入123456,输出123456;用户输入1.0,输出1.0。到目前为止,我的代码打印了一个双精度。这是我的fullCellText方法

我的代码:

package textExcel;

public class ValueCell extends RealCell {
    private boolean isInt;
    public ValueCell(String cell) { 
        super(cell);
        // TODO Auto-generated constructor stub
    }
    public ValueCell(String cell, boolean isInt) { 
        super(cell);
        this.isInt = isInt;
        // TODO Auto-generated constructor stub
    }
    public String fullCellText() {
        return "" + cell;
    }

}

不确定我是否正确理解了您的问题,但我认为您正在尝试打印一个没有小数点的双精度

您可以通过如下操作将双精度值转换为int值:
intx=(int)y
这里的
y
是双精度值。现在,如果打印
x
,则不会得到任何小数位数


注意:int-typecasting不是一个好主意,因为您的double可能超出范围。

我建议您在输入字符串中进行点检查

if (yourString.contains("."))
这不是解决这个问题的最佳方法,但它确实有效

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String buff = scanner.nextLine();
        if (buff.contains(".")){
            double tempDouble = Double.parseDouble(buff);
            System.out.println(tempDouble);
        } else {
            int integer = Integer.parseInt(buff);
            System.out.println(integer);
        }
    }
可能重复的