【发布时间】:2014-05-09 13:37:45
【问题描述】:
我有这个项目是由于我的编程介绍(java)课程。它应该使用方法创建一个弦乐器,并演奏乐器等。我已经在 C# 中完成过一次这个项目,但是出于某种原因,我似乎无法让它在 java 中工作。我不断收到一个错误,“不能从静态内容中引用非静态变量。我在这里搜索过,到目前为止,没有一个解决方案对我有用。请帮助。谢谢!!
` /* * 要更改此许可标头,请在项目属性中选择许可标头。 * 要更改此模板文件,请选择工具 |模板 * 并在编辑器中打开模板。 */
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");
}
}
`
【问题讨论】:
-
查看右侧的所有链接 :)
-
欢迎来到 SO。请先搜索您的问题,然后再发布之前已被多次询问过的新问题
-
前几天我回答了一个完全一样的问题。见stackoverflow.com/q/23429937/217324
-
如果这些链接有用,我就不需要问我的问题了......
标签: java