私はBigDecimalクラスを使って学校向けのプログラムを作成しようとしています。プログラムは、金利計算機ですと、最終的な出力がsomethigのようにする必要があります:BigDecimalクラスJava
Loan Amount: whatever loan amount is in dollars
Interest Rate: as a percent
Interest: amount of interest paid in dollars
continue? Y/N:
本はBigDecimalクラスをコーディングする方法については明らかではないと私はので、私は変更を加える任意の時間をEclipseを使用しています私は混乱しているエラーを取得します。 誰かがこれを見て、私を正しい方向に向けることができますか?私はMurachのJava SE6を使用していますが、この本はあまり役に立ちません。
ありがとうございました!
import java.util.Scanner; //import scanner
import java.text.NumberFormat; //import number format
import java.math.*; //import math classes
public class InterestCalculator //create public class
{
public static void main(String[] args)
{
Scanner calc = new Scanner(System.in); //create scanner
double LoanAmount, InterestRate, Interest; //declareLoanAmount,InterestRate, and Interest as double
//welcome user to the Interest Rate Calculator
System.out.println("Welcome to The Interest Rate Calculator");
System.out.println();
//perform choice calculations until choice isn't equal to "y" or "Y"
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
//Get Loan Amount from user
System.out.println("Enter Loan Amount: ");
LoanAmount = calc.nextDouble();
//Get Interest rate from user
System.out.println("Enter Interest Rate: ");
InterestRate = calc.nextDouble();
BigDecimal decimalInterest = new BigDecimal(Double.toString(Interest));
decimalInterest = decimalInterest.setScale(2, RoundingMode.HALF_UP);
BigDecimal decimalInterestRate = new BigDecimal(Double.toString(InterestRate));
decimalInterestRate = decimalInterestRate.setScale(2, RoundingMode.HALF_UP);
//calculate interest
System.out.println("message");
//prompt user to continue?
System.out.println("Continue? Y/N: ");
choice = calc.next();
System.out.println();
}
}
}
私はまたInterest = LoanAmount * InterestRateのようなものでなければならない計算をコーディングすることに問題があります。そして私は下の方のプリントが単に "Message"という単語を印刷することを知っています。私の以前の試みでは、私は出力全体を印刷する文字列を書こうとしていましたが、私は文字列メッセージに題名を付けました。 –
どのようなエラーが表示されますか? – Kris
[BigDecimal javadoc](http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html)をご覧になることをお勧めします。かなり包括的です。一般に、 'BigDecimal'は不変であるため、操作を実行するたびに新しいインスタンスを取得します。 'BigDecimal someValue = decimalInterest.multiply(decimalInterestRate);'それ以外は、 'Double.toString(Interest)'のような文字列変換は不要です。 'BigDecimal' ctorはdoubleを受け取ります。 javadoc!) –