Java - 来自交换机案例的连续循环。我在这做错了什么?

时间:2016-03-14 16:31:43

标签: java switch-statement

以下是我的一个小项目的代码。我已经写了所需的内容,但是我要么缺少某些东西,要么没有放置有效的代码来阻止println“在此输入你的关键字”的连续循环。有人可以在这个源代码中找到我的错。我在这做错了什么?选择退出选项后,我不断被问到“输入您的关键字”问题。

import java.util.Scanner;

public class JobApplication {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Job Search Portal");
        System.out.println("_ _ _ _ _ _ _ _ _ ");
        System.out.println("name - To Enter Your Name:  ");
        System.out.println("dob - To Enter Your Date of Birth:  ");
        System.out.println("profession - To enter your profession");
        System.out.println("years - To enter the number of years you've workd");
        System.out.println("income - To enter your income from previous employer");

        String keyword = null;
        String name = null;
        String dob = null;
        String profession = null;
        int years = 0;
        long income = 0;
        String exit = null;

        do {
            System.out.println("\nEnter your keyword here");
            keyword = input.next();

            switch (keyword.toLowerCase()) {

                case "name":
                    System.out.println("Please enter your name: ");
                    name = input.next();
                    break;
                case "dob":
                    System.out.println("Please enter your date of birth");
                    dob = input.next();
                    break;
                case "profession":
                    System.out.println("Please enter your profession");
                    profession = input.next();
                    break;
                case "years":
                    System.out.println("Please enter the number of years experience you have");
                    years = input.nextInt();
                    if (years >= 2) {
                        if (years > 40) System.out.println("You are too old ");
                    } else {
                        System.out.println("You are in experienced");
                    }
                    break;

                case "income":
                    System.out.println("Please enter your previous income");
                    income = input.nextLong();
                    break;

                case "exit":
                    System.out.println("The following are your details!");
                    System.out.println("Name: " + name);
                    System.out.println("Date of Birth: " + dob);
                    System.out.println("Profession: " + profession);
                    System.out.println("Experience: " + years + " years");
                    System.out.println("Yearly Salary: K" + income);
                    System.out.println("We will get back to you soon!");
                    System.out.println("Good luck!");break;

                default: System.out.println("Enter a valid choice!");

        }

    }while(true);
  }
}

3 个答案:

答案 0 :(得分:0)

您可以按照here所述使用label for循环。

在你的例子中:

mainLoop: do {
//and then
break mainLoop;

但是在我看来,在主循环中改变条件会是更好的解决方案。

答案 1 :(得分:0)

之所以发生这种情况,是因为您使用while(true)的当前条件无限地运行它。您需要一些条件来退出while循环。查看代码,看起来您想在用户输入exit后停止提问。将您的while条件更改为:

while(keyword.toLowerCase().compareTo("exit") != 0);

或(正如@Tom指出的那样),更好的是:

 while (!keyword.toLowerCase().equals("exit"));

这将保证它至少运行一次,并在用户输入exit时退出。干杯!

答案 2 :(得分:0)

而不是While(true)使用flag变量来控制循环。

我已经纠正了你的代码......你可以查看它。

import java.util.Scanner;
public class JobApplication {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Job Search Portal");
    System.out.println("_ _ _ _ _ _ _ _ _ ");
    System.out.println("name - To Enter Your Name:  ");
    System.out.println("dob - To Enter Your Date of Birth:  ");
    System.out.println("profession - To enter your profession");
    System.out.println("years - To enter the number of years you've workd");
    System.out.println("income - To enter your income from previous employer");

    String keyword = null;
    String name = null;
    String dob = null;
    String profession = null;
    int years = 0;
    long income = 0;
    String exit = null;

    int flag=0;

    do {
        System.out.println("\nEnter your keyword here");
        keyword = input.next();

        switch (keyword.toLowerCase()) {

            case "name":
                System.out.println("Please enter your name: ");
                name = input.next();
                break;
            case "dob":
                System.out.println("Please enter your date of birth");
                dob = input.next();
                break;
            case "profession":
                System.out.println("Please enter your profession");
                profession = input.next();
                break;
            case "years":
                System.out.println("Please enter the number of years experience you have");
                years = input.nextInt();
                if (years >= 2) {
                    if (years > 40) System.out.println("You are too old ");
                } else {
                    System.out.println("You are in experienced");
                }
                break;

            case "income":
                System.out.println("Please enter your previous income");
                income = input.nextLong();
                break;

            case "exit":
                System.out.println("The following are your details!");
                System.out.println("Name: " + name);
                System.out.println("Date of Birth: " + dob);
                System.out.println("Profession: " + profession);
                System.out.println("Experience: " + years + " years");
                System.out.println("Yearly Salary: K" + income);
                System.out.println("We will get back to you soon!");
                System.out.println("Good luck!");
        flag=1;
        break;

            default: System.out.println("Enter a valid choice!");

    }

}while(flag == 0);
}
}