Java 多种方法
我很难理解为什么输出都是0而不是相应的用户输入。我已经将每个变量设置为静态,因此它可以在任何地方使用。我感到困惑的是不知从何而来的随机0Java 多种方法,java,eclipse,Java,Eclipse,我很难理解为什么输出都是0而不是相应的用户输入。我已经将每个变量设置为静态,因此它可以在任何地方使用。我感到困惑的是不知从何而来的随机0 static int hours,payrate,id; static double grosspay; static Scanner sc = new Scanner(System.in); public static void readInput(int hours,int payrate,int id,Scanner sc) { if (id !
static int hours,payrate,id;
static double grosspay;
static Scanner sc = new Scanner(System.in);
public static void readInput(int hours,int payrate,int id,Scanner sc)
{
if (id != -1)
{
System.out.println("Employees id : ");
id = sc.nextInt();
System.out.println("The number of hours worked is : ");
hours = sc.nextInt();
System.out.println("The standard payrate is : ");
payrate = sc.nextInt();
}
else
return;
}
public static double computePay(int hours,double grosspay,int payrate)
{
if(hours <= 160)
grosspay = hours * payrate;
else
grosspay = 160 * payrate + (hours - 160) * 1.5 * payrate;
return grosspay;
}
public static void Output(double grosspay,int id)
{
System.out.println("For Employee id " + id + ", grosspay is " + grosspay);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
readInput(hours,payrate,id,sc);
System.out.println("Grosspay is: " + computePay(hours,grosspay,payrate));
Output(grosspay,id);
System.out.println(id);
static int hours,payrate,id;
静态双格罗斯帕;
静态扫描仪sc=新扫描仪(System.in);
公共静态无效读取输入(整数小时、整数工资率、整数id、扫描仪sc)
{
如果(id!=-1)
{
System.out.println(“员工id:”);
id=sc.nextInt();
System.out.println(“工作小时数为:”);
小时=sc.nextInt();
System.out.println(“标准工资率为:”);
payrate=sc.nextInt();
}
其他的
返回;
}
公共静态双倍计酬(整数小时、双倍总收入、整数工资率)
{
如果(hours您的错误是将参数传递给readInput
。这些参数是本地变量,它们隐藏了具有相同名称的静态成员。因此静态成员不会更新
只需将方法更改为:
public static void readInput() {
if (id != -1) {
System.out.println("Employees id : ");
id = sc.nextInt();
System.out.println("The number of hours worked is : ");
hours = sc.nextInt();
System.out.println("The standard payrate is : ");
payrate = sc.nextInt();
}
}
这将解决这个问题
但是,您的代码看起来不太好。您应该更喜欢实例变量或局部变量,而不是静态变量。将您的方法更改为使用此
引用实际的静态类变量,所有这些都可以:
public static void readInput(int hours,int payrate,int id,Scanner sc) {
if (id != -1) {
System.out.println("Employees id : ");
this.id = sc.nextInt();
System.out.println("The number of hours worked is : ");
this.hours = sc.nextInt();
System.out.println("The standard payrate is : ");
this.payrate = sc.nextInt();
}
return;
}
由于readInput()
中的参数与静态类变量具有相同的名称,因此前一个变量“掩码”后者。因此,您的赋值充耳不闻,不会粘滞。在readInput
方法中,您将结果赋值给传递给该方法的变量。实例变量不受此赋值的影响
我感到困惑的是不知从何而来的随机0
static int hours,payrate,id;
static double grosspay;
static Scanner sc = new Scanner(System.in);
public static void readInput(int hours,int payrate,int id,Scanner sc)
{
if (id != -1)
{
System.out.println("Employees id : ");
id = sc.nextInt();
System.out.println("The number of hours worked is : ");
hours = sc.nextInt();
System.out.println("The standard payrate is : ");
payrate = sc.nextInt();
}
else
return;
}
public static double computePay(int hours,double grosspay,int payrate)
{
if(hours <= 160)
grosspay = hours * payrate;
else
grosspay = 160 * payrate + (hours - 160) * 1.5 * payrate;
return grosspay;
}
public static void Output(double grosspay,int id)
{
System.out.println("For Employee id " + id + ", grosspay is " + grosspay);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
readInput(hours,payrate,id,sc);
System.out.println("Grosspay is: " + computePay(hours,grosspay,payrate));
Output(grosspay,id);
System.out.println(id);
这根本不是随机的,这种行为在:
每个类变量、实例变量或数组组件在创建时都会使用默认值进行初始化(,):
- [……]
- 对于int类型,默认值为零,即0
- [……]
在您的readInput
方法中,您正在将结果分配给传递给该方法的变量。实例变量不受此分配的影响。在哪里?使用什么输入?请您进一步解释一下您的测试和问题。或者删除这些参数,因为它们无论如何都是无用的。仍然需要扫描仪
参考@MuratK.你的意思是静态扫描仪sc=新扫描仪(System.in);
?已经有一个静态扫描仪sc;)哦,是的,没关系。我研究了main方法。这是一个非常清晰的答案,+1…但他打算在readInput()中指定的变量
不是实例变量,它们是静态类级别的变量。但我明白你想说的一点。@TimBiegeleisen类变量是静态的,它们有默认值。