gpt4 book ai didi

java - 不将值存储到变量

转载 作者:行者123 更新时间:2023-12-01 10:25:31 24 4
gpt4 key购买 nike

所以,我对 Java 还很陌生,而且我对此感到很困难。对于一门类(class),我必须编写一个程序来计算用户的纳税申报金额(这实际上并不准确)。当我运行它时,它实际上并没有存储年收入金额(存储在 userInput 方法中)。当我将其更改为 public userInput() 时,它不会给我以下行的错误:

double annualIncome = entry.nextDouble;

但是,有一个错误提示我应该将该方法更改为构造函数,或者将其更改为 public void userInput,然后这会提示我一个错误,提示变量 AnnualIncome 不是 user。

有人可以帮我理解为什么会出现这种情况、如何​​解决它以及为什么该解决方案有效吗?

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

解决此问题后,我的下一步是确保仅当 ssn 字段遵循 111-11-1111 格式、zip 字段只有 5 个数字、annualIncome 为不是负数(这很糟糕),并且 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();
}

}

最佳答案

您正在方法 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();
}

关于java - 不将值存储到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35376318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com