0

我正在编写一个程序以“O”显示灯的级别,但是当我编译时,它显示“找不到符号”,我已经声明了“消息”和“亮度”,还有什么我想声明的吗?类 Lamp 和类 TestLamp 我保存在不同的文件中,当我编译 Lamp 时,它没有显示错误。但是编译TestLamp时显示“找不到符号”

class Lamp {

    // Sub-task 1: Declare and initialize data member with default value
    int brightness=1;

    // Sub-task 2: Define a method to indicate the brightness level of lamp
    String getBrightness() {
        String message = "";
        while(brightness>0) { 
            brightness--;
            message += "O"; 
        } 

        return message;
    }

    // Sub-task 3: Define a method to update the brightness of the lamp
    void setBrightness(int b) {

        if(b<1 || b>5) 
            brightness=2;
        else
            brightness=b;

    }
}

class TestLamp {
    public static void main (String[] args) {
        // Sub-task 4: Declare and create 3 lamp objects
        Lamp lamp1,lamp2,lamp3;

        // Sub-task 5: Adjust the lamp brightness according to the requirement
        lamp1.setBrightness(3);
        lamp2.setBrightness(10);

        // Sub-task 6: Display the information of the created lamps
        lamp1.getBrightness();
        System.out.println("Lamp1"+lamp1.message);
        lamp2.getBrightness();
        System.out.println("Lamp2"+lamp2.message);
    }
}
4

9 回答 9

5

you are just creating the reference Lamp lamp1,lamp2,lamp3; but you are not creating any object create object first like the below

Lamp lamp1=new Lamp();

The scope of String message is with in the method getBrightness() so lamp1.message will give you error

So to print message either you can define String message at class level or use lamp1.getBrightness()

Please see the below working code

class Lamp {

// Sub-task 1: Declare and initialize data member with default value
    int brightness=1;
    String message = "";
// Sub-task 2: Define a method to indicate the brightness level of lamp
String getBrightness() {

    while(brightness>0) { 
        brightness--;
        message += "O"; 
    } 


    return message;
}

// Sub-task 3: Define a method to update the brightness of the lamp
void setBrightness(int b) {

    if(b<1 || b>5) 
        brightness=2;
    else
        brightness=b;


}


  }

 class ddd {
public static void main (String[] args) {
    // Sub-task 4: Declare and create 3 lamp objects
    Lamp lamp1 = new Lamp();
    Lamp lamp2=new Lamp();

    // Sub-task 5: Adjust the lamp brightness according to the requirement
    lamp1.setBrightness(3);
    lamp2.setBrightness(10);

    // Sub-task 6: Display the information of the created lamps
    lamp1.getBrightness();
      System.out.println("Lamp1"+lamp1.message);
    lamp2.getBrightness();
      System.out.println("Lamp2"+lamp2.message);


}
      }
于 2013-11-08T07:44:31.937 回答
3

在您的 setBrightness() 方法之前,您应该实例化灯对象。

Lamp lamp1 = new Lamp();

对所有灯对象执行相同的操作。

然后改变你的

System.out.println("Lamp1"+lamp1.message);

System.out.println("Lamp1"+lamp1.getBrightness());
于 2013-11-08T07:46:40.117 回答
3

您使用lamp1.message但课堂上没有message字段Lamp

此外,您没有初始化 的实例Lamp,这将导致另一个编译时错误。(感谢 JasonC,我已经习惯了 IDE,以至于忘记了基本的东西)。

于 2013-11-08T07:43:37.160 回答
3

You haven't instantiated your lamp objects, and your Lamp class lacks a message field

于 2013-11-08T07:44:54.743 回答
2

更正了错误。

Error 1:
System.out.println("Lamp1"+lamp1.getBrightness()); //changed from lamp1.message
System.out.println("Lamp1"+lamp1.getBrightness()); //changed from lamp2.message

Error 2:
        lamp1 = new Lamp(); //missing instance creation
        lamp2 = new Lamp();//missing instance creation

/* 如果你打算使用lamp3和lamp4,也创建那个实例*/

