Java - 表达式的非法开始

标签 java

所以我对这个错误进行了一些搜索并找到了一些结果。然而,这些似乎都不是答案。我确信问题很简单,而且我太累了,无法弄清楚,但我不知道我的错误是什么。这些方法旨在成为增加或减少音量的变异方法。

Television.java:94: error: illegal start of expression
public int increaseVolume()
^
Television.java:94: error: ';' expected
public int increaseVolume()
                        ^
Television.java:103: error: illegal start of expression
public int decreaseVolume()
^
Television.java:103: error: ';' expected
 public int decreaseVolume()
                        ^
Television.java:106: error: reached end of file while parsing
}
^
5 errors

这是发生错误的代码末尾:

public class Television
{
private String MANUFACTURER = "None"; // This represents the manufacturer of the TV set.
private int SCREEN_SIZE = 0; // This represents the size of screen of the TV set.
private boolean powerOn; // This represents the state the TV is in (On or Off)
private int channel; // This represents the channel the TV set is on.
private int volume; // This represents the volume value of the TV set.

public static void main(String[] args)
  {

  }  

 /**
  Constructor
  @param brand The manufacturer brand of the TV set.
  @param size The screen size of the TV set.
 */

public Television(String brand, int size)
{
 MANUFACTURER = brand;
 SCREEN_SIZE = size;
 powerOn = false;
 volume = 20;
 channel = 2;
}

 /**
     The getVolume method gets the volume of the TV set.
     @return The current volume on the TV set as an integer.
 */
public int getVolume()
  {
     return volume;
  }

/**
  The getChannel method gets the channel of the TV set.
  @return The current channel on the TV set as an integer.
*/
  public int getChannel()
    {
      return channel;
    }

/**
  The getScreenSize method gets the screen size of the TV set.
  @return The screen size as an integer.
*/
 public int getScreenSize()
  {
    return SCREEN_SIZE;
  }

/** 
  The getManufacturer method gets the brand manufacturer of the TV set.
  @return The manufacturer name as a string.
*/
 public String getManufacturer()
  {
    return MANUFACTURER;
  }

/**
  The setChannel method is designed to set the channel for the user.
  @return The channel on the TV that is set.
*/
public int setChannel(int chan)
{
  return channel = chan;
}

/**
  The power method is designed to take the current power state and turn it on or off based on its current state.
  @return The power state after it is changed.
*/
public boolean power()
  {
 if (powerOn = true)
 {
   return powerOn = !powerOn;
 }
 else
 {
    return powerOn = false;
 }

 /**
  The increaseVolume method is designed to increase the volume of the TV set in increments of 1.
  @return The volume of the TV set as it is after being increased.
 */
 public int increaseVolume()
   {
    return volume += 1;
   }

 /**
  The decreaseVolume method is designed to decrease the volume of the TV set in increments of 1.
  @return The volume of the TV set as it is after being decreased.
 */
 public int decreaseVolume()
   {
    return volume -= 1;
   }
  }
}

我要么真的很累,要么就是个白痴。抱歉,如果这么明显。

最佳答案

您忘记关闭方法 power() 并在类末尾删除一个 }

除此之外,您还需要将此 if (powerOn = true) 更改为 if (powerOn == true)。您正在将值分配给 powerOn 而不是测试相等性

关于Java - 表达式的非法开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22144076/

相关文章:

java - 什么是NullPointerException,我该如何解决?

Java 字符串首字母缩写

java - Spring security Basic Authentication - 401 Unauthorized with correct credentials

java - Android从xlsx读取数据

java - Spring @RequestParam DateTime 格式为 ISO 8601 日期可选时间

java - 为每个元素创建对象

java - 在没有 java :comp/env prefix 的情况下访问 JNDI 数据源

java - 使用 IabHelper 时出现内存泄漏(应用内计费 v3)

java - 当按下微调器下拉按钮时,全屏模式被禁用

java - 我可以使用哪种数据类型?