【发布时间】:2022-11-15 11:47:19
【问题描述】:
导入 java.util.Scanner;
公共课数字{
public static void main(String[] args) {
/*
*
count = 1
temp = n
while (temp > 10)
Increment count.
Divide temp by 10.0.
*/
//Assignment: fix this code to print: 1 2 3 (for 123)
//temp = 3426 -> 3 4 2 6
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int count = 1;
int temp = input.nextInt();
while(temp >= 10){
count++;
temp = temp / 10;
System.out.print(temp + " ");
}
}
}
需要帮助修复代码。 示例:当您输入 123 时,它变为 1 2 3。
【问题讨论】:
-
尝试将输入读取为字符串,然后使用循环
for (char c : temp.toCharArray()) -
它说 Cannot invoke toCharArray() on the primitive type int
-
也许你应该把它改成
String
标签: java eclipse loops pseudocode