在Java中切换和循环

在Java中切换和循环,java,Java,关于我的开关,我需要您的帮助 以下是一个例子: 1) 我输入选项1 *****MENU***** 1) Start Play : 2) Exit : Enter your choice : 1 2) 我输入数字4 You have 5 attemps. Enter your number : 4 Bravo ! 3) 我赢了 我又想玩了 我在选项1中输入数字1 Bravo ! *****MENU***** 1) Start Play : 2) Exit : Enter your

关于我的
开关
,我需要您的帮助

以下是一个例子:

1) 我输入选项1

*****MENU*****
1) Start Play : 
2) Exit : 
Enter your choice : 1
2) 我输入数字4

You have 5 attemps.
Enter your number : 4
Bravo ! 
3) 我赢了

我又想玩了

我在选项1中输入数字1

Bravo ! 
*****MENU*****
1) Start Play : 
2) Exit : 
Enter your choice : 1
在这里,我有一个问题。。。我不能进入选项1然后玩

我总是这样:

Enter your choice : 1
Option 1 : 
*****MENU*****
1) Start Play : 
2) Exit : 
Enter your choice : 
我想我的问题出在我的
案例1

int choice_user = 0;
int number_to_search = 4;
int number_user = 0;
boolean number_found = false;
int attemps = 5;

do {
    System.out.println("*****MENU*****");
    System.out.println("1) Start Play : ");
    System.out.println("2) Exit : ");
    System.out.print("Enter your choice : ");
    choice_user = sc.nextInt();

    switch (choice_user) {
        case 1:
            System.out.println("Option 1 : ");

            while (number_to_search > 0 && !number_found) {
                System.out.println("You have " + attemps + " attemps.");
                System.out.print("Enter your number : ");
                number_user = sc.nextInt();

                if (number_user > number_to_search) {
                    System.out.println("Smallest ! ");
                } else if (number_user < number_to_search) {
                    System.out.println("Biggest ! ");
                } else {
                    number_found = true;
                    System.out.println("Bravo ! ");
                }
                attemps--;
            }
            break;

        default:
            System.out.println("Exit...");
    }
} while (choice_user != 2);
int-choice\u user=0;
整数搜索=4;
int number_user=0;
布尔数\u found=false;
int attemps=5;
做{
System.out.println(“******菜单*******”);
System.out.println(“1)开始播放:”;
系统输出打印项次(“2)退出:”;
System.out.print(“输入您的选择:”);
choice_user=sc.nextInt();
开关(用户选择){
案例1:
System.out.println(“选项1:”);
while(编号搜索>0&!找到编号){
System.out.println(“您有”+attemps+“attemps”);
System.out.print(“输入您的号码:”);
number_user=sc.nextInt();
如果(编号用户>编号到搜索){
System.out.println(“最小的!”);
}else if(数字用户<数字到搜索){
System.out.println(“最大的!”);
}否则{
找到的数字=真;
System.out.println(“太棒了!”);
}
尝试--;
}
打破
违约:
System.out.println(“退出…”);
}
}while(选择用户!=2);

您的代码逻辑有几个问题

主要问题,以及为什么它在第一次迭代后继续循环而不改变,是因为您没有在下一场游戏中将
number\u found
的值重置为
false

此外,您需要将
attemps
重置回
5
,还需要在(attemps>0)期间检查
,而不是在搜索时检查
编号,这不会改变

更改后的代码如下所示:

public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        int choice_user = 0;
        int number_to_search = 4;
        int number_user = 0;

        do{
           System.out.println("*****MENU*****");
           System.out.println("1) Start Play : ");
           System.out.println("2) Exit : ");
           System.out.print("Enter your choice : ");
           choice_user = sc.nextInt();

           int attemps = 5;
           boolean number_found = false;

           switch(choice_user){
               case 1 :
                  System.out.println("Option 1 : ");

                   while(attemps > 0 && !number_found){
                        System.out.println("You have " + attemps + " attemps.");
                        System.out.print("Enter your number : ");
                        number_user = sc.nextInt();

                        if(number_user > number_to_search){
                            System.out.println("Smallest ! ");
                        }
                        else if(number_user < number_to_search){
                            System.out.println("Biggest ! ");
                        }
                        else{
                            number_found = true;
                            System.out.println("Bravo ! ");
                        }
                        attemps--;
                   }
                 break;
                 default:
                       System.out.println("Exit...");
           }
        }while(choice_user != 2);
    }
