2015-01-23 2 views
2

Так что я работал над Программой, и прошло некоторое время с тех пор, как я последний раз использовал java. Мне было интересно, как заставить мою программу принимать десятичные знаки. Я пытался его поиск, но я не мог найти что-нибудь полезное и все, что я на самом деле understood.Below это то, что я сделал до сих пор ...Приложение для изменения 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

Вы можете использовать двойной или поплавок для типа данных переменной. Чтобы прочитать двойной из вашего входа вы могли бы сделать:

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

Смежные вопросы