2015-01-23 3 views
2

그래서 저는 프로그램 작업을하고 있으며 마지막으로 java를 사용하고 있기 때문에 잠시 기다려 왔습니다. 내 프로그램에 소수를 허용하는 방법을 궁금 해서요.Java 변경 응용 프로그램

package test; 
    import java.util.Scanner; 

    /** 
    * 
    * @author Thao 
    */ 
    public class Test { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
    // TODO code application logic here 
    // create Scanner to obtain input from command window 
    Scanner input = new Scanner(System.in); 
    double cash; // double makes it allow decimals 
    int hundred; 
    int twenty; 
    int ten; 
    int toonies ; 
    int lonies; 
    int quarters; 
    int dimes; 
    int nickels; 
    int pennies; 
    int money; 
    int change; 

    System.out.println();// blank line 
    System.out.print("Hello "); 
    System.out.println(); 
    System.out.println("Please enter a number with no decimal places "); 
    cash = input.nextInt(); // read first integer 
    System.out.println("you have Entered "+cash); 
    hundred = 100; 
    change = (int)hundred - (int)cash; 
    System.out.println("The change from $100.00 is:" + (double)change); 


    change = 100 * change; // multiply cash by 100 
    money = (int)change; //cash is now a int 

    twenty = money/2000; 
    money = money - (twenty * 2000); 

    toonies = money/200; // money is multiplied by 100 than/by 200 
    money = money - (toonies * 200); // ex. money = 1586 - (7.93 * 200) 

    lonies = money/100 ; 
    money = money - (lonies * 100); 

    quarters = money/25; 
    money = money - (quarters * 25); 

    dimes = money/10; 
    money = money - (dimes * 10); 

    nickels = money/5; 
    money = money - (nickels * 5); 

    pennies = money/1; 
    money = money - (pennies * 1); 
    if(twenty>0){ 
    System.out.println(); 
    System.out.print("You will have this many Twenties "); 
    System.out.print(twenty + "."); 
    } 
    else{ 
    } 
    if(toonies>0){ 
    System.out.println(); 
    System.out.print("You will have this many Toonies "); 
    System.out.print(toonies + "."); 
    System.out.println(); 
    } 
    else{ 
    } 
    if(lonies>0){ 
    System.out.print(" You will have this many Lonies "); 
    System.out.print(lonies + "."); 
    System.out.println(); 
    } 
    else{ 
    } 
    if(quarters>0){ 
    System.out.print(" You will have this many quarters "); 
    System.out.print(quarters + "."); 
    System.out.println(); 
    } 
    else{ 
    } 

    if(dimes>0){ 
    System.out.print(" You will have this many dimes "); 
    System.out.print(dimes + "."); 
    System.out.println(); 
    } 
    else{ 
    } 

    if(dimes>0){ 
    System.out.print(" You will have this many nickels "); 
    System.out.print(nickels); 
    System.out.println(); 
    } 
    else{ 
    } 
    if(pennies>0){ 
    System.out.print(" You will have this many pennies "); 
    System.out.print(pennies); 
    System.out.println(); 
    } 
    else{ 
    } 
    System.out.println(); 
    System.out.println("Thank you for using my app "); 


    } 
    } 

답변

3

당신은 두 배를 사용하거나 변수의 데이터 유형에 대한 플로트 수 있습니다 ... 정말 understood.Below 내가 지금까지 한 일이다 나는 그것을 찾는 시도했지만 내가 도움이 아무것도 아무것도 찾을 수 없습니다. 사용자의 입력에서 두 번을 읽으려면 당신이 할 것 :

double cash = input.nextDouble(); 

또는

float cash = input.nextFloat(); 
+2

을 경고 참고로, 실제 금융 (즉, 실제 돈이 아닌 교실 프로젝트) 여기서 정밀도를 포함 아무것도 BigDecimal의 사용을 고려해야합니다 (http://stackoverflow.com/questions/6320209/javawhy-should-we-use-bigdecimal-instead-of-double-in-the-real-world). – Compass

+0

@haley 감사합니다. – user3215990