publicstaticvoidmain(字符串[]args)
{
扫描仪sc=新的扫描仪(System.in);
int choice_user=0;
整数搜索=4;
int number_user=0;
做{
System.out.println(“******菜单*******”);
System.out.println(“1)开始播放:”;
系统输出打印项次(“2)退出:”;
System.out.print(“输入您的选择:”);
choice_user=sc.nextInt();
int attemps=5;
布尔数\u found=false;
开关(用户选择){
案例1:
System.out.println(“选项1:”);
while(尝试>0&!找到编号){
System.out.println(“您有”+attemps+“attemps”);
System.out.print(“输入您的号码:”);
number_user=sc.nextInt();
如果(编号用户>编号到搜索){
System.out.println(“最小的!”);
}
else if(数字用户<数字到搜索){
System.out.println(“最大的!”);
}
否则{
找到的数字=真;
System.out.println(“太棒了!”);
}
尝试--;
}
打破
违约:
System.out.println(“退出…”);
}
}while(选择用户!=2);
}

注意我是如何移动
intattemps=5的
布尔数\u found=false进入
do while
循环。我测试了代码,现在它似乎对我来说工作正常。

您的代码逻辑有一些问题

主要问题,以及为什么它在第一次迭代后继续循环而不改变,是因为您没有在下一场游戏中将
number\u found
的值重置为
false

此外,您需要将
attemps
重置回
5
,还需要在(attemps>0)
期间检查
,而不是在搜索时检查
编号,这不会改变

更改后的代码如下所示:

public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        int choice_user = 0;
        int number_to_search = 4;
        int number_user = 0;

        do{
           System.out.println("*****MENU*****");
           System.out.println("1) Start Play : ");
           System.out.println("2) Exit : ");
           System.out.print("Enter your choice : ");
           choice_user = sc.nextInt();

           int attemps = 5;
           boolean number_found = false;

           switch(choice_user){
               case 1 :
                  System.out.println("Option 1 : ");

                   while(attemps > 0 && !number_found){
                        System.out.println("You have " + attemps + " attemps.");
                        System.out.print("Enter your number : ");
                        number_user = sc.nextInt();

                        if(number_user > number_to_search){
                            System.out.println("Smallest ! ");
                        }
                        else if(number_user < number_to_search){
                            System.out.println("Biggest ! ");
                        }
                        else{
                            number_found = true;
                            System.out.println("Bravo ! ");
                        }
                        attemps--;
                   }
                 break;
                 default:
                       System.out.println("Exit...");
           }
        }while(choice_user != 2);
    }
publicstaticvoidmain(字符串[]args)
{
扫描仪sc=新的扫描仪(System.in);
int choice_user=0;
整数搜索=4;
int number_user=0;
做{
System.out.println(“******菜单*******”);
System.out.println(“1)开始播放:”;
系统输出打印项次(“2)退出:”;
System.out.print(“输入您的选择:”);
choice_user=sc.nextInt();
int attemps=5;
布尔数\u found=false;
开关(用户选择){
案例1:
System.out.println(“选项1:”);
while(尝试>0&!找到编号){
System.out.println(“您有”+attemps+“attemps”);
System.out.print(“输入您的号码:”);
number_user=sc.nextInt();
如果(编号用户>编号到搜索){
System.out.println(“最小的!”);
}
else if(数字用户<数字到搜索){
System.out.println(“最大的!”);
}
否则{
找到的数字=真;
System.out.println(“太棒了!”);
}
尝试--;
}
打破
违约:
System.out.println(“退出…”);
}
}while(选择用户!=2);
}
注意如何