使用户输入浮点值可以在打印时保持相同的格式

时间:2016-07-28 02:29:44

标签: java

我使用nextLine读取用户的输入并按空格分割。

将用户的输入视为浮动。

但输出与原始用户输入不同。

我该如何解决?

输出

Enter 2 floats divided by a space12.31 2213.12
you entered 12.310000 and 2213.120117 successfully
Process finished with exit code 0

预期输出

Enter 2 floats divided by a space12.31 2213.12
you entered 12.31 and 2213.12 successfully
Process finished with exit code 0

我的源代码

private static float[] getTwoFloats(){
    Scanner rd = new Scanner(System.in);
    while (true){
        System.out.print("Enter 2 floats divided by a space");
        try{
            String toks[] = rd.nextLine().split(" ");
            if(toks.length==2){
                float[] rtnFloats = new float[2];
                for(int i=0; i < 2; i++){
                    rtnFloats[i] = Float.valueOf(toks[i]);
                }
                return rtnFloats;
            }

        }catch (Exception e){

        }

    }

}

public static void main(String[] args) {
    float[] twoFloats = getTwoFloats();
    System.out.printf("you entered %f and %f successfully", twoFloats[0], twoFloats[1]);

}

2 个答案:

答案 0 :(得分:-1)

您应该在使用时转换数据类型,打印输出时,只需将输入存储为字符串。

private static String[] getTwoFloats() {
    Scanner rd = new Scanner(System.in);
    while (true) {
        System.out.print("Enter 2 floats divided by a space");
        try {
            String toks[] = rd.nextLine().split(" ");
            return toks;
        } catch (Exception e) {
        }
    }
}

public static void main(String[] args) {
    String[] twoFloats = getTwoFloats();
    System.out.printf("you entered %s and %s successfully", twoFloats[0], twoFloats[1]);
}

<强>输出:

Enter 2 floats divided by a space55.248 5465.5645
you entered 55.248 and 5465.5645 successfully

<强> ================= UPDATE ===================== < / p>

对于@HarshilSharma的担忧,我们可以按照以下方式进行。

public static class FloatObj {
    private float value; // Store original string for printing
    private String str; // Store value for calculation

    public float getValue() {
        return value;
    }

    public void setValue(float value) {
        this.value = value;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }
}

private static FloatObj[] getTwoFloats() {
    Scanner rd = new Scanner(System.in);
    while (true) {
        System.out.print("Enter 2 floats divided by a space");
        try {
            String toks[] = rd.nextLine().split(" ");
            if (toks.length == 2) {
                FloatObj[] result = new FloatObj[2];
                for (int i = 0; i < 2; i++) {
                    FloatObj floatObj = new FloatObj();
                    floatObj.setStr(toks[i]);
                    floatObj.setValue(Float.valueOf(toks[i]));
                    result[i] = floatObj;
                }
                return result;
            }
        } catch (Exception e) {
        }
    }
}

public static void main(String[] args) {
    FloatObj[] results = getTwoFloats();
    System.out.printf("you entered %s and %s successfully", results[0].getStr(), results[1].getStr());
}

答案 1 :(得分:-1)

使用System.out.println代替System.out.printf

System.out.println("you entered " + twoFloats[0] + " and " + twoFloats[1] + " successfully");

我不确定为什么这确实起作用,两者之间的区别是什么,但我会调查。也许有更好理解的人可以解释一下?