我没有得到以下代码的预期输出:
问题描述:
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);
}
}
我总是得到'停止'作为产品名称与最低价格的输出。我该如何解决这个问题?我没有得到以下代码的预期输出:
答
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);
+1
谢谢你的工作! –
@Jens不起作用它仍然给我“一站式”作为输出:/ –
Madhurya,你的输出不匹配你的代码。不知何故,您正在运行的代码与您在此显示的代码不同。我认为你应该清理你的编译目录并重新编译一切。 –