不将值存储到变量

时间:2016-02-13 05:07:00

标签: java input joptionpane

所以,我对Java仍然相当新,而且我很难用这个。 对于一个班级,我必须编写一个程序来计算用户的纳税申报金额(这绝不是真正准确的)。 当我运行它时,它实际上并不存储年收入金额(存储在userInput方法中)。当我将其更改为public userInput()时,它不会给我一行错误:

double annualIncome = entry.nextDouble;

然而,有一个错误说我应该将方法更改为构造函数,或者将其更改为public void userInput,然后给出一个错误,说明变量annualIncome不是用户。

有人可以帮助我理解为什么会这样,如何修复它,以及为什么该解决方案有效?

我也试图通过对话框接受用户输入,但由于那只返回字符串值,我试图将annualIncome转换为float或double,因此我可以通过使用Float.parseFloat()在calcTax方法中使用它;这也没有存储任何价值,我相信上面的答案对我有帮助。

解决此问题后,我的下一步是确保只有ssn字段遵循111-11-1111格式时,程序才会向前移动,zip字段只有5个数字,yearIncome不是负数(那会很糟糕,而且maritalStatus的条目以M,m,S或s开头。

如果有人能指出我正确的方向,这部分将非常感激。 尽管如此,我很难向老师学习。

我觉得我有代码的一般结构:

package javaProgramming;
import java.util.Scanner;

public class TaxReturn{
Scanner entry = new Scanner(System.in);

// Tax return data fields
String ssn;
String lastName;
String firstName;
String streetAddress;
String city;
String state;
String zip;
String maritalStatus;
double annualIncome;
float taxLiability;

public TaxReturn(){

}

// Not needed? Program runs the same when this part is taken out.
public TaxReturn(String ssn, String lastName, String firstName, String streetAddress, String city, String state, String zip, String maritalStatus, double annualIncome, float calculateTax){

    this.ssn = ssn;
    this.lastName = lastName;
    this.firstName = firstName;
    this.streetAddress = streetAddress;
    this.city = city;
    this.state = state;
    this.zip = zip;
    this.maritalStatus = maritalStatus;
    this.annualIncome = annualIncome;
    taxLiability = calculateTax;
}

// Used to get user input for Tax Return info
public void userInput(){
    System.out.println("What is your social security number?");
    ssn = entry.nextLine();
    System.out.println("What is your last name?");
    lastName = entry.nextLine();
    System.out.println("What is your first name?");
    firstName = entry.nextLine();
    System.out.println("What is your street address?");
    streetAddress = entry.nextLine();
    System.out.println("What city do you live in?");
    city = entry.nextLine();
    System.out.println("What state do you live in?");
    state = entry.nextLine();
    System.out.println("What's your zip code?");
    zip = entry.nextLine();
    System.out.println("What's your marital status?");
    maritalStatus = entry.nextLine();
    System.out.println("How much is your annual income?");
    double annualIncome = entry.nextDouble();


}

// Will calculate the tax rate depending on the user's income and marital status.
double calcTax(){
double rate = 0;
if(annualIncome >= 0 && annualIncome <= 20000){
    if (maritalStatus == ("Married"))
        rate = .14;
    else
        rate = .15;
} else if ((annualIncome >= 20001) && annualIncome <= 50000){
    if (maritalStatus == ("Married"))
        rate = .2;
    else
        rate = .22;
} else if (annualIncome >= 50001){
    if (maritalStatus == ("Married"))
        rate = .28;
    else
        rate = .3;
}
return annualIncome * rate;
}

// Displays a report for the user with their tax return amount based on what they entered.
public void returnData(){
    System.out.println("Name: " + firstName + " " + lastName
            + "\nAddress: " + streetAddress + " " + city + ", " + state + " " + zip
            + "\nMarital Status: " + maritalStatus
            + "\nAnnual Income: " + annualIncome
            + "\nTax return amount: $" + calcTax());
}

public static void main(String[] args){
    TaxReturn taxReturn1 = new TaxReturn();
    taxReturn1.userInput();
    taxReturn1.calcTax();
    taxReturn1.returnData();
}

}

3 个答案:

答案 0 :(得分:1)

您在方法userInput...中声明您的变量(双年收入)。但是这个方法是public void方法,意味着它不会返回任何内容,但是你需要double。将public void userInput()更改为public double userInput()

 public double userInput(){
    System.out.println("What is your social security number?");
    ssn = entry.nextLine();
    System.out.println("What is your last name?");
    lastName = entry.nextLine();
    System.out.println("What is your first name?");
    firstName = entry.nextLine();
    System.out.println("What is your street address?");
    streetAddress = entry.nextLine();
    System.out.println("What city do you live in?");
    city = entry.nextLine();
    System.out.println("What state do you live in?");
    state = entry.nextLine();
    System.out.println("What's your zip code?");
    zip = entry.nextLine();
    System.out.println("What's your marital status?");
    maritalStatus = entry.nextLine();
    System.out.println("How much is your annual income?");
    this.annualIncome = entry.nextDouble();
}

答案 1 :(得分:0)

你在这里使用局部变量:

double annualIncome = entry.nextDouble();

只需删除&#34; double&#34;使用你想要的东西:

 annualIncome = entry.nextDouble();

对于你的问题:你需要检查输入的格式,似乎最好使用正则表达式,如:

System.out.println("What is your social security number?");
ssn = entry.nextLine();
while( ! (ssn.matches("^(\\d{3}-\\d{2}-\\d{4})$")) )
{
    System.out.println("Please enter ssn in 111-11-1111 format:");
    ssn = entry.nextLine();
}

答案 2 :(得分:0)

无论您在何处使用实例变量,请使用&#34; this来调用它们。&#34;运营商。检查下面编辑的代码。它接受年收入并提供正确的产出。我希望我能正确理解你的问题。

public class TaxReturn {
Scanner entry = new Scanner(System.in);
// Tax return data fields
String ssn;
String lastName;
String firstName;
String streetAddress;
String city;
String state;
String zip;
String maritalStatus;
double annualIncome;
float taxLiability;

public TaxReturn(){

}

// Not needed? Program runs the same when this part is taken out.
public TaxReturn(String ssn, String lastName, String firstName, String streetAddress, String city, String state, String zip, String maritalStatus, double annualIncome, float calculateTax){

    this.ssn = ssn;
    this.lastName = lastName;
    this.firstName = firstName;
    this.streetAddress = streetAddress;
    this.city = city;
    this.state = state;
    this.zip = zip;
    this.maritalStatus = maritalStatus;
    this.annualIncome = annualIncome;
    taxLiability = calculateTax;
}

// Used to get user input for Tax Return info
public void userInput(){
    System.out.println("What is your social security number?");
    this.ssn = entry.nextLine();
    System.out.println("What is your last name?");
    this.lastName = entry.nextLine();
    System.out.println("What is your first name?");
    this.firstName = entry.nextLine();
    System.out.println("What is your street address?");
    this.streetAddress = entry.nextLine();
    System.out.println("What city do you live in?");
    this.city = entry.nextLine();
    System.out.println("What state do you live in?");
    this.state = entry.nextLine();
    System.out.println("What's your zip code?");
    this.zip = entry.nextLine();
    System.out.println("What's your marital status?");
    this.maritalStatus = entry.nextLine();
    System.out.println("How much is your annual income?");
    this.annualIncome = entry.nextDouble();


}

// Will calculate the tax rate depending on the user's income and marital status.
public double calcTax(){
double rate = 0;
if(this.annualIncome >= 0 && this.annualIncome <= 20000){
    if (this.maritalStatus == ("Married"))
        rate = .14;
    else
        rate = .15;
} else if ((this.annualIncome >= 20001) && this.annualIncome <= 50000){
    if (this.maritalStatus == ("Married"))
        rate = .2;
    else
        rate = .22;
} else if (this.annualIncome >= 50001){
    if (this.maritalStatus == ("Married"))
        rate = .28;
    else
        rate = .3;
}
return this.annualIncome * rate;
}

// Displays a report for the user with their tax return amount based on what they entered.
public void returnData(){
    System.out.println("Name: " + this.firstName + " " + this.lastName
            + "\nAddress: " + this.streetAddress + " " + this.city + ", " + this.state + " " + this.zip
            + "\nMarital Status: " + this.maritalStatus
            + "\nAnnual Income: " + this.annualIncome
            + "\nTax return amount: $" + calcTax());
}

public static void main(String[] args){
    TaxReturn taxReturn1 = new TaxReturn();
    taxReturn1.userInput();
    taxReturn1.calcTax();
    taxReturn1.returnData();
}
}