Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使字符串的行为像双重_Java_String_Parsing_Double_Joptionpane - Fatal编程技术网

Java 如何使字符串的行为像双重

Java 如何使字符串的行为像双重,java,string,parsing,double,joptionpane,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 annualInter

我相信我使用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);
}

}}
import javax.swing.JOptionPane;
公共阶级利益{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
//输入年利率
字符串年度利率1;
Double.parseDouble(年度利率1);
JOptionPane.showInputDialog(“输入您的第一年利率”);
//获得月利率
双月利率1;
**月利率1=年利率1/12**
//输入储蓄金额
字符串savingsAmount1;
Double.parseDouble(savingsAmount1);
JOptionPane.showInputDialog(“输入您的第一笔储蓄金额”);
双端天平=0;
//显示结果
showMessageDialog(null,“Adekunle Akinmola”);
showMessageDialog(null,“”);
//计算付款

对于(int i=0;i,不能将字符串除以整数

试试这个:

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

//Obtain Monthly Interest Rate
double monthlyInterestRate1 = dblAnnualInterestRate1 / 12;
问题是

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);
在此代码中

String annualInterestRate1;
Double.parseDouble(annualInterestRate1);
JOptionPane.showInputDialog("Enter your 1st annual interest rate");
您调用了正确的方法,但放弃了结果。在阅读输入流之前,您还尝试解析输入流。我建议您尝试一下

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

你认为这三条线有什么作用

String annualInterestRate1;
Double.parseDouble(annualInterestRate1);
JOptionPane.showInputDialog("Enter your 1st annual interest rate");
第一行声明了一个变量,但没有给它赋值

第二行将无法编译,因为
annualInterestate1
尚未“明确分配”。
调用
parseDouble
而不使用返回值是毫无意义的(除非您这样做只是为了查看参数是否是有效的
double
值,而您不是)

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


也许您应该仔细阅读赋值运算符:

您有什么错误?参数类型字符串intOk的运算符/未定义,现在请仔细查看您的代码,您确实解析了double from字符串。但是您没有使用返回值。您从未赋值
JOptionPane.showInputDialog(“”);
返回值。谢谢!这帮了大忙,但并没有改变循环中的错误:for(int i=0;这就是为什么我尝试解析字符串,使其可以作为表达式中的双精度!问题不在于如何解析字符串,问题在于,
double.parseDouble
的结果从未分配给变量。因此它丢失了。您应该阅读有关scop的更多信息变量和参数的集合。在您的代码中
Double.parseDouble(annualInterestate1)
不会影响AnnualInterestate1。因此,您必须将操作结果分配给
AnnualInterestate1
。很抱歉,我对编程非常陌生,因此我在一些小问题上遇到了困难,如this@KunleAkinmola没关系。这就是为什么我解释了台词的实际作用,所以你可以从中学习。其他答案显示了how试图修复它,但并没有真正描述你做错了什么。