import java.io.*;
public class CheckbookBalancingCalculator {
public static void main( String[] args ) throws IOException {
String statementbalance, depositstotal, checkstotal, feestotal;
int statement, deposits, checks, fees, index;
BufferedReader dataIn = new BufferedReader( new InputStreamReader( System.in ));
System.out.println( "\tBalancing Your Checkbook:" );
System.out.println();
System.out.print( "\t\tWhat is the balance from your last statement?" );
statementbalance = dataIn.readLine();
statement = Integer.parseInt( statementbalance );
System.out.print( "\t\tWhat is the to开发者_运维问答tal amount of all deposits?" );
depositstotal = dataIn.readLine();
deposits = Integer.parseInt( depositstotal );
System.out.print( "\t\tWhat is the total amount of all checks?" );
checkstotal = dataIn.readLine();
checks = Integer.parseInt( checkstotal );
System.out.print( "\t\tWhat is the total amount of all transaction fees?" );
feestotal = dataIn.readLine();
fees = Integer.parseInt( feestotal );
index = statement + deposits - checks - fees;
System.out.println();
System.out.println( "\tYour new balance is " + Math.round( index ) + "." );
System.out.println();
}
}
Floats/Doubles are a bad idea. US Currency is a fixed decimal calculation.
String statementBalanceInput, depositsTotalInput, checksTotalInput, feesTotalInput;
int statement, deposits, checks, fees, index;
BigDecimal statementBalance = new BigDecimal( statementBalanceInput);
BigDecimal depositsTotal = new BigDecimal( depositsTotalInput);
BigDecimal checksTotal = new BigDecimal( checksTotalInput);
BigDecimal feesTotal = new BigDecimal( feesTotalInput);
This is off the top of my head. Then do the math with BigDecimal.
import java.io.*; public class CheckbookBalancingCalculator { public static void main( String[] args ) throws IOException { String statementbalance, depositstotal, checkstotal, feestotal; float statement, deposits, checks, fees, index; BufferedReader dataIn = new BufferedReader( new InputStreamReader( System.in )); System.out.println( "\tBalancing Your Checkbook:" ); System.out.println(); System.out.print( "\t\tWhat is the balance from your last statement?" ); statementbalance = dataIn.readLine(); statement = Float.parseFloat( statementbalance ); System.out.print( "\t\tWhat is the total amount of all deposits?" ); depositstotal = dataIn.readLine(); deposits = Float.parseFloat( depositstotal ); System.out.print( "\t\tWhat is the total amount of all checks?" ); checkstotal = dataIn.readLine(); checks = Float.parseFloat( checkstotal ); System.out.print( "\t\tWhat is the total amount of all transaction fees?" ); feestotal = dataIn.readLine(); fees = Float.parseFloat( feestotal ); index = statement + deposits - checks - fees; System.out.println(); System.out.println( "\tYour new balance is " + Math.round( index ) + "." ); System.out.println(); } }
The answer you are probably looking for is Float.parseFloat() or Double.parseDouble(), depending on required precision.
If you happen to accept currency / numeric values in different formats, depending on locale, try NumberFormat methods instead (passing valid Locale object).
精彩评论