试图理解for()和while()循环

时间:2017-02-05 01:56:39

标签: java loops for-loop break

我需要帮助完成学校作业的代码:

  

制作一个用户可以选择的菜单并允许他们选择   哪个循环练习选项运行。以下循环将永远运行   直到用户输入一个退出选项int choice;

while(true) { 
    System.out.println(“Please enter in a choice, 0 to quit”); 
    choice = input.nextInt(); //assumes a Scanner object has been created 
    if (choice == 0) {
        break; 
    } 
}

到目前为止,这是我的代码:

import java.util.Scanner;

public class Christmas3 {
    public static void main(String[] args) { 
        Scanner one = new Scanner(System.in);
        int on,tw;
        System.out.println("Enter 1 to execute loop one, 2 to execute loop two or 3 to exit");
        on = one.nextInt();
        while (on == 1) {
            System.out.println("Please enter 1 to confirm the loop or 0 to exit"); 
            tw = one.nextInt(); 
            if (tw == 0) {
                break; 
            }
        }
        for(int i = 0; on > i; on++) {
            System.out.print(" "+on + ", "); 
        } 
        while (on == 2) { 
            System.out.println("Please enter 2 to confirm the loop or 0 to exit"); 
            tw = one.nextInt(); 
            if (tw == 0) {
                break; 
            }
            for(int j = 0; on > j; on = on + 10){
                System.out.print(" " + on  + ", "); 
            }

            if (on == 3) {
                break;
            }
        }
        one.close();
    }
} 

我不知道如何执行以下部分"以下循环将一直运行,直到用户输入退出选项int choice;"

感谢。

2 个答案:

答案 0 :(得分:0)

public static void printMyInstructions()
{
    System.out.println("Enter 1 to execute loop one, 2 to execute loop two or 3 to exit");

}

public static void main( String[] args )
{
    Scanner myInput = new Scanner(System.in);
    int userChoice = -1 , exitChoice = 3;
    while( userChoice != exitChoice )
    {
        printMyInstructions();
        userChoice = myInput.nextInt();
        analyzeChoice( userChoice );
    }
}

public static void analyzeChoice( int enteredUserChoice )
{
    if( enteredUserChoice == 1 )
    {
        // Do Something
    }
    else if( enteredUserChoice == 2 )
    {
        // Do Something
    }
    else if( enteredUserChoice == 3 )
    {
        System.out.println("Bye Bye");
    }
    else
    {
        System.out.println("Can't really tell what to do with this Option: " + enteredUserChoice);
    }
}

答案 1 :(得分:0)

这是你要找的吗?

import java.util.Scanner;

public class Loops {
    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in);
        for (;;) {
            System.out.println("\nMain: Enter 1 to execute loop one, 2 to execute loop two or 3 to exit");
            switch (input.nextInt()) {
            case 1:
                for (boolean loopExit = false;!loopExit;) {
                    System.out.println("Loop 1: Please enter 1 to confirm the loop or 0 to exit"); 
                    switch (input.nextInt()) {
                    case 0:
                        loopExit = true;
                        break;
                    case 1:
                        System.out.println("Loop 1: Confirmed!");
                        break;
                    default:
                        System.out.println("Loop 1: ?Unrecognized input");
                        break;
                    }
                }
                System.out.println("Exited loop 1");
                break;
            case 2:
                for (boolean loopExit = false;!loopExit;) {
                    System.out.println("Loop 2: Please enter 1 to confirm the loop or 0 to exit"); 
                    switch (input.nextInt()) {
                    case 0:
                        loopExit = true;
                        break;
                    case 1:
                        System.out.println("Loop 2: Confirmed!");
                        break;
                    default:
                        System.out.println("Loop 2: ?Unrecognized input");
                        break;
                    }
                }
                System.out.println("Exited loop 2");
                break;
            case 3:
                input.close();
                System.exit(-1);
                break;
            default:
                System.out.println("?Unrecognized input");
                break;
            }
        }
    }
}