需要澄清这个java程序的错误

时间:2012-12-08 21:38:33

标签: java methods

我试图修复电视方法,但我一直得到同样的错误:找不到符号构造函数Television(java.lang.String,int)。如果有人告诉我为什么会收到此错误消息,我将不胜感激。 这是我的代码:

import java.util.Scanner;
public class Television
{
    private String manufacturer;       //the brand name
    private int SCREEN_SIZE;           //the size of the television screen.
    private boolean powerOn;           //the value true if the power is on
    private int channel;               //the value of the station that the television is showing.
    private int volume;                //a number value representing the loudness

    Scanner keyboard = new Scanner(System.in);
    /*
     *Constructor to assign the values to manufacturer and SCREEN_SIZE
     *@param the manufacturer value
     *@param the SCREEN_SIZE value
     */
    public Television(String sony, int size, int chan)
    {
        manufacturer = sony;
        SCREEN_SIZE = size;
        channel = chan;

        powerOn = false;
        channel = 2;
        volume = 20;
    }
    public int getChannel()
    {
        return channel;
    }
    public int getVolume()
    {
        return volume;
    }
    public String getManufacturer()
    {
        return manufacturer;
    }
    public int getScreenSize()
    {
        return SCREEN_SIZE;
    }
    public void setChannel(int channel)
    {
        channel = keyboard.nextInt();
    }
    public void power()
    {
        if (true)
        {
            powerOn = !powerOn;
        }
        else
        {
            powerOn = powerOn;
        }
    }
    public void increaseVolume()
    {
        volume = volume + 1;
    }
    public void decreaseVolume()
    {
        volume = volume - 1;
    }
public static void main(String[] args)
{
    //create a Scanner object to read from the keyboard
    Scanner keyboard = new Scanner (System.in);
    //declare variables
    int station; //the user’s channel choice


    //declare and instantiate a television object
    Television bigScreen = new Television("Toshiba", 55);



    //turn the power on
    bigScreen.power();
    //display the state of the television
    System.out.println("A " + bigScreen.getScreenSize() +
    bigScreen.getManufacturer() + " has been turned on.");
    //prompt the user for input and store into station
    System.out.print("What channel do you want? ");
    station = keyboard.nextInt();
    //change the channel on the television
    bigScreen.setChannel(station);
    //increase the volume of the television
    bigScreen.increaseVolume();
    //display the the current channel and volume of the television
    System.out.println("Channel: " + bigScreen.getChannel() +
    " Volume: " + bigScreen.getVolume());
    System.out.println("Too loud!! I am lowering the volume.");
    //decrease the volume of the television
    bigScreen.decreaseVolume();
    bigScreen.decreaseVolume();
    bigScreen.decreaseVolume();
    bigScreen.decreaseVolume();
    bigScreen.decreaseVolume();
    bigScreen.decreaseVolume();
    //display the current channel and volume of the television
    System.out.println("Channel: " + bigScreen.getChannel() +
    " Volume: " + bigScreen.getVolume());
    System.out.println(); //for a blank line
    //HERE IS WHERE YOU DO TASK #5
}
}

2 个答案:

答案 0 :(得分:4)

您的电视构造函数接受三个参数,一个字符串和两个整数,您只是传递两个参数。

Television bigScreen = new Television("Toshiba", 55);

应该是

Television bigScreen = new Television("Toshiba", 55, there should be another int argument here);
从我的代码中我可以看到,你应该将int(通道)作为第三个参数传递给构造函数。

答案 1 :(得分:2)

构造函数定义为在传递两个参数时接收3个参数

public Television(String sony, int size, int chan);

Television bigScreen = new Television("Toshiba", 55);