2014-10-30 76 views
0

我想不通爲什麼我不能打電話給我的方法。我檢查了幾次並嘗試運行它,但它總是在我爲這筆錢輸入價值後立即結束。它應該返回給定數量的金額,使用百,五十,二十,五十,單身,宿舍,硬幣,鎳和便士的變化量。因此,如果它是42.55美元,則產量將是「0 0 2 0 0 2 2 0 1 0」(二十二,二單打,兩季,一鎳)。先謝謝你!有人可以幫我弄清楚爲什麼我不能打電話給我的方法嗎?

import java.util.Scanner; 
public class MakeChange { 
    public static void main(String arg[]) { 
    Scanner readmoney = new Scanner(System.in); 

    System.out.println("Money amount? >"); 
    double money = readmoney.nextDouble(); 
    System.out.println(); 

    String thing = makeChange(money); 
} 

public static String makeChange(double money) { 

    int hundreds = (int) money/100; 

    int fifties = (int)(money/50) - (2*hundreds); 

    int twenties = (int) (money/20) - (5*hundreds) - (int)(2.5*fifties); 

    int tens = (int)(money/10) - (10*hundreds) - (5*fifties) - (2*twenties); 

    int fives = (int)(money/5) - (20*hundreds) - (10*fifties) - (4*twenties) - (2*tens); 

    int singles = (int)(money) - (100*hundreds) - (50*fifties) - (20*twenties) - (10*tens) - (5*fives); 

    int quarters = (int)(money/0.25) - (400*hundreds) - (200*fifties) - (80*twenties) - (40*tens) - (20*fives) - (4*singles); 

    int dimes = (int)(money/0.1) - (1000*hundreds) - (500*fifties) - (200*twenties) - (100*tens) - (50*fives) - (10*singles) - (int)(2.5*quarters); 

    int nickels = (int)(money/0.05) - (2000*hundreds) - (1000*fifties) - (400*twenties) - (200*tens) - (100*fives) - (20*singles) - (5*quarters) - (2*dimes); 

    int pennies = (int)(money/0.01) - (10000*hundreds) - (5000*fifties) - (2000*twenties) - (1000*tens) - (500*fives) - (100*singles) - (25*quarters) - (10*dimes) - (5*nickels); 

    String change = (hundreds + " " + fifties + " " + twenties + " " + tens + " " + fives + " " + singles + " " + quarters + " " + dimes + " " + nickels + " " + pennies); 

    return change; 

}}

+8

你想用'thing'做什麼?也許你想'System.out.println(東西)'將其打印到控制檯? – 2014-10-30 02:34:47

+1

看起來它正在做你剛纔告訴它... – MadProgrammer 2014-10-30 02:35:43

回答

4
String thing = makeChange(money); 
} 
// end of program 

你不打印你的結果。

該方法被調用(並且不會因異常而崩潰)。

0

該方法調用成功。它返回一個結果,將其存儲在變量中,然後程序結束。您需要實際將返回值打印到控制檯以顯示某些內容。

public static void main(String arg[]) { 
    Scanner readmoney = new Scanner(System.in); 

    System.out.println("Money amount? >"); 
    double money = readmoney.nextDouble(); 
    System.out.println(); 

    String thing = makeChange(money); 
    System.out.println(thing); 
} 
相關問題