Java 我如何修复错误;从静态内容引用的非静态变量“;?
我在《编程入门》(java)课程中完成了这个项目。它应该创造一个弦乐器使用的方法,并发挥乐器等。。我已经在C#中完成过这个项目一次,但不知出于什么原因,我似乎无法让它在java中工作。我一直收到一个错误,“非静态变量不能从静态内容引用。我在这里搜索了,到目前为止,没有任何解决方案对我有效。请帮助。谢谢 `/* *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */Java 我如何修复错误;从静态内容引用的非静态变量“;?,java,Java,我在《编程入门》(java)课程中完成了这个项目。它应该创造一个弦乐器使用的方法,并发挥乐器等。。我已经在C#中完成过这个项目一次,但不知出于什么原因,我似乎无法让它在java中工作。我一直收到一个错误,“非静态变量不能从静态内容引用。我在这里搜索了,到目前为止,没有任何解决方案对我有效。请帮助。谢谢 `/* *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */ 您也必须将内部类声明为静态的。否则它是非静态的(您可以
您也必须将内部类声明为静态的。否则它是非静态的(您可以从非静态内部类访问外部this指针).查看右侧的所有链接:)欢迎使用SO。请先搜索您的问题,然后再发布一个新问题。前几天我回答了一个与此完全相同的问题。看看这些链接是否有帮助,我就不需要问我的问题了。。。。
package egreenhornfinal;
import java.util.Scanner;
/**
*
* @author Eddie
*/
public class EGreenhornFinal {
class Guitar
{
boolean isTuned; //Guitar starts off not tuned
boolean isPlaying; //Guitar is not playing at start
boolean isBroken; //Guitar has broken a string
boolean isFixed; //Broken String has been fixed
boolean isNotPlaying; //Guitar has stopped playing
boolean songOver; //The song is over now.
//array for Guitar strings
char[] GuitarStrings = { 'e', 'A', 'D', 'G', 'B', 'E' };
private int numberofStrings = 6;
//default Guitar object
public Guitar()
{
isTuned = false;
isPlaying = false;
isBroken = false;
isFixed = false;
isNotPlaying = false;
songOver = false;
System.out.println("The Guitar is not playing, and it is not tuned. All Strings are intact.\n");
}
public Guitar(boolean T, boolean P)
{
isTuned = T;
isPlaying = P;
}
public boolean playGuitar()
{
System.out.println("The Guitar is playing!\n");
return isPlaying = true;
}
public boolean stopPlaying()
{
System.out.println("The Guitar has stopped playing.\n");
return isNotPlaying = true;
}
public boolean tuneGuitar()
{
System.out.println("The Guitar is being tuned!\n");
return isTuned = true;
}
public boolean brokenString()
{
System.out.println("One of the strings was too tight, it has broken!\n");
return isBroken = true;
}
public boolean fixedString()
{
System.out.println("The broken string has been fixed, the guitar is now playing again.\n");
return isFixed = true;
}
public boolean EndOfSong()
{
System.out.println("The Guitar has stopped playing because the song is over.\n");
return songOver = true;
}
}
/**
*
* @param args
*/
public static void main(String[] args)
{
String WantToPlay = "Y";
do
{
Guitar MyGuitar = new Guitar();
//Error-- Non static variable being referenced from static content??
boolean varIsTuned = false;
varIsTuned = MyGuitar.tuneGuitar();
boolean varIsPlaying = false;
varIsPlaying = MyGuitar.playGuitar();
boolean varIsNotPlaying = true;
varIsNotPlaying = MyGuitar.stopPlaying();
boolean varIsBroken = false;
varIsBroken = MyGuitar.brokenString();
boolean varIsFixed = false;
varIsFixed = MyGuitar.fixedString();
boolean varsongOver = false;
varsongOver = MyGuitar.EndOfSong();
System.out.println("Would you like to play the Guitar again? (enter Y for yes, and N for no)");
Scanner inUsr = new Scanner(System.in);
WantToPlay = inUsr.nextLine();
}
while (WantToPlay == "Y");
}
}
`