无法继续使该程序正常运行。大部分都能使用,请参见下面的代码:
import java.io.*;
import java.util.*;
public class demo {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String str[] = { "Bananas", "Apples", "Oranges" };
double price[] = { 2.09, 3.99, 2.19};
int i = 0;
int j = 0;
System.out.print("Enter type of product: ");
String string = sc.next();
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.next();
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;
int[] g = {0,1,2};
if ((a[h] == (qty)) || (strs) || (strs1) || (strs2)) {
System.out.print("\n"+ qty + " bag(s) of " + string1 + " have been added to your basket, "
+ "total costing £"+ (qty) * (price[g[0]]) + "p");
}
}
}
我如何才能使此代码起作用……任何想法?(例如,产品的类型(水果),水果的类型(香蕉或苹果或橙子)以及数量(介于1到10之间) ,但是在计算总成本时,却出现错误...
答案 0 :(得分:0)
这是一种逻辑。
double itemCost = 0;
if (strs){
System.out.print("Enter qty of " + str[0] + " (by bag): ");
itemCost = price[0];
}
if (strs1) {
System.out.print("Enter qty of " + str[1] + " (by bag): ");
itemCost = price[1];
}
if (strs2) {
System.out.print("Enter qty of " + str[2] + " (by bag): ");
itemCost = price[2];
}
int qty = sc.nextInt();
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int h = 1;
int[] g = {0,1,2};
if ((a[h] == (qty)) || (strs) || (strs1) || (strs2)) {
System.out.print("\n"+ qty + " bag(s) of " + string1 + " have been added to your basket, "
+ "total costing £"+ (qty) * (itemCost) + "p");