如何在 if 语句中留下一个 while 循环并继续执行 if 语句?
How do I leave a while loop which is within an if statement and carry on with the if statement?
我基本上是在用 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");
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");
}
}
}
}
int num=1;
if(num==1){
while(num<10){
num++;
if(num==5){
break;
}
}
System.out.println(num);
}
输出为 5
这是因为 break; 语句跳出了它能找到的最内层循环
1.I 可以建议您对每个要执行的操作使用不同的方法。通过这种方式你可以轻松地 return 到代码的任何部分,因为实际上它将是一个 return 到一个方法。
2.您不必多次启动Scanner,而只需在开始时启动一次。
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");
}
}
我基本上是在用 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");
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");
}
}
}
}
int num=1;
if(num==1){
while(num<10){
num++;
if(num==5){
break;
}
}
System.out.println(num);
}
输出为 5
这是因为 break; 语句跳出了它能找到的最内层循环
1.I 可以建议您对每个要执行的操作使用不同的方法。通过这种方式你可以轻松地 return 到代码的任何部分,因为实际上它将是一个 return 到一个方法。 2.您不必多次启动Scanner,而只需在开始时启动一次。 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");
}
}