2013-09-26 41 views
-2

我試圖採取少於1.00美元但大於零的變化量並顯示進行該更改所需的四分之一,十美分,五分硬幣和便士的數量。輸出必須以類似於英語的描述性方式呈現,具有以下格式規則:顯示更改java

如果數量爲零,則應使用單詞「否」。 如果金額爲1,則單位必須是單數。 如果金額不是1,則單位必須是複數。

此外,當輸入0或1以下的任何內容時,程序應該捕獲用戶的錯誤,並進入數據驗證循環以顯示提示並要求用戶重新輸入金額。此循環將繼續,直到用戶輸入的金額低於$ 1.00,但大於零。

當我運行我的代碼時,我不斷得到。

感謝您的時間!並非常感謝任何幫助!

我迄今爲止代碼:

import java.util.Scanner; 

    public class CoinChange 
    { 
     public static void main(String[] args) 
     {    
      double amt; 
      int cents, quarter, dime, nickle, penny; 

      Scanner keyboard = new Scanner(System.in); 

      System.out.print("Change in Coins\n" + 
          "----------------\n\n"); 

      System.out.println("Enter the amount less than $1.00, but\n" + 
          "more than zero."); 

      System.out.print("\nEnter amount: "); 
        amt = keyboard.nextInt(); 

    // ----------------------------------------------    

      cents = (int)(amt*100 + .1); 

      quarter = cents/25; 
      cents %= 25; 

      dime = cents/10; 
      cents %= 10; 

      nickle = cents/5; 
      cents %= 5; 

      penny = cents; 

    // ----------------------------------------- 

      while(amt > 0 && amt <= 1) 
      { 

    // ----------------------------------------- 

      if(quarter == 0) 
      { 
       System.out.print("no "); 
      } 
      else 
       System.out.print(quarter); 
      } 
      if(quarter == 1) 
      { 
       System.out.print("quarter"); 
      } 
      else 
      { 
       System.out.print("quarters"); 
      } 



      }   
    } 
+2

你希望這行會做什麼:'while(amt> 0 && amt <= 1)'? – ajb

+0

下一個問題:你想要'amt'是什麼樣的值?你是怎麼聲明'amt'的? – ajb

+0

的變化量小於1.00美元,但大於零@ajb – Smith

回答

2

您已經聲明amtint改用float

float amt = 0.0; 
int cents, quarter, dime, nickle, penny; 

// Get amt from user between 0.0 and 1.0 

cents = (int)amt*100; 

quarter = cents/25; 
cents -= quarter*25; 

dime = cents/10; 
cents -= dime*10; 

nickel = cents/5; 
cents -= nickel*5; 

penny = cents; 

// Then print out the numbers of each 

/* 
if amt = 0.91 

cents = 91 

quarter = 3 
cents = 16 

dime = 1 
cents = 6 

nickel = 1 
cents = 1 

penny = 1 
*/ 

使用模%

float amt = 0.0; 
int cents, quarter, dime, nickle, penny; 

// Get amt from user between 0.0 and 1.0 

cents = (int)amt*100; 

quarter = cents/25; 
cents = cents % 25; 

dime = cents/10; 
cents = cents % 10; 

nickel = cents/5; 
cents %= 5; 

penny = cents; 

// Then print out the numbers of each 

完整程序

import java.util.Scanner; 

public class CoinChange { 
    public static void main(String[] args) { 
     float amt; 
     int cents, quarter, dime, nickle, penny; 

     Scanner keyboard = new Scanner(System.in); 

     System.out.print("Change in Coins\n" + "----------------\n\n"); 

     System.out.println("Enter the amount less than $1.00, but\n" 
       + "more than zero."); 

     System.out.print("\nEnter amount: "); 

     amt = keyboard.nextFloat(); 
     keyboard.close(); 

     cents = (int) (amt * 100); 

     quarter = cents/25; 
     cents = cents % 25; 

     dime = cents/10; 
     cents = cents % 10; 

     nickle = cents/5; 
     cents %= 5; 

     penny = cents; 

     System.out.println("$" + amt + " = " + quarter + " quarters, " + dime 
       + " dimes, " + nickle + " nickles and " + penny + " pennies"); 

    } 
} 

