从不同方法寻址变量的问题
Issue with addressing variables from different method
我正在学习将 Java 和 运行 编码成一个问题。
任务描述如下:“下面的class SalaryCountingWithMethods 求三个浮点数(工时、每小时工资和税收百分比),然后计算税前和税后工资以及工资的税收部分基于提供给程序的信息。”
几天来我一直被这个问题困扰,我就是找不到解决方案。
这是我目前的进度:
import java.util.Scanner;
public class SalaryCountingWithMethods {
public static void main (String [] args) {
double hours, salaryPerHour, taxPercent, taxlessSalary, taxPart;
hours = askHours();
salaryPerHour = askSalaryPerHour();
taxPercent = askTaxPercent();
taxlessSalary = countTaxlessSalary(hours, salaryPerHour);
taxPart = taxlessSalary * taxPercent /100;
System.out.println("\nSalary before taxes: " + taxlessSalary);
System.out.println("Tax part of the salary: " + taxPart);
System.out.println("Salary after taxes: " + (taxlessSalary-taxPart));
}
static Scanner reader = new Scanner(System.in);
private static int askHours(){
System.out.print("Type in the number of work hours: ");
int hours = reader.nextInt();
return hours;
}
private static int askSalaryPerHour(){
System.out.print("Type in salary per hour: ");
int perHour = reader.nextInt();
return perHour;
}
public static int askTaxPercent(){
System.out.print("Type in the number of work hours: ");
int taxPercent = reader.nextInt();
return taxPercent;
}
private static double countTaxlessSalary(double hours, double salaryPerHour){
double taxPercent = 0;
double fullSalary = (float)hours * (float)salaryPerHour;
double taxPart = (float)fullSalary * (float)taxPercent /100;
double taxLess = (float)fullSalary - (float)taxPart;
return taxLess;
}
}
我收到以下错误:
Your program returned a none-zero value. You should return 0 from the main() function.
Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at SalaryCountingWithMethods.askHours(SalaryCountingWithMethods.java:29) at SalaryCountingWithMethods.main(SalaryCountingWithMethods.java:14)
下面的class SalaryCountingWithMethods 求三个浮点数
问题可能是您正在处理浮点数,但您定义的方法正在接受 integer
输入。当您给出双精度而不是整数时,这将导致 java.util.InputMismatchException
错误。
private static int askHours(){
System.out.print("Type in the number of work hours: ");
int hours = reader.nextInt();
return hours;
}
您应该接受双精度而不是整数作为输入。
private static double askHours(){
System.out.print("Type in the number of work hours: ");
double hours = reader.nextDouble();
return hours;
}
你应该对其他两种方法askSalaryPerHour()
和askTaxPercent()
做同样的事情。
我正在学习将 Java 和 运行 编码成一个问题。 任务描述如下:“下面的class SalaryCountingWithMethods 求三个浮点数(工时、每小时工资和税收百分比),然后计算税前和税后工资以及工资的税收部分基于提供给程序的信息。” 几天来我一直被这个问题困扰,我就是找不到解决方案。 这是我目前的进度:
import java.util.Scanner;
public class SalaryCountingWithMethods {
public static void main (String [] args) {
double hours, salaryPerHour, taxPercent, taxlessSalary, taxPart;
hours = askHours();
salaryPerHour = askSalaryPerHour();
taxPercent = askTaxPercent();
taxlessSalary = countTaxlessSalary(hours, salaryPerHour);
taxPart = taxlessSalary * taxPercent /100;
System.out.println("\nSalary before taxes: " + taxlessSalary);
System.out.println("Tax part of the salary: " + taxPart);
System.out.println("Salary after taxes: " + (taxlessSalary-taxPart));
}
static Scanner reader = new Scanner(System.in);
private static int askHours(){
System.out.print("Type in the number of work hours: ");
int hours = reader.nextInt();
return hours;
}
private static int askSalaryPerHour(){
System.out.print("Type in salary per hour: ");
int perHour = reader.nextInt();
return perHour;
}
public static int askTaxPercent(){
System.out.print("Type in the number of work hours: ");
int taxPercent = reader.nextInt();
return taxPercent;
}
private static double countTaxlessSalary(double hours, double salaryPerHour){
double taxPercent = 0;
double fullSalary = (float)hours * (float)salaryPerHour;
double taxPart = (float)fullSalary * (float)taxPercent /100;
double taxLess = (float)fullSalary - (float)taxPart;
return taxLess;
}
}
我收到以下错误:
Your program returned a none-zero value. You should return 0 from the main() function.
Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at SalaryCountingWithMethods.askHours(SalaryCountingWithMethods.java:29) at SalaryCountingWithMethods.main(SalaryCountingWithMethods.java:14)
下面的class SalaryCountingWithMethods 求三个浮点数
问题可能是您正在处理浮点数,但您定义的方法正在接受 integer
输入。当您给出双精度而不是整数时,这将导致 java.util.InputMismatchException
错误。
private static int askHours(){
System.out.print("Type in the number of work hours: ");
int hours = reader.nextInt();
return hours;
}
您应该接受双精度而不是整数作为输入。
private static double askHours(){
System.out.print("Type in the number of work hours: ");
double hours = reader.nextDouble();
return hours;
}
你应该对其他两种方法askSalaryPerHour()
和askTaxPercent()
做同样的事情。