gpt4 book ai didi

java - 无法让这个 Java 程序运行

转载 作者:行者123 更新时间:2023-12-02 11:03:57 25 4
gpt4 key购买 nike

好吧,稍微改变了代码,但程序仍然无法正常运行。我希望能够在程序运行时在java控制台中输入产品类型(水果),输入任何类型的水果(香蕉、苹果或橙子),然后输入数量。

import java.util.*;

public class demo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String str[] = { "Bananas", "Apples", "Oranges" };
double price[] = { 2.09, 2.59, 2.25 };
int i = 0;
int j = 0;

System.out.print("Enter type of product: ");
String string = sc.nextLine();
if ("fruit".equals(string)) {
while (i < str.length) {
while (j < price.length) {
System.out.print(str[i++] + ": " + "£" + (price[j++]) + "p per bag \n");

}
}
}
System.out.print("\n");
System.out.print("Enter which type of " + string + ": ");

String string1 = sc.nextLine();

boolean strs = "bananas".equals(string1);
boolean strs1 = "apples".equals(string1);
boolean strs2 = "oranges".equals(string1);
if (strs) {
System.out.print("Enter qty of " + str[0] + " (by bag): ");
}

if (strs1) {
System.out.print("Enter qty of " + str[1] + " (by bag): ");

}
if (strs2) {
System.out.print("Enter qty of " + str[2] + " (by bag): ");
}

int qty = sc.nextInt();
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int h = 1;
if ((a[h] == (qty)) && (strs) || (strs1) || (strs2)){
System.out.print("\n");
System.out.print(qty + " bag(s) of " + string1 + " have been added to your basket, " + "total costing £"
+ (qty) * price[0] + "p");

}
}
}

还有更多想法吗?

最佳答案

错误出现在 String string = sc.next("fruit");要么将其更改为 sc.nextLine() 或类似下面的内容

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String str[] = { "Bananas", "Apples", "Oranges" };
double stk[] = { 1.09, 1.59, 1.25 };
int i = 0;
int j =0;

try {
System.out.print("Enter type of product: ");
String string = sc.next();

while (i < str.length) {
while (j < stk.length) {
System.out.print(str[i++] + ": " + "£" + (stk[j++]) + "p per bag \n");

}
}
System.out.print("\n");
System.out.print("Enter which type of "+string+": ");
String string1 = sc.next();
if(string1 != null) {
System.out.print("Enter qty of "+string1+ "(per bag) \n");
}
String string2 = sc.next();
if(string2 != null) {
System.out.print("Enter qty of " +string2+ "(in lbs) \n");

}
String string3 = sc.next();
if (string3 != null) {
System.out.print("Enter qty of " +string3+ "(in lbs) \n");

}

} catch (Exception e) {
System.out.println("Eror");
e.printStackTrace();
}
}

关于java - 无法让这个 Java 程序运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51121324/

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