gpt4 book ai didi

java - 有人可以帮我处理返回函数吗?

转载 作者:行者123 更新时间:2023-12-02 09:06:41 24 4
gpt4 key购买 nike

我真的是编码初学者。对于学校,我需要创建一个“在线商店”,用户可以在其中添加、删除、查看订单或结账。我创建了用于添加和删除的返回函数,因为在添加或删除项目后我必须返回更新的“购物车”。问题就在这里。

public static void main(String[] args){
Scanner key = new Scanner(System.in);
int arr[] = new int[5];
int task, Item, Add, Remove;

System.out.println("Welcome to Ye Old Shoppe!\nWe sell items at the lowest price in town!\nDue to prices being so low, we sell only 5 items\nevery day and the items change every day!");
System.out.println("For today, we are selling:");

System.out.println("\n1. Add Items\n2. Remove Items\n3. View Order\n4. Checkout");
System.out.print("> ");
task = key.nextInt();

if(task == 1){
System.out.println("\nWhich item do you want to add?");
System.out.println("\n1. Premium fidget spinners($25)\n2. Nintendo Switch($295)\n3. Handmade leather wallet($65)\n4. 2 packages of A4 paper($50)\n5. Bluetooth Earbuds($40)");
System.out.print("> ");
Item = key.nextInt();
System.out.println("\nHow many?");
System.out.print("> ");
Add = key.nextInt();
addItems(arr, Item, Add);
}else if(task == 2){
System.out.println("\nWhich item do you want to remove?");
System.out.println("\n1. Premium fidget spinners($25)\n2. Nintendo Switch($295)\n3. Handmade leather wallet($65)\n4. 2 packages of A4 paper($50)\n5. Bluetooth Earbuds($40)");
System.out.print("> ");
Item = key.nextInt();
System.out.println("\nHow many?");
System.out.print("> ");
Remove = key.nextInt();
removeItems(arr, Item, Remove);
}else if(task == 3){
viewOrder(arr);
}
}
public static int[] addItems(int[] cart, int item, int addNum){
if(item == 1){
while(item > 0 && item < 6 && addNum > 0 && addNum < 6){
for(int i = 0; i < addNum; i++){
cart = ArrayUtils.add(cart,1);
return cart;
}
}
System.out.println("Invalid chocie");
}else if(item == 2){
while(item > 0 && item < 6 && addNum > 0 && addNum < 6){
for(int i = 0; i < addNum; i++){
cart = ArrayUtils.add(cart,1);
return cart;
}
}
System.out.println("Invalid chocie");
}else if(item == 3){
while(item > 0 && item < 6 && addNum > 0 && addNum < 6){
for(int i = 0; i < addNum; i++){
cart = ArrayUtils.add(cart,1);
return cart;
}
}
System.out.println("Invalid chocie");
}else if(item == 4){
while(item > 0 && item < 6 && addNum > 0 && addNum < 6){
for(int i = 0; i < addNum; i++){
cart = ArrayUtils.add(cart,1);
return cart;
}
}
System.out.println("Invalid chocie");
}else if(item == 5){
while(item > 0 && item < 6 && addNum > 0 && addNum < 6){
for(int i = 0; i < addNum; i++){
cart = ArrayUtils.add(cart,1);
return cart;
}
}
System.out.println("Invalid chocie");
}
return cart;
}

将商品添加到购物车后,我想返回到 main 方法中的主菜单,程序会再次询问我是否要添加、删除、查看订单或结账。我该怎么做??

最佳答案

要连续获取输入,请使用它。

public static void main(String[] args)
{
Scanner key = new Scanner(System.in);

while(true)
{
String input = scanner. nextLine();//take user input
if(input.equals("add"))
{
int cartUpdated[]=addItems( cart, item, addNum);
}
else if(input.equals("remove"))
{
//call remove method
}
else if(input.equals("quit"))
{
System.exit(0);//exit(0) : Generally used to indicate successful termination.
}
}

}

关于java - 有人可以帮我处理返回函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59764637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com