2017-06-02 94 views
1

我对Java比较陌生,我正在编写一个简单的程序来玩摇滚,纸,剪刀。这是代码:变量未被方法识别

import java.util.Random; 
import java.util.Scanner; 

public class Main { 
    public static String comChoice() { 
    Random rand = new Random(); 
    int num = rand.nextInt(299) + 1; 

    if (num >= 1 && num <= 99) { 
     String pick = "SCISSORS"; 
    } else if (num >= 100 && num <= 199) { 
     String pick = "ROCK"; 
    } else if (num >= 200 && num <= 299) { 
     String pick = "PAPER"; 
    } 
    return pick; 
    } 

    public static void main(String args[]) { 
    int wins = 0; 
    int losses = 0; 
    int ties = 0; 
    while (1 == 1) { 
     Scanner s = new Scanner(System.in); 
     System.out.println("Would you like to play Rock Paper Scissors? Y/N"); 
     String n = s.next(); 

     if (n.equalsIgnoreCase("Y")) { 
     Scanner t = new Scanner(System.in); 
     System.out.println("Enter your pick: rock, paper, or scissors"); 
     String userChoice = t.next(); 

     String pick = comChoice(); 

     if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("SCISSORS")) { 
      System.out.println("TIE"); 
      ties++; 
     } else if (userChoice.equalsIgnoreCase("ROCK") && pick.equalsIgnoreCase("SCISSORS")) { 
      System.out.println("WIN"); 
      wins++; 
     } else if (userChoice.equalsIgnoreCase("PAPER") && pick.equalsIgnoreCase("SCISSORS")) { 
      System.out.println("LOSE"); 
      losses++; 
     } else if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("ROCK")) { 
      System.out.println("LOSE"); 
      losses++; 
     } else if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("PAPER")) { 
      System.out.println("WIN"); 
      wins++; 
     } else { 
      System.out.println("Enter a valid choice"); 
     } 
     } else { 
     System.out.println("You won " + wins + " matches"); 
     System.out.println("You tied " + ties + " matches"); 
     System.out.println("You lost " + losses + " matches"); 
     break; 
     } 
    } 
    } 
} 

我得到在我的方法的错误,这样说:

Main.java:23: error: cannot find symbol 
    return pick; 
     ^
symbol: variable pick 
location: class Main 
1 error 

exit status 1 

我无法弄清楚如何解决这个错误。我很感激你输入以及任何其他一般性的建议

感谢

+2

了解java中的变量作用域! –

+0

您正在声明条件中的变量。这些条件后不可见。 –

回答

0

pick超出范围。尝试在comChoice方法开始时声明。

因此:

public static String comChoice() { 
    String pick=null; 
    Random rand = new Random(); 
    int num = rand.nextInt(299) + 1; 

    if (num >= 1 && num <= 99) { 
     pick = "SCISSORS"; 
    } else if (num >= 100 && num <= 199) { 
     pick = "ROCK"; 
    } else if (num >= 200 && num <= 299) { 
     pick = "PAPER"; 
    } 
    return pick; 
} 
1

你的变量只是在声明if可见。阅读关于范围。

更改为:

import java.util.Scanner; 
import java.util.Random; 

public class Main { 
    public static String comChoice() { 
     Random rand = new Random(); 
     int num = rand.nextInt(299) + 1; 
     String pick = null; 
     if (num >= 1 && num <= 99) { 
      pick = "SCISSORS"; 
     } else if (num >= 100 && num <= 199) { 
      pick = "ROCK"; 
     } else if (num >= 200 && num <= 299) { 
      pick = "PAPER"; 
     } 
     return pick; 
    } 

.... 
} 
+0

好的,谢谢你的帮助! –

0

可变pick超出范围。您必须在所有if else声明之外声明它。如果所有if else条件都失败,则comChoice方法将无法找到必须返回的变量pick(因为它仅在if else块内声明)。

纠正代码

import java.util.Scanner; 
import java.util.Random; 

public class Main 
{ 
    public static String comChoice() 
    { 
    Random rand = new Random(); 
    int num = rand.nextInt(299) + 1; 

    String pick = ""; 

    if(num >= 1 && num <= 99) 
    { 
     pick = "SCISSORS"; 
    } 
    else if (num >= 100 && num <= 199) 
    { 
     pick = "ROCK"; 
    } 
    else if (num >= 200 && num <= 299) 
    { 
     pick = "PAPER"; 
    } 
    return pick; 
    } 
    public static void main (String args[]) 
    { 
    int wins = 0; 
    int losses = 0; 
    int ties = 0; 
    while (1 == 1) 
    { 
     Scanner s = new Scanner (System.in); 
     System.out.println("Would you like to play Rock Paper Scissors? Y/N"); 
     String n = s.next(); 

     if (n.equalsIgnoreCase("Y")) 
     { 
     Scanner t = new Scanner (System.in); 
     System.out.println("Enter your pick: rock, paper, or scissors"); 
     String userChoice = t.next(); 

     String pick = comChoice(); 

     if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("SCISSORS")) 
     { 
      System.out.println("TIE"); 
      ties++; 
     } 
     else if (userChoice.equalsIgnoreCase("ROCK") && pick.equalsIgnoreCase("SCISSORS")) 
     { 
      System.out.println("WIN"); 
      wins++; 
     } 
     else if (userChoice.equalsIgnoreCase("PAPER") && pick.equalsIgnoreCase("SCISSORS")) 
     { 
      System.out.println("LOSE"); 
      losses++; 
     } 
     else if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("ROCK")) 
     { 
      System.out.println("LOSE"); 
      losses++; 
     } 
     else if (userChoice.equalsIgnoreCase("SCISSORS") && pick.equalsIgnoreCase("PAPER")) 
     { 
      System.out.println("WIN"); 
      wins++; 
     } 
     else 
     { 
      System.out.println("Enter a valid choice"); 
     } 
     } 
     else 
     { 
     System.out.println("You won " + wins + " matches"); 
     System.out.println("You tied " + ties + " matches"); 
     System.out.println("You lost " + losses + " matches"); 
     break; 
     } 
    } 
    } 
} 
0

你宣布在if else结构中的可变pick。这样做会导致无法从if else结构外部访问变量的问题。简而言之,变量pick的范围限于您的if else结构。除了if else结构之外,你必须声明你的变量(并初始化它,否则你会得到一个错误)。像这样:

String pick = null; 
if(num >= 1 && num <= 99) { 
... 
... 
...// All your if's and else's 
} 
return pick; 

希望这有助于!