需要帮助理解这个基础挑战吗?

huangapple go评论73阅读模式
英文:

Need help understanding this basic challenge?

问题

public class Main {

    public static void main(String[] args) {
        numberToWords(567);
    }

    public static void numberToWords(int number) {
        int lastDigit = 0;
        int digit = number;
        int reverseDigit = 0;

        if (number < 0) {
            System.out.println("Invalid Value");
        }

        for (int i = 0; i < digit; i++) {
            // Taking the last digit from 567 per iteration
            lastDigit = digit % 10;
            // Dividing the digit by 10 each time to get another last digit
            digit /= 10;
            // Flipping the number for correct sequence
            reverseDigit = (reverseDigit * 10) + lastDigit;
            // Printing the word representation of the reversed digit
            System.out.println(reverse(reverseDigit));

            // Printing the word representation of the last digit
            if (lastDigit == 0) {
                System.out.println("Zero");
            } else if (lastDigit == 1) {
                System.out.println("One");
            } else if (lastDigit == 2) {
                System.out.println("Two");
            } else if (lastDigit == 3) {
                System.out.println("Three");
            } else if (lastDigit == 4) {
                System.out.println("Four");
            } else if (lastDigit == 5) {
                System.out.println("Five");
            } else if (lastDigit == 6) {
                System.out.println("Six");
            } else if (lastDigit == 7) {
                System.out.println("Seven");
            } else if (lastDigit == 8) {
                System.out.println("Eight");
            } else if (lastDigit == 9) {
                System.out.println("Nine");
            }
        }
    }

    public static int reverse(int a) {
        int lastDigit = 0;
        for (int i = 0; i < a; i++) {
            lastDigit = a % 10;
            a /= 10;
        }
        return lastDigit;
    }
}
英文:

Currently doing a challenge called number to words, I have created 2 methods, one which
deals with comparing values individually so that it can print out it's string value(method called numberToWord), another method called reverse which basically re-orders the values so that it is printed in a correct sequence, for example:s
Step One 567 --> Step 2, it will be converted into 765 --> Step 3, reverse method will then convert it back to 5,6,7 individually so that it can compare the values with the if statements. However, i have tried so many things to getting this to work, i managed to make it to step 3 but when i try to return the value it gives me 3 random values before it converts e.g = 7,6,7...5,6,7, i am unable to figure out how to remove the first 3 values and return just the last three values so that it can be compared in the numberToWords method.

package com.company;
public class Main {
public static void main(String[] args) {
numberToWords(567);
}
public static void numberToWords(int number) {
int lastDigit = 0;
int digit = number;
int reverseDigit = 0;
if (number &lt; 0) {
System.out.println(&quot;Invalid Value&quot;);
}
for (int i = 0; i &lt; digit; i++) {
//so we are taking the last digit from 567 per iteration
lastDigit = digit % 10;
//we are then dividing the digit by 10 each time so we can get another last digit
digit /= 10;
//i will now try to essentially get down to the final number which will be flipped
reverseDigit = (reverseDigit * 10) + lastDigit;
//checking values
System.out.println(reverse(reverseDigit));
//            if (reverse(reverseDigit)== 0) {
//                System.out.println(&quot;Zero&quot;);
//            } else if (reverse(reverseDigit) == 1) {
//                System.out.println(&quot;One&quot;);
//            }else if (reverse(reverseDigit) == 2) {
//                System.out.println(&quot;Two&quot;);
//            }else if (reverse(reverseDigit) == 3) {
//                System.out.println(&quot;Three&quot;);
//            }else if (reverse(reverseDigit) == 4) {
//                System.out.println(&quot;Four&quot;);
//            }else if (reverse(reverseDigit) == 5) {
//                System.out.println(&quot;Five&quot;);
//            }else if (reverse(reverseDigit) == 6) {
//                System.out.println(&quot;Six&quot;);
//            }else if (reverse(reverseDigit) == 7) {
//                System.out.println(&quot;Seven&quot;);
//            }else if (reverse(reverseDigit) == 8) {
//                System.out.println(&quot;Eight&quot;);
//            }else if (reverse(reverseDigit) == 9) {
//                System.out.println(&quot;Nine &quot;);
//            }
if (lastDigit == 0) {
System.out.println(&quot;Zero&quot;);
} else if (lastDigit == 1) {
System.out.println(&quot;One&quot;);
} else if (lastDigit == 2) {
System.out.println(&quot;Two&quot;);
} else if (lastDigit == 3) {
System.out.println(&quot;Three&quot;);
} else if (lastDigit == 4) {
System.out.println(&quot;Four&quot;);
} else if (lastDigit == 5) {
System.out.println(&quot;Five&quot;);
} else if (lastDigit == 6) {
System.out.println(&quot;Six&quot;);
} else if (lastDigit == 7) {
System.out.println(&quot;Seven&quot;);
} else if (lastDigit == 8) {
System.out.println(&quot;Eight&quot;);
} else if (lastDigit == 9) {
System.out.println(&quot;Nine&quot;);
}
}
}
public static int reverse (int a){
int lastDigit = 0;
for (int i =0; i &lt; a; i++) {
lastDigit = a % 10;
a /= 10;
//testing values here
//System.out.println(lastDigit);
//sout in loop gives us 567
}
return lastDigit;
}
}

