简体   繁体   English

为什么我将null作为输出字符串而不是我想要的?

[英]Why do I get null as my output string instead of what I wanted?

I'm asked to compute a Java program with TestStaff that accepts name, staff id and working perday as inputs from the user and displays the name, staff ID and salary of the staff. 我被要求使用TestStaff计算一个Java程序,该程序接受用户的姓名,职员ID和每天工作作为输入,并显示职员的姓名,职员ID和工资。 But at the end of the output, I got null null 0.0 . 但是在输出的最后,我得到了null null 0.0。 I'm a beginner at this object and class section and I'm really lost. 我是这个对象和类部分的初学者,我真的迷路了。 Can anyone help? 有人可以帮忙吗? Here are my codes: 这是我的代码:

This is the given code: 这是给定的代码:

    class Staff {

       private String name, staffID;

       private double salary;

       private int workingDay;

       public void setStaffInfo(String nm, String id, int wDay){

          name=nm;
          staffID=id;
          workingDay=wDay;
    }

    public void calculateSalary(){

       salary = workingDay * 35.0;

       }
    public double getSalary(){

       return salary;

       }

    public String getName(){

       return name;

       }
    public String getStaffID(){

       return staffID;
       }

    }

    import java.util.Scanner;

Here are the codes for TestStaff: 以下是TestStaff的代码:

    class TestStaff {

       static Scanner console = new Scanner(System.in);
       public static void main(String arg[]){

       Staff staff1= new Staff();

       System.out.print("Enter your name: ");
       String nm=console.nextLine();

       System.out.print("Enter your Staff ID: ");
       String id=console.nextLine();

       System.out.print("Enter your working days: ");
       int wDay=console.nextInt(); 

       System.out.println(staff1.getName() + "\t" + staff1.getStaffID()+ "\t\t" + staff1.getSalary());

       }
     }

You need to use the Staff class set method to initialize its members. 您需要使用Staff类的set方法来初始化其成员。

ie: staff1.setStaffInfo(nm, id, wDay); 即: staff1.setStaffInfo(nm, id, wDay);

What you've done is simply creating 3 new variables and assigning them values, but your Staff objects' members were not set. 您所做的只是简单地创建3个新变量并为其分配值,但是尚未设置Staff对象的成员。
Also you might want to call calculateSalary() before getSalary() because the former initializes the salary variable. 另外,您可能要在getSalary()之前调用calculateSalary() ,因为前者会初始化salary变量。

You are missing to call method staff1.setStaffInfo(nm, id, wDay); 您缺少调用方法staff1.setStaffInfo(nm, id, wDay);

I would suggest to have parameterized constructor with all the instance variables of class Staff, create instance for Staff using that constructor. 我建议使用类Staff的所有实例变量对构造函数进行参数化,并使用该构造函数为Staff创建实例。 That way you won't get null values. 这样,您将不会获得空值。 Suggestion: Create constructor in Staff class as: 建议:在Staff类中创建构造函数为:

Staff(){}

Staff(String name, String staffID, double salary ,  int workingDay){
    this.name = name;
    this.staffID=staffID;
    this.salary=salary;
    this.workingDay = workingDay;
}

And in TestStaff class, you can add these lines: 在TestStaff类中,您可以添加以下行:

Staff staff2 = new Staff(nm,id, 2400, wDay);

    System.out.println(staff2.getName() + "\t" + staff2.getStaffID() + "\t\t" + staff2.getSalary());

But at the end of the output, I got null, null, 0.0. 但是在输出的末尾,我得到了null,null,0.0。

You are not passing any arguments to the setStaffInfo(String nm, String id, int wDay) anywhere in your code so that is why it is showing null, null,0.0 您没有在代码中的任何地方传递任何参数给setStaffInfo(String nm, String id, int wDay) ,所以这就是为什么它显示null,null,0.0

Solution

Pass the following variables to the setStaffInfo(String nm, String id, int wDay) method of the class Staff 通过以下变量到setStaffInfo(String nm, String id, int wDay)之类的方法Staff

String nm=console.nextLine(); String id=console.nextLine(); int wDay=console.nextInt();

Like this: 像这样:

staff1. setStaffInfo(nm,id,wDay)

And an advice 和一个建议

Run your calculateSalary() in getSalary() method like this: 像这样在getSalary()方法中运行您的calculateSalary()

public double getSalary(){
    calculateSalary(); //this will autimatically calculate salary
   return salary;

   }

Then, you will just have to call getSalary() in the main method and it will automatically call calculateSalary() or you can also put that one line of calculateSalary() in getSalary() also. 然后,您只需要在main方法中调用getSalary() ,它将自动调用calculateSalary()或者您也可以将那行的calculateSalary()也放入getSalary()

In the main method you have created the Staff object and stored all the input in some variable but haven't set the input in object. 在main方法中,您已经创建了Staff对象,并将所有输入存储在某个变量中,但尚未在对象中设置输入。 Thats why when you print staff1.getSalary() it returns you the default value of double ie: 0.0. 这就是为什么当您打印staff1.getSalary()时会返回默认值double的原因,即:0.0。 first set the value staff1.setInfo(...) And then print the values. 首先设置值staff1.setInfo(...),然后打印值。

You create a new object from your Class Staff but you have to assign data member to the class and can done from 2 different ways : 您可以通过班级工作人员创建一个新对象,但是您必须将数据成员分配给该班级,并且可以通过2种不同的方式来完成:

1- Your method staff1.setStaffInfo(nm, id, wDay); 1-您的方法staff1.setStaffInfo(nm,id,wDay);

staff1.setStaffInfo(nm, id, wDay);

2- Constructor 2-构造函数

 Staff staff1= new Staff(id, nm, wDay);

Add the constructor below in Staff class this constructor will make you able to create a new object with specifying id, nm and wDay 在Staff类中添加以下构造函数,该构造函数使您能够通过指定id,nm和wDay创建新对象

Staff  (String staffID ,String name , int workingDay) {
this.staffID = staffID;
this.name = name ;
this.workingDay = workingDay ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM