如何停止方法连续循环

时间:2015-02-12 16:58:14

标签: java

我目前正在尝试使用java中的多个类来完成一个程序,该程序将允许用户输入信息以帮助他或她预订摇滚音乐节的门票,住宿,停车等。我已经开始使用其中一个类'accommodation'来将用户的正确输入返回给主类,但是,我发现当我运行程序并输入选项3时,它会立即循环,我必须终止它。我已经在网上搜索了一种停止循环的方法,并且为了返回正确的输入信息无济于事,我会感谢任何对新的新菜鸟的帮助,在这个循环让我变得循环之前! 以下是我的主要课程和班级“住宿”。提前谢谢你,并为我的任何杂乱编码道歉,因为我一直在尝试各种选项,如前所述。

import java.util.Scanner;
import java.util.InputMismatchException;

public class clydeRockfest {

public static void main(String[] args) {




    boolean quit = false;
    Scanner in = new Scanner(System.in);
    int choice;    // Display the menu
    int answer = 0;

    Accommodation accommodation = new Accommodation();
    //accommodation.getaccommodation();


    do{

    System.out.println("1\t Festgoers");
    System.out.println("2\t Ticket");
    System.out.println("3\t Accommodation");
    System.out.println("4\t Transport");
    System.out.println("5\t Parking");
    System.out.println("0\t Quit");


    System.out.println("Please enter your choice:");

    //Get user's choice
    choice=in.nextInt();
     if(choice == 0)
         quit=true;


    //Display the title of the chosen module
    switch (choice) {

   break;

        case 3: accommodation.getaccommodation();
            System.out.println("You require " + answer + " accommodation."); 
   break;


        case 0: 
            quit=true;
   break;

        default: System.out.println("Invalid choice, please choose again.");

    } //end of switch
} //end of the main method
    while(!quit);
} //end of class

}


public class Accommodation {

     private String accommodation;

     void getaccommodation(){


        int no = 0;   // no accommodation at all required
        int self_Pitch = 0;  // chosen if requiring a pitch
        int tent = 0;  // chosen if using a tent
        int answer = 0;
        int choice = 0;

        boolean done = false;


        System.out.println("Do you require accommodation?");
        System.out.println();

        // Answer validation loop
        boolean validanswer = true;
        while (!validanswer){   

        System.out.println("Enter:(1=NO, 2=SELF-PITCH, 3=TENT)");
        System.out.println();


        if(answer > 0 && answer < 4){
            validanswer = true;
            done = true;
        }// ends if
        else{
        System.out.println();
        System.out.println("That is not a valid answer, please choose again:");
        System.out.println();

        }   // ends else
    } //ends while
}

        public void setaccommodation(String accommodation){
        this.accommodation = accommodation;
       }

输出:

Please enter your choice:
3
Do you require accommodation?

You require 0 accommodation.
1    Festgoers
2    Ticket
3    Accommodation
4    Transport
5    Parking
0    Quit
Please enter your choice:

3 个答案:

答案 0 :(得分:1)

通过设置done=false来启动循环,但从未设置done = true,以便循环永远不会结束

答案 1 :(得分:0)

你有两个循环检查用户是否完成,并且第一个(带有done)变量的条件永远不会改变。只需删除此循环,你应该没问题。

此外,看起来第二个循环变量的条件应该是

if (answer > 0 && answer < 4)

匹配您的菜单替代品。

答案 2 :(得分:0)

您从未将done设置为true。看来你可能需要这样做:

if(answer >=0 && answer <=4){
    validanswer = true;
    done = true;
}
else{
    //code
}

但是,我甚至不确定你首先需要那个外环:

while(!done){

似乎多余。