2017-04-27 137 views
0
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

+0

@Jens不起作用它仍然給我「一站式」作爲輸出:/ –

+2

Madhurya,你的輸出不匹配你的代碼。不知何故,您正在運行的代碼與您在此顯示的代碼不同。我認爲你應該清理你的編譯目錄並重新編譯一切。 –

回答

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); 
+1

謝謝你的工作! –

相關問題