2017-04-17 20 views
-2

這個程序的想法是不斷地從用戶那裏讀取一個產品名稱,它的價格在同一行中,直到用戶類型停止,然後計算用戶是否有足夠的資金。但我輸入兩條線後得到一個錯誤,這是我寫的(請大家幫忙):(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) 
+0

你爲什麼要問產品的名稱?因爲你從不使用它,所以它沒用嗎? – azro

+0

好吧,我以後會使用它,因爲我也希望程序顯示最低價格的名稱產品。我會在稍後補充。 –

+0

我只想知道如何讓程序在同一行(String,double)中取兩個值而不會崩潰。 –

回答

0
public static void main (String[] args){ 
     Scanner input = new Scanner(System.in); 
     // Q1 
     System.out.print("How much money do you have? "); 
     double money = Double.valueOf(input.nextLine()); 
     HashMap<String,Double> products = new HashMap<String,Double>(); 
     //Q2 
     double total = 0; 
     String[] product; 
     String line=""; 
     Scanner input1 = new Scanner(System.in); 
     System.out.println("Please,insert the items in the invoice (this typo : 'name-price'):\n" 
       + "Insert \"stop\" as the name product to finish your input "); 
     while (!(line = input1.nextLine()).equalsIgnoreCase("stop")){ 
      product = line.split("-"); 
      products.put(product[0], Double.valueOf(product[1])); 
      total += Double.valueOf(product[1]); 
      System.out.println("Another one ?"); 
     } 
     if (total <= money){ 
      System.out.println("you have enough money for "+products); 
     }else{ 
      System.out.println("you don\'t have enough money for "+products); 
     } 
     input1.close(); 
    }  

所以薩姆改進:

  • 我添加一個HashMap來存儲對產品名稱/價格,HashMap的是存儲對
  • 最便捷的方式
  • 在while條件1分的事情是由:輸入存儲在line和檢查它是否是「停止」,因爲你的代碼,你需要2個輸入,每次需要一個線時間也各產品之間的寫入停止的不同
  • 我建議你像'name - price'這樣問,那麼分離和保存就更容易了,因爲如果分隔符是一個你不能購買的空間,例如「apple pie」^^
  • 並且在打印中添加了打印產品清單

如果您需要更多信息,請在評論中告訴我,例如可以在輸入處添加一個檢查以驗證錯誤名稱價格是驗證,然後找到最小/最大也很容易與地圖

//'Classic' method for min price without using complex notions 
double min = Double.MAX_VALUE; 
//iterate over the prices 
for(Double price : products.values()){ 
    min = Math.min(min, price); 
} 
System.out.println("min is "+min); 

//Second method with Stream (allow to iterate easliy) 
double minBis = products.values().stream().min(Comparator.naturalOrder()).get(); 
System.out.println("mins is "+minBis); 
+0

非常感謝您azro :) 之前,雖然我從來沒有使用HashMap的,但我會讀到它知道究竟怎麼操作。 –

+0

與使用HashMap時獲得最大值或最小值有什麼不同? –

+0

我已經添加了2種方法來獲取最小值,如果您滿意,請不要猶豫,接受答案或投票 – azro

相關問題