此问题在此处已有答案:
What is 'scope' in Java?(2个答案)
上个月关门了。
我是Java的新手,正在尝试写一个程序,询问你的姓名、收入、联邦和州的税率,然后计算出你应该缴纳多少税。它应该包括对话框。我的问题是:我已经完成了所有的代码并放入parseDouble()方法,但是当我去计算时,Eclipse给了我一个错误(stateTaxRate不能解析为变量,它对fedTaxRate变量也做了同样的事情!我被卡住了。
import javax.swing.JOptionPane;
public class FedAndStateTaxes {
public static void main(String[] args) {
String yourName= JOptionPane.showInputDialog("What is your name?");
JOptionPane.showMessageDialog(null, "Name: " + yourName);
String yearlyIncome = JOptionPane.showInputDialog("What is your yearly income?");
try {double num = Double.parseDouble(yearlyIncome);
JOptionPane.showMessageDialog(null, "Yearly Income: "+ num);
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null,"Error. Number input only please.");
}
String fTaxRate = JOptionPane.showInputDialog("Please enter your federal tax rate");
try { double fedTaxRate = Double.parseDouble(fTaxRate);
JOptionPane.showMessageDialog(null, "Federal Tax Rate: " + fedTaxRate +"%");
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Error. Number input only please.");
}
String sTaxRate = JOptionPane.showInputDialog("Pleae enter your state tax rate");
try {
double stateTaxRate = Double.parseDouble(sTaxRate);
JOptionPane.showMessageDialog(null, "State Tax Rate: " + stateTaxRate + "%");
}
catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null,"Error. Number input only please");
}
double calculateIncome = (stateTaxRate);
字符串
我尝试在parseDouble()方法中更改参数/变量名。我希望它能调用我转换成double的字符串变量,但它就是无法解析。
1条答案
按热度按时间y53ybaqx1#
你在try块中声明了它,所以它只存在于该块的作用域中。