好的,稍微修改了代码,但是程序仍然无法正常工作。我希望能够在程序运行时在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");
}
}
}
还有其他想法吗?
答案 0 :(得分:1)
错误发生在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();
}
}
答案 1 :(得分:0)
您遇到的错误是正在读取字符串。
替换
String string = sc.next("fruit"); // Line 17
String string1 = sc.next("bananas"); // Line 26
String string2 = sc.next("apples"); // Line 30
String string3 = sc.next("oranges"); // Line 35
使用
String string = sc.nextLine(); // Line 17
String string1 = sc.nextLine(); // Line 26
String string2 = sc.nextLine(); // Line 30
String string3 = sc.nextLine(); // Line 35
如果要检查输入的String
,请使用以下代码:-
if(string.equals("fruit")){
// statements
}
答案 2 :(得分:0)
根据JavaDoc,您得到的InputMismatchException
是
由Scanner
抛出表示检索到的令牌没有 匹配预期类型的模式,或者令牌不足 预期类型的范围。
因此,如果您没有按照请求("fruit"
)来写String string = sc.next("fruit")
,则执行将因该异常而终止。因此,要修复您的代码,您应该替换
String string = sc.next("fruit");
类似
String string = sc.nextLine();
if ("fruit".equalsIgnoreCase(string)) { /* design your control flow as you want */
您可以将相同的规则应用于代码中存在的其他sc.next()
。
旁注:切勿将异常吞入catch
块中;始终记录它们。
答案 3 :(得分:0)
您对输入行的扫描是错误的。要在Java中扫描输入数据,您应该使用
Scanner in = new Scanner(System.in);
int a = in.nextInt(); // To input integer
char ch = in.nextChar(); // To input character
String str = in.nextLine(); // To get a complete line of input
String s = in.next(); //To get a single string
根据上述内容替换输入行。
干杯:)