而在Java中循环行为异常

而在Java中循环行为异常,java,loops,while-loop,finch,Java,Loops,While Loop,Finch,我试图让我的循环一次又一次地重复,直到用户输入“退出”,然后它就会退出 基本上,我试图让我的Finch机器人根据它的定位方式改变鼻子颜色,但我不知道如何让它允许用户在它已经定位后再次定位,以便鼻子颜色可以多次改变颜色。当它第一次运行时,Finch将执行代码,但随后立即退出 这是我的密码: public class finch { public static final int INCREASE = 20; public static final int SEC = 1000; public s

我试图让我的循环一次又一次地重复,直到用户输入“退出”,然后它就会退出

基本上,我试图让我的Finch机器人根据它的定位方式改变鼻子颜色,但我不知道如何让它允许用户在它已经定位后再次定位,以便鼻子颜色可以多次改变颜色。当它第一次运行时,Finch将执行代码,但随后立即退出

这是我的密码:

public class finch {

public static final int INCREASE = 20;
public static final int SEC = 1000;
public static final int MAXSPEED = 255;
public static final int HALFSEC = 500;

public static Finch myFinch;
public static void main(String[] args) {
    myFinch = new Finch();

    Menu();

}

public static void Menu() {

    Scanner console = new Scanner(System.in);

    System.out.println("Enter your choice:" + "");

    int input;
    int input1;

    boolean flag=true;
    while(flag){

        System.out.println("1.\t" + "Rolling" + "Finch");
        System.out.println("2.\t" + "Obedient" + "Finch");
        System.out.println("3.\t" + "Exit");
        System.out.println();
        System.out.println("Enter your choice:");
        System.out.println();


        input = console.nextInt();
        flag=false;
        if (input == 1) {
            //input = DarkFinch;
            System.out.println("Position the Finch \"down\" or \"up\" to change nose color");
            rolling(myFinch);
        } else if (input == 2) {
            // input = ChasetheFinch;
            //  System.out.println("Chase The Finch");
        } else if (input == 3) {
            //    input = Exit;
            System.out.println("Goodbye");
        } else {
            //    System.out.println("Try again");

            flag=true;
            /*  return Menu(); */
        }
    }
}

public static boolean rolling(Finch myFinch) {//This method will change the Finches nose color depending on the Finches position.
    //"up" = place the finch upright standing on its tail



    for (int i = 1; i <= 20; i++) {


        while (myFinch.isBeakDown() || myFinch.isBeakUp()) {
            if (myFinch.isBeakDown()) {
                myFinch.setLED(0,0,255,3000);
            } else if (myFinch.isBeakUp()) {
                myFinch.setLED(255,0,0,3000);
            } else {
                myFinch.setLED(0,0,0,5000);
            }
        }

    }
    return true;
}
公共类雀鸟{
公共静态最终整数增加=20;
公共静态最终整数秒=1000;
公共静态最终整数MAXSPEED=255;
公共静态最终整数半秒=500;
公共静态雀鸟;
公共静态void main(字符串[]args){
myFinch=新Finch();
菜单();
}
公共静态无效菜单(){
扫描仪控制台=新扫描仪(System.in);
System.out.println(“输入您的选择:+”);
int输入;
int输入1;
布尔标志=真;
while(旗帜){
System.out.println(“1.\t”+“Rolling”+“Finch”);
System.out.println(“2.\t”+“顺从”+“芬奇”);
System.out.println(“3.\t”+“Exit”);
System.out.println();
System.out.println(“输入您的选择:”);
System.out.println();
input=console.nextInt();
flag=false;
如果(输入=1){
//输入=暗英寸;
System.out.println(“将雀鸟“向下”或“向上”放置以改变鼻子颜色”);
滚球;
}else if(输入=2){
//输入=开槽英寸;
//System.out.println(“追逐雀鸟”);
}else if(输入=3){
//输入=退出;
System.out.println(“再见”);
}否则{
//System.out.println(“重试”);
flag=true;
/*返回菜单()*/
}
}
}
公共静态布尔滚动(Finch myFinch){//此方法将根据雀类的位置更改雀类的鼻子颜色。
//“向上”=将雀鸟直立放置在其尾巴上

对于(int i=1;i不要在输入值的条件之前设置您的标志=false。在if(input==3)的情况下,将其设置为false

除非输入为1、2或3,否则您必须退出。如果输入为“quit”,您可以尝试设置标志吗?您将无法使用
nextInt()
但是,为什么你不能只在
案例3
中添加
flag=false
?因此在所有其他案例中,当用户在方法“菜单”中键入“1”时,它会继续循环调用方法“滚动”我希望能够多次重新定位我的Finch机器人,使其颜色根据其面朝上、朝下或朝前而变化。在每个位置后,我想用“请重新定位机器人”这句话提示用户,以便机器人在改变位置时作出反应,但如果用户键入“退出”然后它将退出code@GoldRoger案例3是什么意思?