2011-06-29 67 views
0

這裏是代碼在那裏我遇到了問題:「找不到符號:變量輸入」錯誤

import java.util.*; 
public class Game { 
public static final int ROCK = 1; 
public static final int PAPER = 2; 
public static final int SCISSORS = 3; 

public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    String name = intro(); 
    int numRounds = numRounds(name); 
    game(name, numRounds); 
} 

public static String intro() { 
    System.out.println("Welcome to Rock Paper Scissors. I, the computer, will be your opponent."); 
    System.out.print("Please type in your name and press return: "); 
    String name = input.nextLine(); 
    return name; 
} 

public static int numRounds(String name) { 
    System.out.println("Welcome " + name + "."); 
    System.out.println("All right " + name + ". How many games would you like to play?"); 
    System.out.print("Enter the number of rounds you want to play and press return: "); 
    int numRounds = input.nextInt(); 
    return numRounds; 
} 

當我使用掃描儀來獲取用戶名和輪數,他們會值喜歡玩,我得到的錯誤。我只想返回這些值在主函數中使用。

任何幫助,將不勝感激。

+0

「遊戲」方法的實現在哪裏? –

回答

2

中有你想要的兩種方法沒有input變量 - 將它作爲methhod參數:

public static String intro(Scanner input) { .. } 
public static int numRounds(String name, Scanner input) { .. } 

...

String name = intro(input); 
int numRounds = numRounds(name, input); 

除此之外,沒有game()方法 - 定義它。

+0

我有遊戲方法,我只是沒有發佈在這裏,但謝謝你! – Dennis

+0

不客氣。隨時爲您標記最適合您接受的答案 – Bozho

0

我有點生疏,但我覺得這行:

String name = input.nextLine(); 

嘗試使用可變input,但是這並不在其範圍內:它不能「達到」,所以它不是一個初始化變量。

要麼把本該函數:

Scanner input = new Scanner(System.in); 

或者使input「全局」,宣佈它作爲例如public

0

您的intro()numRounds()方法所引用的input對其不可見。嘗試將其作爲變量傳入。

0

inputmain()方法內聲明,所以它不存在該方法之外。您應該聲明它爲main()方法之外的類變量,以便intro()可以訪問它。