2015-02-05 59 views
0

我正在製作一個Nim遊戲,並且我添加了一個單人遊戲模式。爲此,我創建了一個方法(不確定這是你所稱的方法),並在用戶的回合結束時調用它。彈出錯誤消息說:「變量未解決」。它是我的代碼的位置?請幫忙。如何解決變量?

我試着再次創建變量,只是調用變量。

這是迄今爲止代碼:

import java.util.Scanner; 
import java.util.Random; 
public class nim { 

    public static void computerMove() { 
     Random randA = new Random(); 
     int pcPilePick = randA.nextInt(10); 

     Object amtInPile; 
     if (pcPilePick == 1) { 
      amtInPile = stones1; 
     } 
     if (pcPilePick == 2) { 
      amtInPile = stones2; 
     } 
     if (pcPilePick == 3) { 
      amtInPile = stones3; 
     } 
    } 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     System.out.println("Welcome!"); 
     System.out.println("This is the game of Nim."); 
     System.out.println("[1]Single player or [2]multiplayer?"); 
     String choice = input.nextLine(); 
     int gameMode = Integer.parseInt(choice); 

     int stones1 = 9;// These variables will be used 
     int stones2 = 9;// in order to subtract a number 
     int stones3 = 9;// of stones from the pile. 

     int tStones = stones1 + stones2 + stones3; 

     while (tStones > 0) { 
      System.out.println("[1] Pile 1: " + stones1); //This stack will display 
      System.out.println("[2] Pile 2: " + stones2); //the stone count for 
      System.out.println("[3] Pile 3: " + stones3); //each pile after the first move. 

      System.out.println("From which pile would you like to take?"); 
      String aMove = input.nextLine(); 
      System.out.println("How many stones would you like to take?"); 
      String bMove = input.nextLine(); 
      int pilePick = Integer.parseInt(aMove); 
      int amtInPile = 0; 
      if (pilePick == 1) { 
       amtInPile = stones1; 
      } 
      if (pilePick == 2) { 
       amtInPile = stones2; 
      } 
      if (pilePick == 3) { 
       amtInPile = stones3; 
      } 

      int stonePick = Integer.parseInt(bMove); 

      /*All the while loops after this comment are 
      *used in order to prevent stupid answers. 
      */ 
     //legal move 
      //illegal move 
      if (pilePick > 3 || stonePick > amtInPile) { 
       System.out.println("But nothing happened!"); 

       System.out.println("That move is invalid"); 
       pilePick = 0; 
       stonePick = 0; // ensure that for an illegal move, nothing will change. 
      } 
      if (pilePick == 1) {   //This stack of code will 
       stones1 = stones1 - stonePick; 
      } 
      if (pilePick == 2) {   // subtract stones based on the 
       stones2 = stones2 - stonePick; 
      } 
      if (pilePick == 3) {   // pile input and stone input. 
       stones3 = stones3 - stonePick; 
      } 
      tStones = stones1 + stones2 + stones3; 
      if (stones1 + stones2 + stones3 > 0) { 
       System.out.println("Taking " + stonePick + " stones from stack " + pilePick); 
      } 
      if (gameMode == 1) { 
       computerMove(); 
      } 

     } 
     System.out.println("You lose!"); 
    } 

} 
+1

確切的錯誤是什麼? –

+0

它看起來好像您試圖引用僅在您的'computerMove'方法中的'main'方法中定義的變量。將這些變量設置爲「private static」並將它們放入你的班級中。 –

+0

什麼是確切的錯誤和*哪裏*是確切的錯誤? –

回答

2

變量stones1stones2stones3僅已在main方法內上下文中,它們在computerMove方法

內未定義你可以將它們定義爲static類變量,但這是壞習慣的滑坡的開始,相反,修改computerMove的方法來接受信息,它需要

public static void computerMove(int stone1, int stone2, int stone3) { 
    Random randA = new Random(); 
    int pcPilePick = randA.nextInt(10); 

    Object amtInPile; 
    if (pcPilePick == 1) { 
     amtInPile = stones1; 
    } 
    if (pcPilePick == 2) { 
     amtInPile = stones2; 
    } 
    if (pcPilePick == 3) { 
     amtInPile = stones3; 
    } 
} 

現在呼籲computeMove做一些事情,但它沒有上下文超越方法本身,所以你可能會需要返回值

public static int computerMove(int stone1, int stone2, int stone3) { 
    //... 
    return amtInPile; 
} 

,你可以然後維持從中調用它的同樣的背景下...

int computerPile = 0; 
//... 
if (gameMode == 1) { 
    computerPile = computerMove(stones1, stones2, stones3); 
} 

看看Passing Information to a Method or a Constructor更多細節

0

在如果從方法computeMove線8-> 16語句,您引用未定義的變量stones1, stones2, stones3你。你需要聲明這些變量來引用它們,或者將它們作爲參數傳遞(這可能是你想要的)。

public static void computerMove(int stones1, int stones2, int stones3){ 
    Random randA = new Random(); 
    int pcPilePick = randA.nextInt(10); 

    Object amtInPile; 
    if (pcPilePick==1){ 
     amtInPile = stones1; 
    } 
    if (pcPilePick==2){ 
     amtInPile = stones2; 
    } 
    if (pcPilePick==3){ 
     amtInPile = stones3; 
    } 
} 

和更改線87的函數調用來:

computerMove(stones1, stones2, stones3);