【问题标题】:My reverse method doesn't reverse anything?我的反转方法没有反转任何东西?
【发布时间】:2021-07-21 05:52:49
【问题描述】:

这是我的代码:

public static void numberToWords(int number){
    int lastDigit;

    if(number < 0){
        System.out.println("Invalid Value");
    }
    while(number != 0) {
        lastDigit = number % 10; // lastDigit = 3
        number = number / 10;
        reverse(number);

        if (lastDigit == 0) {
            System.out.println("ZERO");
        }
        if (lastDigit == 1) {
            System.out.println("ONE");
        }
        if (lastDigit == 2) {
            System.out.println("TWO");
        }
        if (lastDigit == 3) {
            System.out.println("THREE");
        }
        if (lastDigit == 4) {
            System.out.println("FOUR");
        }
        if (lastDigit == 5) {
            System.out.println("FIVE");
        }
        if (lastDigit == 6) {
            System.out.println("SIX");
        }
        if (lastDigit == 7) {
            System.out.println("SEVEN");
        }
        if (lastDigit == 8) {
            System.out.println("EIGHT");
        }
        if (lastDigit == 9) {
            System.out.println("NINE");
        }
    }
}

public static void reverse(int a){
    int finalDigit = 0;
    int reverse1 = 0;

    while(a > 0) {
        finalDigit = a % 10;
        reverse1 = reverse1 * 10 + finalDigit;
        a = a / 10;
    }
}

我的数字根据需要打印为字符串,但它们以相反的顺序出现。

我尝试将reverse方法添加到转换方法中,但结果总是一样,不知道如何反转它。

【问题讨论】:

  • 你听说过else和switch吗?
  • 请编辑您的问题并为您使用的语言添加标签
  • 您的函数 reverse 不返回值,因此它所做的任何工作都不会被看到。 reverse 应该是 int 不是 void,需要返回 reverse1。
  • 这个问题不应该被关闭:这绝对不是通过引用传递的问题。 OP 只是无法弄清楚他们的算法,并且忘记了返回他们的反向函数。 @Abdulrahman 可以从对此的贡献中学到很多东西
  • @GhostCat 你能考虑投票重新提出这个问题吗?

标签: java reverse


【解决方案1】:
  1. 删除所有 if 语句,使用 Array 存储数字到单词的关联(数字 = 数组索引,字符串 = 数组值)
  2. 从右到左查找每个数字,构建所有数字单词的列表
  3. 反转它
  4. 对列表做任何你想做的事(即将它加入一个字符串)
class NumTest {
    
    private static final String[] digits = {
        "ZERO", "ONE", "TWO", "THREE", "FOUR",
        "FIVE","SIX", "SEVEN", "EIGHT", "NINE"
    };
    
    private String numAsString(int num) {
        List<String> lDigs = new ArrayList<>();
        while (num > 0) {
            String dig = digits[num % 10];
            lDigs.add(dig);
            num /= 10;
        }
        Collections.reverse(lDigs);
        return lDigs.stream().collect(Collectors.joining(" "));
    }
    
    @Test
    public void testNumAsString() {
        Map<Integer, String> nums = new LinkedHashMap<>();
        //nums.put(0, "ZERO"); <- will return empty string
        nums.put(1, "ONE");
        nums.put(24, "TWO FOUR");
        nums.put(2567,  "TWO FIVE SIX SEVEN");
        nums.put(90000, "NINE ZERO ZERO ZERO ZERO");
        nums.put((int) 1.56e6, "ONE FIVE SIX ZERO ZERO ZERO ZERO");
        
        for (Entry<Integer, String> test: nums.entrySet()) {
            String res = numAsString(test.getKey());
            System.out.println(String.format("%d -> %s", test.getKey(), res));
            assertEquals(test.getValue(), res);
        }
    }
    
}

【讨论】:

  • 非常感谢您的回答,我会尽力从中学到最多的东西:)
猜你喜欢
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 2021-04-21
  • 2021-11-17
相关资源
最近更新 更多