Java 我的代码没有进入while循环

Java 我的代码没有进入while循环,java,while-loop,Java,While Loop,这是我代码的一部分,也是唯一不断出错的部分。即使它为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 kind

这是我代码的一部分,也是唯一不断出错的部分。即使它为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;
} 

在第6行,您声明了
boolean valid=true并且在代码后面从未将其设置为
false
,因此它不会进入while循环。

在第6行,您已声明
布尔有效=true并且在代码后面没有将其设置为
false
,因此它不会进入while循环。

根据您的代码,我理解您希望选择选项,如果该选项未被选中,请再次尝试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;
}

}

根据您的代码,我理解您希望选择选项,如果该选项未选择,请在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;
}

}

可能重置
valid=false在循环之前。我没有看到任何主方法。应该有
publicstaticvoidmain(String[]args){CateringBooking CateringBooking=newcateringbooking();cateringbookindofservice();}
,您不需要使用
=
在循环时签入
。只需使用属性。更改
valid=false
while(valid)
应该可以工作。可能重置
valid=false在循环之前。我没有看到任何主方法。应该有
publicstaticvoidmain(String[]args){CateringBooking CateringBooking=newcateringbooking();cateringbookindofservice();}
,您不需要使用
=
在循环时签入
。只需使用属性。更改
valid=false
while(有效)
应该可以工作。