为了账号安全,请及时绑定邮箱和手机立即绑定

字符串未显示,打印“null”

字符串未显示,打印“null”

青春有我 2023-06-14 16:46:08
今天刚开始学习Java,所以请原谅我的业余错误。问题是它没有显示每个人最喜欢的颜色,而是显示“null”,我有 Pawn 的编码经验,所以我猜我没有正确创建字符串或者只是没有以某种方式填充它。谢谢!我试过这些代码,但我得到的是“null”,而不是每个人最喜欢的颜色。import java.io.*;public class Employee {   String name;   int age;   String designation;   String favoriteColor;   double salary;   // This is the constructor of the class Employee   public Employee(String name) {      this.name = name;   }   // Assign the age of the Employee  to the variable age.   public void empAge(int empAge) {      age = empAge;   }   /* Assign the designation to the variable designation.*/   public void empDesignation(String empDesig) {      designation = empDesig;   }   /* Assign the salary to the variable salary.*/   public void empSalary(double empSalary) {      salary = empSalary;   }   public void empFavoriteColor(String empColor) {       favoriteColor = empColor;   }   /* Print the Employee details */   public void printEmployee() {      System.out.println("Name:"+ name );      System.out.println("Age:" + age );      System.out.println("Designation:" + designation );      System.out.println("Salary:" + salary);      System.out.println("Favorite color:" + favoriteColor);   }}import java.io.*;public class EmployeeTest {   public static void main(String args[]) {      /* Create two objects using constructor */      Employee empOne = new Employee("James Smith");      Employee empTwo = new Employee("Mary Anne");      Employee empThree = new Employee("Alex Johnson");      // Invoking methods for each object created      empOne.empAge(26);      empOne.empDesignation("Senior Software Engineer");      empOne.empSalary(1000);      empOne.printEmployee();      empOne.empFavoriteColor("Green");      empTwo.empAge(21);      empTwo.empDesignation("Software Engineer");      empTwo.empSalary(500);      empTwo.printEmployee();      empTwo.empFavoriteColor("Blue");   }}
查看完整描述

2 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

您需要printEmployee();先致电empFavoriteColor(),如下所示:


import java.io.*;

public class EmployeeTest {


   public static void main(String args[]) {

      /* Create two objects using constructor */

      Employee empOne = new Employee("James Smith");

      Employee empTwo = new Employee("Mary Anne");

      Employee empThree = new Employee("Alex Johnson");


      // Invoking methods for each object created

      empOne.empAge(26);

      empOne.empDesignation("Senior Software Engineer");

      empOne.empSalary(1000);

      empOne.empFavoriteColor("Green");

      empOne.printEmployee();


      empTwo.empAge(21);

      empTwo.empDesignation("Software Engineer");

      empTwo.empSalary(500);

      empTwo.empFavoriteColor("Blue");

      empTwo.printEmployee();


      empThree.empAge(16);

      empThree.empDesignation("Software Developer" );

      empThree.empSalary(250);

      empThree.empFavoriteColor("Orange");

      empThree.printEmployee();

   }

}


查看完整回答
反对 回复 2023-06-14
?
拉丁的传说

TA贡献1789条经验 获得超8个赞

您在调用printEmployee()before empFavoriteColor(),因此打印时该字符串为 null 。



查看完整回答
反对 回复 2023-06-14
  • 2 回答
  • 0 关注
  • 201 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信