无法对非静态字段进行静态引用.... - java eclipse school project

时间:2014-01-11 21:30:54

标签: java eclipse

首先让我宣布我是Java的菜鸟。我正在学习计算机科学,我需要有关此代码的帮助。这是一个游戏,但问题是我的nStones,computerMove和humanMove方法因为一些非静态字段而无法工作。我问过我班上的每个人,我的老师,现在我正在尝试上网。我看起来遍布stackoverflow我是一个菜鸟,有些东西没有意义。这是:

import java.util.*;


public class GameforCompSci
{ /*
To win you must take the last variables so when stone count is zero you lose and the other player wins
    you see how many are left there for you determine a strategy to get to be the last   one to take it
   */


 public static boolean endgame = false;
 private int winner = 0;
 private int nStones;
 public static void main(String[] args)
 {
 nStones = (int)(Math.random() * (51 - 10) + 10);
 System.out.println("Welcome to the game!");
 System.out.println("Stones: " + nStones);
 int whostarts = 0;
 whostarts = (int)(Math.random() * (0 - 2) + 2);
 if (whostarts == 1)
 { 
  System.out.println("You start.");
  while (nStones > 0){
    humanMove(nStones);
    System.out.println("Stones: " + nStones);
    computerMove(nStones);
    System.out.println("Stones: " + nStones);
  }
  }
  else
  { System.out.println("Computer starts.");
  while (nStones > 0){
   computerMove(nStones);
   System.out.println("Stones: " + nStones);
   humanMove(nStones);
   System.out.println("Stones: " + nStones);
 }
 } 


 //endgame logic
 if (endgame = true)
 {
   switch(winner){
   case 1:
     System.out.println("You win!");
    break;
   case 2:
    System.out.println("You lose, computer wins!");
    break;
  default:
    System.out.println("Something went wront please try again!");
   }

   System.exit(0);
  }
  } 

    public int computerMove(int nStones)
     {
   int picked = 0;
   if (nStones <= 0)
    {
    winner = 1;
    endgame = true;
    }
    if (nStones > 10){
     picked = (int)(Math.random() * (4 - 1) + 1);
    nStones = nStones - picked;
    }
    else
    {
     switch(nStones)
     {
   case 10:
    picked = 3;
    break;
  case 9:
    picked = 3;
    break;
  case 8:
    picked = 2;
    break;
  case 7:
    picked = 1;
    break;
  case 6:
    picked = 2;
    break;
  case 5:
    picked = 1;
    break;
  case 4:
    picked = 1;
    break;
  case 3:
    picked = 3;
    break;
  case 2:
    picked = 2;
    break;
  case 1:
    picked = 1;
    break;
  default:
    endgame=true;
    break;
  }
  nStones = nStones - picked;
  }
  return nStones;


  }

public int humanMove(int nStones)
{    
if (nStones <= 0)
{
  winner = 2;
  endgame = true;
}
Scanner in = new Scanner(System.in);
System.out.println("How many stones do you take? (Only 1 to 3)");
int n = in.nextInt();
nStones = nStones - n;
if (n == 1 || n == 2 || n == 3)
{
  System.out.println("You picked: " + n);
  nStones = nStones - n;
 }
 else
 {
   System.out.println("Invalid input");
   System.out.println("No stones taken");
   n=0;
 }
 return nStones;

  }

  }

 /* 
   2 players take turns taking stones from a pile. On each move a player must take one,   two or three stones. The player who takes the last stone wins.


 b) Write a method computerMove:
 /* 
 * Returns the correct number of stones to take 
 * (according to the winning strategy) 
 * when nStones remain in the pile.
  * If such a move is not possible, returns a random number of stones to take
 * Precondition: nStones > 0
 */


 //c) Finish the method humanMove.
 /* 
 * Prompts the user to take a number of stones.
 * If the move is valid returns the number of stones taken; 
 * otherwise displays the correct error message –- 
 * "You are allowed to take 1, 2, or 3 stones, only." 
 * Or "Can't take that many; only <nStones> stones are left in the pile."
 * -- and returns -1;
 * Precondition: nStones > 0


  d) Write a main method to:
  /* 
  * Generate a random number of stones between 10 and 50
  * Store the number of stones in nStones and keep track of it.
 * Alternate calling the players, "Computer" and "Human"
 * Determine when there is a winner, announce it and end the program.
  * You may use more methods to do these steps. */

3 个答案:

答案 0 :(得分:2)

你不能在静态方法中使用非静态成员,这是没有意义的:非静态成员(如变量winner或方法humanMove)属于类的实例(在这种情况下是GameforCompSci的实例)。 至于静态成员,他们不属于GameforCompSci的特定实例

这样,您就无法参考,例如winner方法中的main(非静态变量)(这是静态的),因为它实际上并不存在于此上下文中:您不在{{1 }}

答案 1 :(得分:1)

将静力学视为“一件事”。当某些东西是静止的时,它只有一个。 每次引用它时,都指向同一个对象。

当某些不是静态时,这意味着该对象的每个实例都有一个单独的副本。

因此,静态方法只能访问其他静态变量,或者作为参数传递给它的东西。它无法访问非静态的,因为它不知道你在谈论它的哪个实例。

如果您想学习Java,了解静态/非静态非常关键。当你明白何时使用它们时,它将为你节省大量的工作。

答案 2 :(得分:0)

静态方法只能引用其他静态方法和静态字段。 main方法必须是静态方法,但您的humanMovecomputerMove方法不是静态的,而endgame字段是静态的,winner和{ {1}}不是静态的。

有两种方法可以解决这个问题。您可以向其他字段和方法添加nStones关键字,也可以创建一个名为static的新的非静态方法,并将play的当前内容放入其中其中的方法。然后将main更改为不是静态的,并使用以下main方法启动程序:

endgame

这将创建一个类的实例,然后调用实例方法public static void main(String[] args) { GameforCompSci game = new GameforCompSci(); game.play(); } ,您可以从中引用其他实例方法和实例变量。