Can';我不能让这个Java程序工作
好的,对代码做了一些修改,但是程序仍然不能正常工作。我希望在程序运行时能够在java控制台中输入产品类型(水果),输入任何类型的水果(香蕉、苹果或桔子),然后输入数量Can';我不能让这个Java程序工作,java,Java,好的,对代码做了一些修改,但是程序仍然不能正常工作。我希望在程序运行时能够在java控制台中输入产品类型(水果),输入任何类型的水果(香蕉、苹果或桔子),然后输入数量 import java.util.*; public class demo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str[] = { "Bananas", "Apples"
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");
}
}
}
import java.util.*;
公开课演示{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串str[]={“香蕉”、“苹果”、“橙子”};
双倍价格[]={2.09,2.59,2.25};
int i=0;
int j=0;
系统输出打印(“输入产品类型:”);
String String=sc.nextLine();
如果(“水果”。等于(字符串)){
而(i
还有什么想法吗?你的错误是读字符串 替换
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
与
如果要检查输入的字符串
,请使用以下代码:-
if(string.equals("fruit")){
// statements
}
错误位于String=sc.next(“水果”);将其更改为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();
}
}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串str[]={“香蕉”、“苹果”、“橙子”};
双stk[]={1.09,1.59,1.25};
int i=0;
int j=0;
试一试{
系统输出打印(“输入产品类型:”);
String String=sc.next();
而(i
您将得到一个输入不匹配异常
,根据JavaDoc,它是
由扫描器
抛出,以指示检索到的令牌不存在
匹配所需类型的模式,或者标记已过期
所需类型的范围
因此,如果您没有按照请求编写“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
块;始终记录它们。您对输入行的扫描是错误的。要用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
按照上述说明更换输入线
干杯:)错误确切地说是什么?我放了一个catch异常e,它允许我打印自己的错误消息(如果有意义的话)String String=sc.next(“水果”);如果您想打印e,而不是为了打印错误而忽略它,请修复此错误。您可以将e放入打印消息中,也可以使用e.printstacktrace()。这将为您提供更多关于正在发生的事情的信息;我添加了一个
if
,只是为了证明您可以设计所需的流。
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