我的代码没有进入while循环

hmae6n7t  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(267)

这是我代码的一部分,也是唯一不断出错的部分。即使它为false,也不会进入while循环。我还在学java,所以我想我可能做错了什么。

import java.util.Scanner;
public class CateringBooking {
String serv;
int menuSet;
String timeAndDate;
boolean valid = true;

//Selecting the kind of service method
public String kindOfService () {
    System.out.println("1. TYPE OF SERVICE SELECTION");
    System.out.println("Select the kind of service you want.");
    System.out.println("");
    System.out.println("A: Basic");
    System.out.println("B: Premium");
    System.out.println("C: Deluxe");
    System.out.println("D: Fiesta Deluxe");

    Scanner myService = new Scanner(System.in);
    System.out.print("Letter of your choice: ");
    String selectService = myService.nextLine();

    while (valid != true) {
        if (selectService.equals("A") || selectService.equals("a")) {
            valid = true;
            System.out.println("You've selected: Basic service");
        } else if (selectService.equals("B") || selectService.equals("b")) {
            valid = true;
            System.out.println("You've selected: Premium service");
        } else if (selectService.equals("C") || selectService.equals("c")) {
            valid = true;
            System.out.println("You've selected: Deluxe service");
        } else if (selectService.equals("D") || selectService.equals("d")) {
            valid = true;
            System.out.println("You've selected: Fiesta Deluxe service");
        } else {
            valid = false;
            System.out.println("Your choice is not available. Please try again.");
            System.out.println("");
        }
    }
    serv = selectService;
    return selectService;
}
hs1ihplo

hs1ihplo1#

在你申报的第6行 boolean valid = true; 从来没有设定过 false 在代码的后面,所以它不会进入while循环。

cczfrluj

cczfrluj2#

根据你的代码,我理解你想选择选项的,如果选项不是select,请在while循环中再次尝试该选项。你可以试试下面的方法。

import java.util.Scanner;
public class CateringBooking {
String serv;
int menuSet;
String timeAndDate;
boolean valid = true;

public String kindOfService() {
    System.out.println("1. TYPE OF SERVICE SELECTION");

    Scanner myService = new Scanner(System.in);
    String selectService = "";
    valid = false;
    while (valid != true) {
        System.out.println("Select the kind of service you want.");
        System.out.println("");
        System.out.println("A: Basic");
        System.out.println("B: Premium");
        System.out.println("C: Deluxe");
        System.out.println("D: Fiesta Deluxe");
        System.out.print("Letter of your choice: ");
        selectService = myService.nextLine();

        if (selectService.equals("A") || selectService.equals("a")) {
            valid = true;
            System.out.println("You've selected: Basic service");
        } else if (selectService.equals("B") || selectService.equals("b")) {
            valid = true;
            System.out.println("You've selected: Premium service");
        } else if (selectService.equals("C") || selectService.equals("c")) {
            valid = true;
            System.out.println("You've selected: Deluxe service");
        } else if (selectService.equals("D") || selectService.equals("d")) {
            valid = true;
            System.out.println("You've selected: Fiesta Deluxe service");
        } else {
            valid = false;
            System.out.println("Your choice is not available. Please try again.");
            System.out.println("");
        }
    }
    serv = selectService;
    return selectService;
}

}

相关问题