我最近刚开始使用Java,我很糟糕。我真的压力很大。我需要弄清楚如何使用do-while
循环将用户输入的1到10之间的内容转换为星号。如果你能教我怎么做我会很感激的。
System.out.println( "Enter number between one and ten: " );
示例:input = 7
预期输出:*******
如果数字不在1和10之间,则显示“重试”并再次询问
public class JavaApplication12 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
System.out.println( "Enter number between one and ten: " );
int count = in.nextInt();
int counter = 0;
if (count<1||count>10) {
System.out.println("Try again");
count = in.nextInt();
System.out.print("*");
counter++;
}else{
do {
System.out.print("*");
counter++;
} while (counter < count);
}
}
}
https://stackoverflow.com/questions/52863900
复制相似问题