Java 如何在if语句中留下while循环并继续执行if语句?

Java 如何在if语句中留下while循环并继续执行if语句?,java,bluej,Java,Bluej,本质上,我正在用blueJ制作一个购物应用程序,我偶然发现了一个问题。我已经做了和If语句,它允许用户1。商店2。检查篮3。去结帐,当我输入1完成我的购物并将我的东西添加到购物列表时,我似乎无法退出while循环。关于我将如何解决这个问题有什么好主意 import java.util.Scanner; import java.util.ArrayList; public class List { public String itemsList; ArrayList<Stri

本质上,我正在用blueJ制作一个购物应用程序,我偶然发现了一个问题。我已经做了和If语句,它允许用户1。商店2。检查篮3。去结帐,当我输入1完成我的购物并将我的东西添加到购物列表时,我似乎无法退出while循环。关于我将如何解决这个问题有什么好主意

import java.util.Scanner;
import java.util.ArrayList;
public class List
{
    public String itemsList;
    ArrayList<String> alist=new ArrayList<String>();
    public List()
    {
    }

    /*public String itemList()
    {
    System.out.println("1. Veg");
    System.out.println("2. sweets");
    System.out.println("3. drink");
    System.out.println("4. toiletries");
    return itemsList;
    }
     */

    public void Shopping()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("1. do shopping");
        System.out.println("2. check basket");
        System.out.println("3. go to checkout");
        String choice1 = sc.next();

        if(choice1.equals("1"))
        {
            boolean stop = false;
            while(!stop){
                Scanner scanner = new Scanner(System.in);
                System.out.println("1. Veg");
                System.out.println("2. sweets");
                System.out.println("3. drink");
                System.out.println("4. toiletries");
                System.out.println("5. alcohol");
                System.out.println("6. go back");
                String choice = scanner.next();
                if(choice.equals("1"))
                {
                    System.out.println("Veg has been added to your basket");
                    alist.add("veg");
                }else if(choice.equals("2"))
                {
                    System.out.println("sweets have been added to your basket");
                    alist.add("veg");
                }else if(choice.equals("3"))
                {
                    System.out.println("drinks have been added to your basket");
                    alist.add("veg");
                }else if(choice.equals("4"))
                {
                    System.out.println("toiletries have been added to your basket");
                    alist.add("veg");
                }else if(choice.equals("5"))
                {
                    Scanner scanner1 = new Scanner(System.in);
                    System.out.println("Check ID if age is under 25");
                    System.out.println("How old are you?");
                    int underAge = scanner1.nextInt();
                    int i;
                    i = 18;
                    if(underAge < i)
                    {
                        System.out.println("You are not old enough to buy alcohol");
                    } else{
                        System.out.println("Alcohol has been added to your basket");
                        alist.add("veg");
                    }
                }else if(choice.equals("6"))
                {

                }

            }
        }else if(choice1.equals("2")){
            System.out.println(alist);
        }else if(choice1.equals("3")){
            System.out.println("Are you sure you want to checkout? ");
            System.out.println("1. No");
            System.out.println("2. Yes");
            Scanner scanner3 = new Scanner(System.in);
            String checkout = scanner3.next();
            if(checkout.equals("1"))
            {
                return;
            } else {
                System.out.println("test");
            }

        }
    }
}
import java.util.Scanner;
导入java.util.ArrayList;
公共班级名单
{
公共字符串项列表;
ArrayList alist=新的ArrayList();
公开名单()
{
}
/*公共字符串itemList()
{
System.out.println(“1.Veg”);
System.out.println(“2.sweets”);
系统输出打印(“3.饮料”);
系统输出打印(“4.化妆品”);
返回项目列表;
}
*/
公众购物()
{
扫描仪sc=新的扫描仪(System.in);
System.out.println(“1.购物”);
System.out.println(“2.检查篮”);
System.out.println(“3.去结帐”);
String choice1=sc.next();
如果(选择1.等于(“1”))
{
布尔停止=假;
当(!停止){
扫描仪=新的扫描仪(System.in);
System.out.println(“1.Veg”);
System.out.println(“2.sweets”);
系统输出打印(“3.饮料”);
系统输出打印(“4.化妆品”);
系统输出打印(“5.酒精”);
System.out.println(“6.返回”);
字符串选择=scanner.next();
if(选择等于(“1”))
{
System.out.println(“蔬菜已添加到您的篮子中”);
添加(“素食”);
}else if(选择等于(“2”))
{
System.out.println(“糖果已添加到您的篮子中”);
添加(“素食”);
}else if(选择等于(“3”))
{
System.out.println(“饮料已添加到您的篮子中”);
添加(“素食”);
}else if(选择等于(“4”))
{
System.out.println(“化妆品已添加到您的购物篮中”);
添加(“素食”);
}else if(选择等于(“5”))
{
扫描仪scanner1=新扫描仪(System.in);
System.out.println(“如果年龄在25岁以下,请检查ID”);
System.out.println(“你多大了?”);
int underAge=scanner1.nextInt();
int i;
i=18;
if(未成年
int num=1;
如果(num==1){

while(num1)。我可以建议您对您想要执行的每个操作使用单独的方法。通过这种方式,您可以轻松地返回到代码的任何部分,因为实际上它将返回到一个方法。 2.您不必多次启动扫描仪,只需在开始时启动一次。 3.您可以通过将常见或类似的操作组合到一个方法中来优化代码。 例如,您的代码可以通过以下方式进行修改:

import java.util.ArrayList;
import java.util.Scanner;

public class List{

    static ArrayList<String> alist = new ArrayList<String>();
    static Scanner sc = new Scanner(System.in);
    static String choice;

public static void main(String args[]){
    mainMenue();
}

private static void mainMenue() {
     System.out.println("1. do shopping \n2. check basket \n3. go to checkout");
     choice = sc.next();
     if(choice.equals("1")) shoppingMenue();
     else if(choice.equals("2")) checkingMenue();
     else if(choice.equals("3")) checkoutMenue();
}

private static void checkoutMenue() {
    System.out.println("Are you sure you want to checkout? \n1. No \n2. Yes");
    choice = sc.next();
    if(choice.equals("1"))return;
    else System.out.println("test");
}

private static void checkingMenue() {
    System.out.println(alist);
}

private static void shoppingMenue() {
    boolean stop = false;
    while(!stop){
        System.out.println("1. Veg \n2. sweets \n3. drink \n4. toiletries \n5. 
alcohol \n6. go back");
        choice = sc.next();
        if(choice.equals("1")) printAndSave(1);
        else if(choice.equals("2")) printAndSave(2);
        else if(choice.equals("3")) printAndSave(3);
        else if(choice.equals("4")) printAndSave(4);
        else if(choice.equals("5")) {
            System.out.println("Check ID if age is under 25 \nHow old are you?");
            if(sc.nextInt() < 18) System.out.println("You are not old enough to buy 
alcohol");
            else printAndSave(5);
        }
        else if(choice.equals("6")) mainMenue();
    }
}

private static void printAndSave(int i) {
    ArrayList <String> textList = new ArrayList <>(6);
    textList.add(0, "");
    textList.add(1, "Veg has been added to your basket");
    textList.add(2, "Sweets have been added to your basket");
    textList.add(3, "Drinks have been added to your basket");
    textList.add(4, "Toiletries have been added to your basket");
    textList.add(5, "Alcohol has been added to your basket");

    System.out.println(textList.get(i));
    alist.add("veg");
    }
}
import java.util.ArrayList;
导入java.util.Scanner;
公共班级名单{
静态ArrayList alist=新的ArrayList();
静态扫描仪sc=新扫描仪(System.in);
静态字符串选择;
公共静态void main(字符串参数[]){
mainMenue();
}
私有静态void mainMenue(){
System.out.println(“1.购物\n2.检查购物篮\n3.结帐”);
choice=sc.next();
如果(choice.equals(“1”))shoppingMenue();
else如果(choice.equals(“2”)选中menue();
如果(choice.equals(“3”))checkoutMenue();
}
私有静态void checkoutMenue(){
System.out.println(“您确定要签出吗?\n1.否\n2.是”);
choice=sc.next();
如果(选择等于(“1”))返回;
else System.out.println(“测试”);
}
私有静态无效检查菜单(){
System.out.println(列表);
}
私有静态void shoppingMenue(){
布尔停止=假;
当(!停止){
System.out.println(“1.蔬菜\n2.糖果\n3.饮料\n4.化妆品\n5。
酒精\n6.回去“;
choice=sc.next();
如果(选择等于(“1”))打印并保存(1);
否则,如果(选择等于(“2”))打印并保存(2);
如果(选择等于(“3”))打印并保存(3);
否则,如果(选择等于(“4”))打印并保存(4);
else if(选择等于(“5”)){
System.out.println(“如果年龄在25岁以下,请检查ID\n您几岁?”);
if(sc.nextInt()<18)System.out.println(“您的年龄还不足以购买
酒精);
else打印并保存(5);
}
else如果(choice.equals(“6”)mainMenue();
}
}
私有静态无效打印和保存(int i){
ArrayList textList=新的ArrayList(6);
文本列表。添加(0,“”);
textList.add(1,“蔬菜已添加到您的篮子中”);
textList.add(2,“糖果已添加到您的篮子中”);
添加(3),“您已经添加了饮料
import java.util.ArrayList;
import java.util.Scanner;

public class List{

    static ArrayList<String> alist = new ArrayList<String>();
    static Scanner sc = new Scanner(System.in);
    static String choice;

public static void main(String args[]){
    mainMenue();
}

private static void mainMenue() {
     System.out.println("1. do shopping \n2. check basket \n3. go to checkout");
     choice = sc.next();
     if(choice.equals("1")) shoppingMenue();
     else if(choice.equals("2")) checkingMenue();
     else if(choice.equals("3")) checkoutMenue();
}

private static void checkoutMenue() {
    System.out.println("Are you sure you want to checkout? \n1. No \n2. Yes");
    choice = sc.next();
    if(choice.equals("1"))return;
    else System.out.println("test");
}

private static void checkingMenue() {
    System.out.println(alist);
}

private static void shoppingMenue() {
    boolean stop = false;
    while(!stop){
        System.out.println("1. Veg \n2. sweets \n3. drink \n4. toiletries \n5. 
alcohol \n6. go back");
        choice = sc.next();
        if(choice.equals("1")) printAndSave(1);
        else if(choice.equals("2")) printAndSave(2);
        else if(choice.equals("3")) printAndSave(3);
        else if(choice.equals("4")) printAndSave(4);
        else if(choice.equals("5")) {
            System.out.println("Check ID if age is under 25 \nHow old are you?");
            if(sc.nextInt() < 18) System.out.println("You are not old enough to buy 
alcohol");
            else printAndSave(5);
        }
        else if(choice.equals("6")) mainMenue();
    }
}

private static void printAndSave(int i) {
    ArrayList <String> textList = new ArrayList <>(6);
    textList.add(0, "");
    textList.add(1, "Veg has been added to your basket");
    textList.add(2, "Sweets have been added to your basket");
    textList.add(3, "Drinks have been added to your basket");
    textList.add(4, "Toiletries have been added to your basket");
    textList.add(5, "Alcohol has been added to your basket");

    System.out.println(textList.get(i));
    alist.add("veg");
    }
}