答案1

得分: 0

我认为你对于 numberToWords() 和 reverse() 函数使用取余除法的想法是正确的。
但是我认为你退出 for 循环的条件是错误的:

for (int i = 0; i < digit; i++)

在获取所有数字之前,循环将退出。你应该循环直到 digit 为 0,因为你想要执行取余除法直到没有更多可以除的为止(也就是 digit 为 0)。

所以你应该这样写:

for (int i = 0; digit != 0; i++) {
...
}

使用 while 循环可能比 for 循环更好,因为你实际上并没有使用 i 变量:

while (digit != 0) {
...
}

另外,这也取决于你使用的是哪个集成开发环境(IDE),但是 IntelliJ 和 Eclipse 都可以逐行调试,这样你就能看到哪一行代码出问题了。

英文:

I think you have the right idea of doing remainder division for numberToWords() and reverse().
But I think you're condition for exiting the for loop is wrong

for (int i = 0; i &lt; digit; i++)

It will exit before you get all the digits. You want to loop until digit is 0, since you want to remainder divide UNTIL there's no more to divide ( meaning digit is 0).

so

for(int i = 0; digit != 0; i++){
...
}

while loop might be better than a for a loop since you're not using i anyways.

while(digit != 0){
...
}

It also depends on which IDE you're using but IntelliJ and eclipse can do a line by line debug so you see which line is causing issues.

答案2

得分: 0

如果我正确理解您的需求,您不需要太多的代码。只需打印除以10的余数,然后在下一次迭代中除以10,直到得到零为止。

enum Numbers {
    Zero,
    One,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine;
}

private static void process(int number) {
    do {
        final int digit = number % 10;
        System.out.println(Numbers.values()[digit]);
        number /= 10;
    } while (number > 0);
}

首先使用枚举,因为您可以像使用数组一样使用它,但更容易声明。然后,使用您得到的数字,首先取模十。对于任何表示为十进制的整数,模十就是最后一位数字。由于您必须首先打印出最后一位数字,所以只需打印它。然后将数字替换为数字除以十。这将丢弃最后一位数字(刚刚打印出来的数字),因为这是整数除法。我的意思是,1234除以10是123.4,但由于这些都是整数,它被截断为123。所以现在循环,最后一位数字将再次被打印出来(但现在由于除法,它将成为最左边的数字)。在某个时候,在打印原始数字的第一位数字之后,您将得到一个一位数的整数(例如7),除以10后结果为零(因为它将是0.7,但这是整数除法)。
您需要添加一些先决条件,例如负数。

英文:

If I understand what you need correctly, you don't need that much code. Just print the reminder of division by 10 and then divide by ten for then next iteration until you get zero.

enum Numbers {
Zero,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine;
}
private static void process(int number) {
do {
final int digit = number % 10;
System.out.println(Numbers.values()[digit]);
number /= 10;
} while (number &gt; 0);
}

First use an enum since you can use it like an array but easier to declare. Then with the number you get, first get the reminder over ten. For any integer expressed as decimal the reminder over ten is the last digit. Since you have to print the last digit in the first place, just print it. Then replace number with number divided by ten. That will discard the last digit (the one just printed out) because this is an integer division. I mean, 1234 over 10 is 123.4 but because these are integer it gets truncated to 123. So now loop and the last digit will be printed again (but now it will be the first to the left due to the division). At some point, after printing the first digit of the original number you will have a one digit integer (i.e. 7) divided by ten which results in zero (because it would be 0.7 but it's an integer division).
You need to add some preconditions like the negatives.

huangapple
  • 本文由 发表于 2020年9月15日 08:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63893538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定