我没有获得以下代码的预期输出:

时间:2017-04-27 10:29:34

标签: java

import java.util.Scanner;

public class Main {

public static void main (String[]args) {
    Scanner input = new Scanner(System.in);
    int n, money;
    String prodName = null;
    String minProdName = null;
    double price = 0.0;
    double total = 0.0;
    double min = 3000.0;

    System.out.println ("How much money do you have?");
    money = input.nextInt();

    System.out.println ("Please, insert the items in the invoice (the name of the product and its price) or enter stop to finish billing " );
     prodName = input.next();

    while (!(prodName.equals("stop")))
    {
        price = input.nextDouble();
        prodName = input.next();
        total = total +price;
    if (price<min && !(prodName.equals("stop")))
        {  
            min = price;
           minProdName = prodName;
        }

    }  
    if (total<money)
             System.out.println("You have enough money");
             System.out.println();

System.out.printf(" %s is the item with the minimum price(which is KD %.3f)", min);

}
}

我总是以“最低价格”作为产品名称的输出“停止”。我该如何解决这个问题?

预期产出:
Expected Output

我的输出:
My Output

1 个答案:

答案 0 :(得分:3)

System.out.printf(" %s is the item with the minimum price(which is KD %.3f)", min);

这里只传递一个值来打印。你应该在这里传递两个值。首先是String,第二个是Double

所以将此行更改为

System.out.printf(" %s is the item with the minimum price(which is KD %.3f)", minProdName, min);