Java 由于编译错误,无法在主方法中引用我的方法
我创造了下面的石头,布,剪刀游戏。我试图创建一个名为winner的方法,告诉用户谁赢得了游戏。我创建了一个变量end,它是方法winner的输出。当我执行$System.out.printlnwinnerweapon时,它会给我一个编译错误。有人能帮忙调试这个问题吗?我是java的新手 错误:错误:无法对类型游戏中的非静态方法winnerjava.lang.String、java.lang.String进行静态引用Java 由于编译错误,无法在主方法中引用我的方法,java,methods,Java,Methods,我创造了下面的石头,布,剪刀游戏。我试图创建一个名为winner的方法,告诉用户谁赢得了游戏。我创建了一个变量end,它是方法winner的输出。当我执行$System.out.printlnwinnerweapon时,它会给我一个编译错误。有人能帮忙调试这个问题吗?我是java的新手 错误:错误:无法对类型游戏中的非静态方法winnerjava.lang.String、java.lang.String进行静态引用 import java.util.Scanner; public class G
import java.util.Scanner;
public class Game {
String end;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String computerWeapon;
System.out.print("Human, choose your weapon: ");
String weapon = input.next().toLowerCase();
int randomNumber = (int)(Math.random()*(3));
if (randomNumber == 0) {
computerWeapon = "rock";
}
else if (randomNumber == 1) {
computerWeapon = "scissors";
}
else {
computerWeapon = "paper";
}
System.out.println("Computer chooses: " + computerWeapon);
System.out.println(winner( weapon, computerWeapon));
}
public static String winner(String weapon, String computerWeapon){
if (weapon.equals("rock")){
if(computerWeapon.equals("rock")) {
end= "Tie!";
}
else if (computerWeapon.equals("scissors")) {
end = "Human wins!";
}
else if (computerWeapon.equals("paper")) {
end= "Computer wins!";
}
}
else if (weapon.equals("paper")){
if(computerWeapon.equals("rock")) {
end= "Human wins!";
}
else if (computerWeapon.equals("scissors")) {
end= "Computer wins!";
}
else if (computerWeapon.equals("paper")) {
end= "Tie!" ;
}
}
else if (weapon.equals("scissors")){
if(computerWeapon.equals("rock")) {
end= "Computer wins!";
}
else if (computerWeapon.equals("scissors")) {
end= "Tie!";
}
else if (computerWeapon.equals("paper")) {
end= "Human wins!";
}
}
return end;
}
}
您试图从静态方法winner中访问非静态end变量,这是不正确的。 要修复它,您可以将其设置为静态,也可以将其设置为winner方法中的局部变量,因为您正在返回它。首先 您不能从非静态方法引用静态变量。要理解这一点,您需要理解静态和非静态之间的区别。 静态变量是类变量,它们属于具有 它们只有一个实例,仅在第一个实例中创建。每次创建类的对象时,都会初始化非静态变量。 在代码中,end param是非静态的。但是您正在从静态方法调用此参数,该方法是winner。你不能这样做 在静态方法中,您试图返回字符串。全局变量end param将是winner方法中的内部变量。所以,像这样更改代码
public static String winner(String weapon, String computerWeapon) {
String end = "";
if (weapon.equals("rock")) {
if (computerWeapon.equals("rock")) {
end = "Tie!";
} else if (computerWeapon.equals("scissors")) {
end = "Human wins!";
} else if (computerWeapon.equals("paper")) {
end = "Computer wins!";
}
} else if (weapon.equals("paper")) {
if (computerWeapon.equals("rock")) {
end = "Human wins!";
} else if (computerWeapon.equals("scissors")) {
end = "Computer wins!";
} else if (computerWeapon.equals("paper")) {
end = "Tie!";
}
} else if (weapon.equals("scissors")) {
if (computerWeapon.equals("rock")) {
end = "Computer wins!";
} else if (computerWeapon.equals("scissors")) {
end = "Tie!";
} else if (computerWeapon.equals("paper")) {
end = "Human wins!";
}
}
return end;
}
我认为错误是由非静态的end属性引起的。有没有办法使其成为静态的?您是否在winner方法之外使用它?如果不是,那么为什么不在该方法中将其声明为变量呢。通过在属性前面添加static,可以将属性声明为static。您只需添加一个修饰符:static String end;。尽管我同意NielsNet的观点,但我不确定您是否真的希望在类级别声明变量。@NielsNet谢谢您,我在winner方法中将其作为局部变量。