這個程序的想法是不斷地從用戶那裏讀取一個產品名稱,它的價格在同一行中,直到用戶類型停止,然後計算用戶是否有足夠的資金。但我輸入兩條線後得到一個錯誤,這是我寫的(請大家幫忙):(java)是否有可能從同一行中的用戶2值,一個字符串和另一個double讀取?
import java.util.Scanner;
public class AA_152 {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.print("How much money do you have? ");
double money = input.nextDouble();
double price;
double total = 0;
String product;
Scanner input1 = new Scanner(System.in);
System.out.println("Please, Insert the items in the invoice (the name product
and its price):\n"
+ "Insert \"stop\" as the name product to finish your input ");
while (!input1.next().equals("stop")){
product = input1.next();
price = input1.nextDouble();
total += price;
}
if (total <= money)
System.out.println("you have enough money");
else
System.out.println("you don\'t have enough money");
}
}
這是我得到的錯誤,當我運行程序:
How much money do you have? 100
Please, Insert the items in the invoice (the name product and its price):
Insert "stop" as the name product to finish your input
orange 12.200
water 15.400
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at AlaaAwad_152306.main(AlaaAwad_152306.java:27)
C:\Users\Alaa\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53:
Java returned: 1
BUILD FAILED (total time: 16 seconds)
你爲什麼要問產品的名稱?因爲你從不使用它,所以它沒用嗎? – azro
好吧,我以後會使用它,因爲我也希望程序顯示最低價格的名稱產品。我會在稍後補充。 –
我只想知道如何讓程序在同一行(String,double)中取兩個值而不會崩潰。 –