I am having trouble with the following question...
"write a method displayDigits that receives an integer between 1 and 99999 and displays it as a sequence of digits, separating each pair of digits by two spaces. For example, the integer 4562 should appear as 4 5 6 2"
This is what I have currently, I know it displays the numbers backwards, but won't compile with a "error: missing return statement"
error.
public static int displayDigits(int num1)
{
for (int i=0; num1>0; i++)
{
return(num1 % 10)
}
}
The book noted that we only use %
and /
to solve the problem. Help please!! I've been working on this for hours...
答案 0 :(得分:-1)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
答案 1 :(得分:-1)
public static void displayDigits(int num1) {
String result = "";
for (int i=0; num1>0; i++) {
if(i>0) result = " " + result;
result = num1%10 + result;
num1 /= 10;
}
System.out.println(result);
}
答案 2 :(得分:-3)
Do following:
String resultNumber = "";
for(int i = 0; i < num1.toString().length(); i++)
resultNumber += num1.toString().substring(i, i+1) + " ";
resultNumber = resultNumber.substring(0, resultNumber.length() - 2);
When num1 is 4564, then resultNumber is "4 5 6 4" and that works for any number (between min and max value of integer)