如何使字符串像双重一样

时间:2015-09-22 14:42:13

标签: java string parsing double joptionpane

我相信我使用Double.parseDouble是正确的,但我一直收到错误。代码有什么问题?我一直收到错误的行在它们周围有星号。

import javax.swing.JOptionPane;

public class Interest {


public static void main(String[] args) {

// TODO Auto-generated method stub


//Enter Annual Interest Rate
String annualInterestRate1;
Double.parseDouble(annualInterestRate1);
JOptionPane.showInputDialog("Enter your 1st annual interest rate");

//Obtain Monthly Interest Rate
double monthlyInterestRate1;
**monthlyInterestRate1= annualInterestRate1 / 12;**


//Enter the Savings Amount
String savingsAmount1;
Double.parseDouble(savingsAmount1);
JOptionPane.showInputDialog ("Enter your 1st savings amount") ;

double endingBalance=0;



//Display Results
JOptionPane.showMessageDialog(null, "Adekunle Akinmola");
JOptionPane.showMessageDialog(null," ");

//Calculate Payment
for(int i =0; i<=5;i++){
    **endingBalance = savingsAmount1 * (1+ monthlyInterestRate1);**
    System.out.println("$"+ endingBalance);
}

}}

4 个答案:

答案 0 :(得分:2)

在此代码中

String annualInterestRate1Str = 
    JOptionPane.showInputDialog("Enter your 1st annual interest rate");
double annualInterestRate1 = Double.parseDouble(annualInterestRate1Str);

您调用正确的方法,但丢弃结果。您还尝试在读取之前解析输入流。我建议你试试。

SELECT * FROM lportal.expandovalue where data_ like 21005;

答案 1 :(得分:1)

您不能将字符串除以整数。

试试这个:

String annualInterestRate1 = "24";
double dblAnnualInterestRate1 = Double.parseDouble(annualInterestRate1);

//Obtain Monthly Interest Rate
double monthlyInterestRate1 = dblAnnualInterestRate1 / 12;

答案 2 :(得分:1)

问题是

String annualInterestRate1;/ this line you need to save the value for the string and then use that.
Double.parseDouble(annualInterestRate1); // this line you need to save the value for the double and then use that.

你想做的是

//Enter Annual Interest Rate
String annualInterestRate1;
annualInterestRate1=JOptionPane.showInputDialog("Enter your 1st annual interest rate");
double annualInterestRate1d = Double.parseDouble(annualInterestRate1);

//Obtain Monthly Interest Rate
double monthlyInterestRate1;
monthlyInterestRate1= annualInterestRate1d / 12;

编辑:

String savingsAmount1;
Double.parseDouble(savingsAmount1);
JOptionPane.showInputDialog ("Enter your 1st savings amount") ;

需要改为

String savingsAmount=JOptionPane.showInputDialog ("Enter your 1st savings amount") ;
double savingsAmount1 = Double.parseDouble(savingsAmount);

答案 3 :(得分:0)

您认为这三条线有什么作用?

String annualInterestRate1;
Double.parseDouble(annualInterestRate1);
JOptionPane.showInputDialog("Enter your 1st annual interest rate");

第一行声明一个变量,但没有给它赋值。

第二行将无法编译,因为annualInterestRate1未被&#34;明确分配&#34;。
在不使用返回值的情况下调用parseDouble是没有意义的(除非您只是查看参数是否为有效的double值,否则您不会这样做。)

第三行将向用户显示一个对话框,要求输入,但由于您没有使用返回值,因此无论用户类型被丢弃。

也许你应该阅读作业运算符:Assignment Statements in Java