(java)是否可以从同一行中读取用户2的值,一个字符串和另一个字符串?

时间:2017-04-17 08:26:06

标签: java loops

这个程序的想法是继续向用户读取产品名称和它的价格在同一行,直到用户输入停止,然后计算用户是否有足够的钱。但输入两行后我收到错误,我写的(请帮忙):

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)

1 个答案:

答案 0 :(得分: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'然后拆分并保留更容易,因为如果分隔符是你不能买的空间,例如“苹果派”^^
  • 并在印刷品中添加了产品清单
  • 的印刷品

如果您需要更多,请在评论中告诉我,可以在输入处添加一个检查来验证拼写错误名称 - 价格是否验证,然后使用Map <查找最小/最大值也很容易/ p>

//'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);