測試它:http://ideone.com/4Xq466

+0

它說失去了精度,如果我只留下一個0它仍然會重複4次 – Smith

+0

@史密斯現在退房。 – TheKojuEffect

+0

我必須使用模運算符來將餘下的美分作爲規格的一部分。 – Smith

0

我的猜測是,你在進入數字像1.05,1.15,等等。如果你輸入的AMT,你乘以100(以獲取總美分)並添加.1(不知道爲什麼你這樣做)。然後當你真的想要一個雙精度的時候投入int。既然你投入了一個int,那麼美分/ 25通常會是4,例如115/25將四次除以剩下的剩餘部分,因此爲什麼你一直得到4作爲答案。

+0

我試圖只輸入0 - $ 1.00之間的數字 – Smith

+0

@Smith無論你需要投入一個雙精度分割數字。 –

+0

他只需要整數,這就是爲什麼他乘以100.他加0.1以試圖使其超過零作爲輸入。 – Aaron

0

您的代碼正常工作。唯一的問題是你在最後被卡在一個循環中,因爲amt始終是原始數量,所以它永遠輸出輸出。

如果你想輸出,例如,

季度 季度 一分錢一分錢 一分錢

,那麼你需要爲你去遞減AMT。

編輯**好吧......它看起來很奇怪,就像您剛剛計算使用的宿舍數量一樣。但問題是一樣的。我不知道爲什麼你甚至需要一個循環。

+0

這就是我的壞我還沒有進入其他部分。但循環用於在用戶輸入高於1.00的任何內容時捕獲錯誤,並且應該進入數據驗證循環以顯示提示並要求用戶重新輸入金額。此循環將繼續,直到用戶輸入的金額低於$ 1.00,但大於零。 – Smith

+0

我在調試模式下運行了你的代碼,並且正確地計算了四分之一個鎳角錢等。然後,它只是卡在循環打印出222222222222222 ....等,因爲有2個季度,當我輸入0.68 – Aaron

0

Scanner.nextInt()返回一個int。如果您希望用戶輸入一個整數的便士(1到99),那麼您應該將其分配給美分。你對amt感到困惑。

+0

然後我把'amt'放在這部分代碼 '美分=(int)amt * 100;' – Smith

0

您可以使用keyboard.nextDouble()。您應該使用BigDecimal進行貨幣比較。試試下面的代碼:

public class CoinChange {  

    public static void main(String[] args) {  
     double amt = getValidInputFromUser(); 

     int remainingCents = (int) (amt * 100);   

     int quarters = remainingCents/25; 
     remainingCents %= 25; 

     int dimes = remainingCents/10; 
     remainingCents %= 10; 

     int nickles = remainingCents/5; 
     remainingCents %= 5; 

     int pennies = remainingCents; 

     System.out.println(String.format(
        "Quarters %d, Dimes %d, Nickles %d, Cents %d", quarters, 
        dimes, nickles, pennies));  
    } 


    private static double getValidInputFromUser() { 
     double amt = BigDecimal.ZERO.doubleValue(); 
     do { 
      Scanner keyboard = new Scanner(System.in); 
      System.out.print("Change in Coins\n" + "----------------\n\n"); 
      System.out.println("Enter the amount less than $1.00, but\n" 
        + "more than zero."); 
      System.out.print("\nEnter amount: "); 
      amt = keyboard.nextDouble(); 
     } while (amountGreaterThanOne(amt) || amountIsZero(amt)); 
     return amt; 
    } 


    private static boolean amountIsZero(double amt) { 
     return (BigDecimal.valueOf(amt).compareTo(BigDecimal.ZERO) == 0); 
    } 


    private static boolean amountGreaterThanOne(double amt) { 
     return BigDecimal.valueOf(amt).compareTo(BigDecimal.ONE) > 0; 
    } 
}