Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法对非静态字段进行静态引用….-java eclipse学校项目_Java_Eclipse - Fatal编程技术网

无法对非静态字段进行静态引用….-java eclipse学校项目

无法对非静态字段进行静态引用….-java eclipse学校项目,java,eclipse,Java,Eclipse,首先,让我宣布,我是Java的一名noob。我正在学习计算机科学,我需要这段代码的帮助。这是一个游戏,但问题是我的NSTONE、computerMove和humanMove方法不起作用,因为一些非静态场的东西。我问过班上的每个人,我的老师,现在我正在尝试上网。我已经找遍了所有的地方,我是个笨蛋,有些东西没有意义。这是: import java.util.*; public class GameforCompSci { /* To win you must take the last vari

首先,让我宣布,我是Java的一名noob。我正在学习计算机科学,我需要这段代码的帮助。这是一个游戏,但问题是我的NSTONE、computerMove和humanMove方法不起作用,因为一些非静态场的东西。我问过班上的每个人,我的老师,现在我正在尝试上网。我已经找遍了所有的地方,我是个笨蛋,有些东西没有意义。这是:

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. */

您不能在静态方法中使用非静态成员,这毫无意义:非静态成员(如变量winner或方法humanMove)属于类的实例,在本例中是GameforCompSci的实例。 至于静态成员,它们不属于GameforCompSci的特定实例


这样,您就不能在主方法中引用例如winner非静态变量,该变量是静态的,因为它实际上不存在于此上下文中:您不在GameforCompSci的实例中。

将静态视为一件事。当某物是静止的,它只有一个。 每次引用它时,都会指向同一对象

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

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


如果您想学习Java,理解静态/非静态是非常关键的。当您了解何时使用每个静态方法时,它将为您节省大量工作。

静态方法只能引用其他静态方法和静态字段。主方法必须是静态方法,但humanMove和computerMove方法不是静态的,而endgame字段是静态的,winner和nStones也不是静态的

有两种方法可以解决此问题。您可以向这些其他字段和方法添加一个静态关键字,或者创建一个新的非静态方法,例如play,并将主方法的当前内容放入其中。然后将endgame更改为非静态,并使用以下主要方法启动程序:

public static void main(String[] args) {
    GameforCompSci game = new GameforCompSci();
    game.play();
}

这将创建类的实例,然后调用实例方法play,从中可以引用其他实例方法和实例变量。

您看到了什么错误?预期的行为是什么?你试过做什么?是什么让你怀疑这是一个关于静力学的问题呢?我想我要问的是如何让它工作,或者如何让move和nstone在实例中工作。我是否需要不使用main方法?如果需要,如何使其运行?我想我要问的是如何使其工作,或者如何使move和nstone在实例中工作。我是否需要不使用main方法?如果需要,如何使其运行?