2014-11-02 24 views
-1

我正在设计一个假设为ATM机的课程项目。该项目的参数有5个方法和一个主程序。一切似乎都运转良好,但经过进一步测试并存入更多资金后,新的余额变成了一个约有16位小数的数字。我没有使用任何除法和方法是简单的加减算术。如何确保我的双重输出只有2个小数位?

这基本上是我得到了一些存款后。

Enter the number for one of the following choices. 
1. Display Balance 
2. Deposit 
3. Withdraw 
4. Log Out 
2 
Enter the amount you wish to deposit: $ 222.22 

$ 2943.48 

Enter the number for one of the following choices. 
1. Display Balance 
2. Deposit 
3. Withdraw 
4. Log Out 
2 
Enter the amount you wish to deposit: $ 333.22 

$ 3276.7 

Enter the number for one of the following choices. 
1. Display Balance 
2. Deposit 
3. Withdraw 
4. Log Out 
2 
Enter the amount you wish to deposit: $ 222.33 

$ 3499.0299999999997 

Enter the number for one of the following choices. 
1. Display Balance 
2. Deposit 
3. Withdraw 
4. Log Out 

这是我现在的代码和我所做的。谢谢您的帮助。

import java.util.Scanner; 

public class ATM { 

public static Scanner keyboard = new Scanner(System.in); 
// The checkID method determines if acctNum is a valid account number 
// and pwd is the correct password for the account. If the account information 
// is valid, the method returns the current account balance, as a string. 
// If the account information is invalid, the method returns the string "error". 
public static String checkID(String acctNum, String pwd) 
{ 
    String result = "error"; 


    // Strings a, b, and c contain the valid account numbers and passwords. 
    // For each string, the account number is listed first, followed by 
    // a space, followed by the password for the account, followed by a space, 
    // followed by the current balance. 
    String a = "44567-5 mypassword 520.36"; 
    String b = "1234567-6 anotherpassword 48.20"; 
    String c = "4321-0 betterpassword 96.74"; 

    if (acctNum.equals(a.substring(0, a.indexOf(" "))) && 
      pwd.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" ")))) 
     return result = a.substring(a.lastIndexOf(" ") + 1); 

    if (acctNum.equals(b.substring(0, b.indexOf(" "))) && 
      pwd.equals(b.substring(b.indexOf(" ")+1,b.lastIndexOf(" ")))) 
     return result = b.substring(b.lastIndexOf(" ") + 1); 

    if (acctNum.equals(c.substring(0, c.indexOf(" "))) && 
      pwd.equals(c.substring(c.indexOf(" ") + 1,c.lastIndexOf(" ")))) 
     return result = c.substring(c.lastIndexOf(" ") + 1); 

    return result; 
} 
public static int menu() 
{ 
    int menuChoice; 
    do 
    { 
     System.out.print("\nEnter a number corresponding to one of the" 
       + " following choices.\n 1. Display Balance" 
       + "\n 2. Deposit\n 3. Withdraw\n 4. Log Out\n"); 
     menuChoice = keyboard.nextInt(); 
     if (menuChoice < 1 || menuChoice > 4){ 
      System.out.println("error"); 
     } 
    }while (menuChoice < 1 || menuChoice > 4); 
    return menuChoice; 
} 
public static void displayBalance(double x) 
{ 
    System.out.printf("\nYour current balance is $%.2f\n", x); 
} 
public static double deposit(double x, double y) 
{ 
    return x + y; 
} 
public static double withdraw(double x, double y) 
{ 
    if (y > x){ 
     return x; 
    } 
    return x-y; 
} 
public static void main(String[] args) { 

    String accNum, pass, origBal = "error"; 
    int count = 0, menuOption = 0; 
    double depositAmt, withdrawAmt, currentBal; 

    //loop that will count the number of login attempts 
    //you make and will exit program if it is more than 3. 
    //as long as oriBal equals an error. 
    do{ 

     System.out.println("Please enter your account number: "); 
     accNum = keyboard.next(); 

     System.out.println("Enter your password: "); 
     pass = keyboard.next(); 

     origBal = checkID(accNum, pass); 

     count++; 
     if (count >= 3 && origBal.equals("error")){ 
      System.out.print("Maximum login attempts reached."); 
      System.exit(0); 
     } 
     if (!(origBal.equals("error"))){ 
      System.out.println("\nYour balance is $ "+ origBal); 
     } 
     else 
      System.out.println(origBal); 


    }while(origBal.equals("error")); 

    currentBal=Double.parseDouble(origBal); 
    //this loop will keep track of the options that 
    //the user inputs in for the menu. and will 
    //give the option of deposit, withdraw, or logout. 

    while (menuOption != 4) 
    { 
     menuOption=menu(); 
     switch (menuOption) 
     { 
     case 1: 
      displayBalance(currentBal); 
      break; 
     case 2: 
      System.out.print("Enter the amount you wish to deposit: $ "); 
      depositAmt = keyboard.nextDouble(); 
      currentBal = deposit(depositAmt, currentBal); 
      System.out.printf("Your new balance is $%.2f\n", currentBal); 
      break; 
     case 3: 
      System.out.print("Enter the amount you wish to withdraw: $ "); 
      withdrawAmt = keyboard.nextDouble(); 
      if (withdrawAmt > currentBal) 
       System.out.println("error"); 
      currentBal = withdraw(currentBal, withdrawAmt); 
      System.out.printf("Your new balance is $%.2f\n",currentBal); 
      break; 
     case 4: 
      System.exit(0); 
      break; 
     } 
    } 
} 

}

这是我的更新版本。

感谢您的建议。

+0

这与双提交问题无关。您应该将标签更改为Java。 – Makoto 2014-11-02 22:48:32

回答

0

我建议你阅读working with Currency in java--它提到的一件事是避免使用float或double来表示金钱,因为这些数据类型不太适合像推荐的BigDecimal那样四舍五入。

0

您得到的浮点错误是由于数字存储在内存中的方式造成的,更多信息请参见herehere

要回答你的问题,使用的格式,例如:分别

DecimalFormat df = new DecimalFormat("#.00"); 
System.out.println("\nYour current balance is $" + df.format(currentBal) + "\n"); 

变化在其他地方。

0

您可以使用printf()函数。

System.out.printf("Your current balance is $%.2f\n", currentBal); 

进一步说明:

printf的功能将打印格式的输出。 %是要打印的变量的开头,.2告诉您要在小数点后面显示两个插槽的函数,并且f表示您给出的变量类型(在这种情况下,f代表浮点数/双精度值)。该类型也表示输入的结束,所以“\ n”不会作为格式化的一部分集中并保持为正常的换行符。

注意:printf函数也将做修正您的随机十进制数所需的舍入。在3499.0299999999997的情况下,它将打印3499.03

+0

感谢您的信息。我明白你在说什么。出于好奇,虽然因为我是新手,但是我是否必须更改打印新余额的所有打印报表? – mlopman 2014-11-03 00:52:49

+0

只有你打印你的双值的地方。所以是的,基本上在打印天平时。 – Aargonian 2014-11-03 18:53:58

1

如果你不使用double需要,可考虑改用BigDecimal用秤2不同于doubleBigDecimal可以代表所有的输入和结果完全吻合。对于大多数货币计算而言,这是一个更好的选择,包括ATM模拟。