修复后的工作代码:

 class Lamp {

// Sub-task 1: Declare and initialize data member with default value
    int brightness=1;

// Sub-task 2: Define a method to indicate the brightness level of lamp
String getBrightness() {
    String message = "";
    while(brightness>0) { 
        brightness--;
        message += "O"; 
    } 


    return message;
}

// Sub-task 3: Define a method to update the brightness of the lamp
void setBrightness(int b) {

    if(b<1 || b>5) 
        brightness=2;
    else
        brightness=b;


}


  }

 class TestLamp {
public static void main (String[] args) {
    // Sub-task 4: Declare and create 3 lamp objects
    Lamp lamp1,lamp2,lamp3;

    lamp1 = new Lamp();
    lamp2 = new Lamp();

    // Sub-task 5: Adjust the lamp brightness according to the requirement
    lamp1.setBrightness(3);
    lamp2.setBrightness(10);

    // Sub-task 6: Display the information of the created lamps
    lamp1.getBrightness();
      System.out.println("Lamp1"+lamp1.getBrightness());
    lamp2.getBrightness();
      System.out.println("Lamp2"+lamp2.getBrightness());


}
      }
于 2013-11-08T07:46:01.000 回答
1

在 Testlamp 类中为 Lamp 创建一个对象

Lamp lamp1=new Lamp();
于 2013-11-08T07:46:42.383 回答
1

首先:

  • 您尝试在 System.out.printline 中调用 Lamp 类中没有消息变量,请使用 System.out.println("Lamp1"+lamp1.getBrightness()); 反而

接下来,要涵盖的一些约定事项:

  • 将源文件中的类之一公开是件好事 -> public class TestLamp {
  • 大多数 getter 用于检索私有/受保护变量,因为它来自类实例内部。在您的情况下,如果您想返回一些字符串表示形式,最好调用方法 toString() 或 getDispleyText()
于 2013-11-08T07:56:50.250 回答
1

重要的是要记住在 Java 中Object objectName;实际上并没有创建一个新对象。它对对象进行指针/引用。首次创建时,它被设置为地址 0(空)。要实际创建您需要使用new关键字的对象。在您的情况下,您应该执行以下操作...

class TestLamp 
{
    public static void main (String[] args) 
    {
        // Sub-task 4: Declare and create 3 lamp objects
        Lamp lamp1,lamp2,lamp3;

        lamp1 = new Lamp();
        lamp2 = new Lamp();
        lamp3 = new Lamp();

        // Sub-task 5: Adjust the lamp brightness according to the requirement
        lamp1.setBrightness(3);
        lamp2.setBrightness(10);

        // Sub-task 6: Display the information of the created lamps
        lamp1.getBrightness();
        System.out.println("Lamp1"+lamp1.message);
        lamp2.getBrightness();
        System.out.println("Lamp2"+lamp2.message);
    }
}

编辑:此外,主方法无法访问 Lamp 中的字符串消息,因为它在函数中。而是使用您为此疯狂的方法 getBrightness()。

        System.out.println("Lamp1" + lamp1.getBrightness(););
        System.out.println("Lamp2" + lamp2.getBrightness(););
于 2015-12-28T16:51:27.783 回答
0

你尝试使用lamp1.message,但message不是一个领域的灯。你有:

    // Sub-task 2: Define a method to indicate the brightness level of lamp
    String getBrightness() {
        String message = "";
        while(brightness>0) { 
            brightness--;
            message += "O"; 
        } 


        return message;
    }

然后你继续使用它:

        lamp1.getBrightness();
        System.out.println("Lamp1"+lamp1.message);

但你的意思可能是:

        System.out.println("Lamp1"+lamp1.getBrightness());

请注意,您正在使用该方法的返回值,该getBrightness()方法返回您描述的字符串。

顺便说一下,按照惯例,属性 getter 和 setter 通常获取和设置相同的值类型。更常见的做法是让您int getBrightness()返回当前亮度值(与 完全相反setBrightness(int)),并添加一个特殊方法,例如String getBrightnessMessage()生成您正在使用的字符串。

于 2013-11-08T07:48:40.970 回答