2016-02-13 144 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(); 
}