猿问

如何离开 if 语句中的 while 循环并继续执行 if 语句?

本质上,我正在用 blueJ 制作一个购物应用程序,但我偶然发现了一个问题。我已经做出了 if 语句,它允许用户 1. 购物 2. 检查购物篮 3. 去结账,当我输入 1 来完成我的购物并将我的东西添加到购物清单时,我似乎无法退出内循环。关于如何解决这个问题的任何想法?


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");

    }

}



HUX布斯
浏览 212回答 2
2回答

万千封印

1.我可以推荐你对你想做的每一个操作使用不同的方法。通过这种方式,您可以轻松地返回代码的任何部分,因为实际上它会返回一个方法。2.您不必多次启动扫描仪,而在开始时只需一次。3.您可以通过将常见或相似的操作组合成一种方法来优化您的代码。例如,您的代码可以通过以下方式修改:import java.util.ArrayList;import java.util.Scanner;public class List{&nbsp; &nbsp; static ArrayList<String> alist = new ArrayList<String>();&nbsp; &nbsp; static Scanner sc = new Scanner(System.in);&nbsp; &nbsp; static String choice;public static void main(String args[]){&nbsp; &nbsp; mainMenue();}private static void mainMenue() {&nbsp; &nbsp; &nbsp;System.out.println("1. do shopping \n2. check basket \n3. go to checkout");&nbsp; &nbsp; &nbsp;choice = sc.next();&nbsp; &nbsp; &nbsp;if(choice.equals("1")) shoppingMenue();&nbsp; &nbsp; &nbsp;else if(choice.equals("2")) checkingMenue();&nbsp; &nbsp; &nbsp;else if(choice.equals("3")) checkoutMenue();}private static void checkoutMenue() {&nbsp; &nbsp; System.out.println("Are you sure you want to checkout? \n1. No \n2. Yes");&nbsp; &nbsp; choice = sc.next();&nbsp; &nbsp; if(choice.equals("1"))return;&nbsp; &nbsp; else System.out.println("test");}private static void checkingMenue() {&nbsp; &nbsp; System.out.println(alist);}private static void shoppingMenue() {&nbsp; &nbsp; boolean stop = false;&nbsp; &nbsp; while(!stop){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("1. Veg \n2. sweets \n3. drink \n4. toiletries \n5.&nbsp;alcohol \n6. go back");&nbsp; &nbsp; &nbsp; &nbsp; choice = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; if(choice.equals("1")) printAndSave(1);&nbsp; &nbsp; &nbsp; &nbsp; else if(choice.equals("2")) printAndSave(2);&nbsp; &nbsp; &nbsp; &nbsp; else if(choice.equals("3")) printAndSave(3);&nbsp; &nbsp; &nbsp; &nbsp; else if(choice.equals("4")) printAndSave(4);&nbsp; &nbsp; &nbsp; &nbsp; else if(choice.equals("5")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Check ID if age is under 25 \nHow old are you?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(sc.nextInt() < 18) System.out.println("You are not old enough to buy&nbsp;alcohol");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else printAndSave(5);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(choice.equals("6")) mainMenue();&nbsp; &nbsp; }}private static void printAndSave(int i) {&nbsp; &nbsp; ArrayList <String> textList = new ArrayList <>(6);&nbsp; &nbsp; textList.add(0, "");&nbsp; &nbsp; textList.add(1, "Veg has been added to your basket");&nbsp; &nbsp; textList.add(2, "Sweets have been added to your basket");&nbsp; &nbsp; textList.add(3, "Drinks have been added to your basket");&nbsp; &nbsp; textList.add(4, "Toiletries have been added to your basket");&nbsp; &nbsp; textList.add(5, "Alcohol has been added to your basket");&nbsp; &nbsp; System.out.println(textList.get(i));&nbsp; &nbsp; alist.add("veg");&nbsp; &nbsp; }}

MMTTMM

&nbsp; &nbsp; int num=1;&nbsp; &nbsp; if(num==1){&nbsp; &nbsp; &nbsp; &nbsp; while(num<10){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(num==5){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(num);&nbsp; &nbsp; }输出为 5这是因为休息;语句跳出它可以找到的最内层循环
随时随地看视频慕课网APP

相关分类

Java
我要回答