2016-02-13 149 views
0

所以,我对Java仍然相当陌生,而且我对此很难接受。 对于一个班级,我不得不编写一个程序来计算用户的纳税申报数额(这绝不是准确的)。 当我运行这个时,它实际上并没有存储年收入额(存储在userInput方法中)。当我将其更改为public userInput(),它并没有给我一个错误的行:不存储变量的值

double annualIncome = entry.nextDouble; 

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

有人能帮助我理解为什么这是,如何解决它,以及为什么解决方案的工作原理?

我也试图通过一个对话框来接受用户的输入,但由于只返回字符串值,我试图annualIncome转换为浮动或加倍,这样我可以通过使用Float.parseFloat使用它在calcTax方法( );这也没有存储任何价值,我相信上面的答案会帮助我。

解决此问题后,我的下一步是确保程序仅向前移动,如果ssn字段遵循111-11-1111格式,zip字段只有5个数字,那么annualIncome不是负数(这会吸引),并且婚姻状态的条目以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(); 
} 

回答

0

你在这里使用局部变量:

double annualIncome = entry.nextDouble(); 

只是删除 “双” 使用你想要什么:

annualIncome = entry.nextDouble(); 

为您的EST的问题:你需要检查格式的输入,这似乎是最好的使用正则表达式,是这样的:

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(); 
} 
+1

他不应该还加括号,因为'nextDouble()'是'Scanner'类的方法? –

+0

肯定莱利,我不知道什么时候它会迷路:) – bohuss

0

随时随地可以使用实例变量,以“这个称呼他们“。运营商。检查下面的编辑代码。它接受年收入并给出正确的产出。我希望我能正确理解你的问题。

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(); 
} 
} 
1

你的方法userInput...声明的变量(双年收入)。然而这种方法是一个public void方法,意思是它不返回任何东西,但是你需要双倍的。更改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(); 
}