我需要帮助编写一个程序,该程序将接受介于0到9之间的数字,如果用户输入范围内的数字,我将打印该数字本身的次数。示例:如果用户输入5,程序的输出将为“55555”。提前谢谢。
import java.util.*;
import java.text.*;
public class Numbers{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int oneDigit = 0;
try{
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number between 0 and 9: ");
oneDigit = reader.nextInt();
if (oneDigit < 0 || oneDigit > 9)
System.out.println("You did not enter a number between 0 and 9!");
else
}
catch(InputMismatchException ime){
System.out.println("You didn't enter a number.");
}
}
}
答案 0 :(得分:2)
您需要for
中的else
循环才能打印oneDigit
多个号码。时间。
else{
for (int i = 0; i < oneDigit; i++) {
System.out.print(oneDigit);
}
}
答案 1 :(得分:0)
要跳过if条件本身你可以使用类似这样的东西
for (int i = 0; i<num && num<=9 ; i++){
System.out.print(num);
}
但是如果数字超出范围
,您仍然需要使用if条件来显示警报