-1

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

基本上我试图让我的芬奇机器人根据它的定位方式改变鼻子的颜色,但我对如何让它允许用户在它已经定位后再次定位它感到困惑,这样鼻子的颜色就可以多次改变颜色。当它第一次运行时,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;
}

}

4

1 回答 1

1

不要在输入值的条件之前设置你的 flag = false 。在 if(input == 3) 情况下将其设置为 false

于 2014-05-15T17:50:59.593 回答