import java.util.Scanner;
public class Salary{
String employeeId="";
double wage=0;
static double tax=0;
double realWage=0;
public double computeTax(){
tax=Tools.computeTax(wage);
return tax;
}
public double computeRealWage(){
realWage=wage-this.computeTax();
return realWage;
}
public static void main(String[] args){
Salary salary=new Salary();
Scanner sc=new Scanner (System.in);
System.out.println("职工号");
salary.employeeId=sc.nextLine();
System.out.println("税前工资");
salary.wage=sc.nextDouble();
System.out.println("职工号"+salary.employeeId+"税前
工资"+salary.wage+"扣税"+salary.computeTax()+"实发
工资"+salary.computeRealWage());
}
}
class Tools
{
final static int START_TAX=2000;
double tax;
public double computeTax(double wage)
{
if(wage-START_TAX<=0){
tax=0;
return tax;
}
if(wage-START_TAX<=500)
{
tax=(wage-2000)*0.05;
return tax;
}
if((wage-START_TAX>500)&&(wage-START_TAX<=2000))
{
tax=(wage-2000)*0.1-25;
return tax;
}
if(wage-START_TAX>2000)
{
tax=(wage-2000)*0.15-125;
return tax;
}
}
}
------解决方案--------------------
- Java code
import java.util.Scanner;class Tools{ final static int START_TAX = 2000; double tax; public double computeTax(double wage) { if (wage - START_TAX <= 0) { tax = 0; } if (wage - START_TAX <= 500) { tax = (wage - 2000) * 0.05; } if ((wage - START_TAX > 500) && (wage - START_TAX <= 2000)) { tax = (wage - 2000) * 0.1 - 25; } if (wage - START_TAX > 2000) { tax = (wage - 2000) * 0.15 - 125; } return tax;// 把大括号里面的return语句去掉,直接在外面return。 }}public class Salary{ String employeeId = ""; double wage = 0; static double tax = 0; double realWage = 0; public double computeTax() { tax = new Tools().computeTax(wage);// 这里没必要把此函数设置为static的。 return tax; } public double computeRealWage() { realWage = wage - this.computeTax(); return realWage; } public static void main(String[] args) { Salary salary = new Salary(); Scanner sc = new Scanner(System.in); System.out.println("职工号"); salary.employeeId = sc.nextLine(); System.out.println("税前工资"); salary.wage = sc.nextDouble(); System.out.println("职工号" + salary.employeeId + "税前工资" + salary.wage + "扣税" + salary.computeTax() + "实发 工资" + salary.computeRealWage()); }}
------解决方案--------------------
静态属性和方法只能被静态到代码块访问,在非静态到代码块中访问静态到方法当然不行了,你可以将被调用的方法设置成静态的
------解决方案--------------------
如果必须要这样调用的话,得这样。供楼主参考
- Java code
import java.util.Scanner;class Tools{ final static int START_TAX = 2000; private static double tax;//这里要设置为static public static double computeTax(double wage) ////这里也要设置为static { if (wage - START_TAX <= 0) { tax = 0; } if (wage - START_TAX <= 500) { tax = (wage - 2000) * 0.05; } if ((wage - START_TAX > 500) && (wage - START_TAX <= 2000)) { tax = (wage - 2000) * 0.1 - 25; } if (wage - START_TAX > 2000) { tax = (wage - 2000) * 0.15 - 125; } return tax; }}public class Salary{ String employeeId = ""; double wage = 0; static double tax = 0; double realWage = 0; public double computeTax() { tax =Tools.computeTax(wage); return tax; } public double computeRealWage() { realWage = wage - this.computeTax(); return realWage; } public static void main(String[] args) { Salary salary = new Salary(); Scanner sc = new Scanner(System.in); System.out.println("职工号"); salary.employeeId = sc.nextLine(); System.out.println("税前工资"); salary.wage = sc.nextDouble(); System.out.println("职工号" + salary.employeeId + "税前工资" + salary.wage + "扣税" + salary.computeTax() + "实发工资" + salary.computeRealWage()); }}
------解决方案--------------------