输入数字后显示单词

时间:2013-11-30 10:33:55

标签: java

我有一个测试问题,在那里我被要求编写一个简单的程序,读取2个整数(即小数点前一个和后面一个),它用于显示数字是否应该是向上舍入与否。

例如:

Enter number between 1 and 5: 3
Enter number between 3 and 6: 5
3.5 was rounded up, it's now four

除了整理部分外,我可以使其中的大部分工作。请帮帮我。

下面是我的代码和我得到的输出

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

public class Test {

    public static void main(String[] args) {
        Scanner in = new Scanner(System. in );
        System.out.print("Enter number between 1 and 5: ");
        int num1 = in .nextInt();
        System.out.print("Enter number between 3 and 6: ");
        int num2 = in .nextInt();

        System.out.print(EnglishName(num1, num2));
    }

    public static String EnglishName(int num1, int num2) {

        String numberInWords = "";
        String numberAndDecimal = "";

        if (num1 == 1) {
            numberInWords = "one";
        }
        if (num1 == 2) {
            numberInWords = "two";
        }
        if (num1 == 3) {
            numberInWords = "three";
        }
        if (num1 == 4) {
            numberInWords = "four";
        }
        if (num1 == 5) {
            numberInWords = "five";
        }

        if (num2 == 3) {
            numberAndDecimal = num1 + "." + num2 + " not rounded up its still " + numberInWords;
        }
        if (num2 == 4) {
            numberAndDecimal = num1 + "." + num2 + " not rounded up its still " + numberInWords;
        }
        if (num2 == 5) {
            numberAndDecimal = num1 + "." + num2 + " was rounded up, it's now " + (numberInWords + 1);
        }
        if (num2 == 6) {
            numberAndDecimal = num1 + "." + num2 + " was rounded up, it's now " + (numberInWords + 1);
        }

        return numberAndDecimal;
    }
}

输出:

Enter number between 1 and 5: 1
Enter number between 3 and 6: 3    
1.3 not rounded up its still one

Enter number between 1 and 5: 3
Enter number between 3 and 6: 5
3.5 was rounded up, it's now three1

如果我将行更改为

if (num2 == 5) {numberAndDecimal = num1 + "." + num2 + " was rounded up, it's now " + (num1 + 1);}
if (num2 == 6) {numberAndDecimal = num1 + "." + num2 + " was rounded up, it's now " + (num1 + 1);}

我得到了这个输出

Enter number between 1 and 5: 3
Enter number between 3 and 6: 5
3.5 was rounded up, it's now 3

3 个答案:

答案 0 :(得分:2)

好的,首先:当你执行(numberInWords + 1)时,你要在String中添加一个int。因此,Java将整数转换为字符串并将它们连接起来。这就是你如何得到“three1”作为答案。

如果您将行更改为

if (num2 == 5) {
    numberAndDecimal = num1 + "." + num2 + " was rounded up, it's now " +  (num1 + 1);
}
if (num2 == 6) {
    numberAndDecimal = num1 + "." + num2 + " was rounded up, it's now " + (num1 + 1);
}

输入3.5时输出为4;只是没有说话。

要将数字翻译成单词,在可能添加一个字符后,必须在字符串中进行翻译。因此,最简单的解决方案是使用单独的函数将int转换为字符串。像这样:

public static String EnglishName(int num1, int num2) {

    String numberAndDecimal = "";

    if (num2 == 3) {
        numberAndDecimal = num1 + "." + num2 + " not rounded up its still "
                + translate(num1);
    }
    if (num2 == 4) {
        numberAndDecimal = num1 + "." + num2 + " not rounded up its still "
                + translate(num1);
    }
    if (num2 == 5) {
        numberAndDecimal = num1 + "." + num2 + " was rounded up, it's now "
                + translate(num1 + 1);
    }
    if (num2 == 6) {
        numberAndDecimal = num1 + "." + num2 + " was rounded up, it's now "
                + translate(num1 + 1);
    }

    return numberAndDecimal;
}

public static String translate(int num1) {

    String numberInWords = "";

    if (num1 == 1) {
        numberInWords = "one";
    }
    if (num1 == 2) {
        numberInWords = "two";
    }
    if (num1 == 3) {
        numberInWords = "three";
    }
    if (num1 == 4) {
        numberInWords = "four";
    }
    if (num1 == 5) {
        numberInWords = "five";
    }

    return numberInWords;
}

PS。:请使用名称以小写字母开头的函数命名来保持Java编码约定。因此,在您的情况下,它应该是englishName而不是EnglishName

答案 1 :(得分:0)

尝试这种方法:

String num = num1 + "." + num2;
double number = Double.parseDouble(num);
int roundOffAns = (int)Math.round(number);

答案 2 :(得分:0)

当我在底部使用正确的修复程序运行代码时:

if (num2 == 5) {numberAndDecimal = num1 + "." + num2 + " was rounded up, it's now " + (num1 + 1);}
if (num2 == 6) {numberAndDecimal = num1 + "." + num2 + " was rounded up, it's now " + (num1 + 1);}

输入1到5:3之间的数字 输入3到6:5之间的数字 3.5被四舍五入,现在是4