gpt4 book ai didi

java - 用 if 语句指向程序中的特定行? java

转载 作者:行者123 更新时间:2023-12-02 05:23:36 25 4
gpt4 key购买 nike

我有一个程序要求用户输入 1 到 6 之间的值。但是,如果他们不询问该范围内的值,我希望程序要求用户再次输入。我在该方法中有其他代码,所以我不能只指向该方法。

System.out.print("Now enter a number between 1 and 6 ");
dieNumber = keyboard.nextInt();

if (dieNumber == 1){
drawOne(pane); }
else if (dieNumber == 2){
drawTwo(pane); }
else if (dieNumber == 3){
drawThree(pane);}
else if (dieNumber == 4){
drawFour(pane); }
else if (dieNumber == 5){
drawFive(pane); }
else if (dieNumber == 6){
drawSix(pane); }

最佳答案

只需创建 while 循环,以便在每次用户输入大于 6 的数字时再次询问

System.out.print("Now enter a number between 1 and 6 ");

dieNumber = keyboard.nextInt();
while (dieNumber > 6){
System.out.print("Now enter a number between 1 and 6 ");
dieNumber = keyboard.nextInt();
}
if (dieNumber == 1){
drawOne(pane); }
else if (dieNumber == 2){
drawTwo(pane); }
else if (dieNumber == 3){
drawThree(pane); }
else if (dieNumber == 4){
drawFour(pane); }
else if (dieNumber == 5){
drawFive(pane); }
else if (dieNumber == 6){
drawSix(pane); }

关于java - 用 if 语句指向程序中的特定行? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26267117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com