[英]Can't get this Java program to work
好的,稍微更改了代碼,但是程序仍然無法正常運行。 我希望能夠在程序運行時在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();
}
}
您有錯誤是正在讀取字符串。
更換
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
}
根據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
塊; 始終記錄它們。
輸入行掃描錯誤。要使用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
根據以上給出的內容替換您的輸入行。
干杯:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.