0

I am trying to make a rock paper scissors game in java. i have my base code here

import java.util.Scanner; 
import java.util.Random;
public class RPSBase
{
  public static void main(String args[])
  {
    Random rndm = new Random();
    int c =0 + rndm.nextInt(3);
    Scanner c2 = new Scanner(System.in);
    String pc = c2.next();
    switch (c)
    {
      case 1: 
        String choice = "r";
        char ch = choice.charAt(0);
        break;
      case 2:
        choice = "p";
        ch = choice.charAt(0);
        break;
      case 3:
        choice = "s";
        ch = choice.charAt(0);
        break;
    }
    switch (ch)
    {
      case 'r':
        if (pc == "r")
          System.out.println("It's a tie");
        else if (pc == "p")
          System.out.println("win");
        else if (pc == "s")
          System.out.println("lose");
        break;
      case 'p':
        if (pc == "p")
          System.out.println("It's a tie");
        else if (pc == "s")
          System.out.println("win");
        else if (pc == "r")
          System.out.println("lose");
        break;
      case 's':
        if (pc == "s")
          System.out.println("It's a tie");
        else if (pc == "r")
          System.out.println("win");
        else if (pc == "p")
          System.out.println("lose");
        break;
    }
  }
}

for some reason when i compile the program i get this error

1 error found:
File: C:\Users\Larry\RPSBase.java  [line: 26]
Error: ch cannot be resolved to a variable

Why do i get this error and how do i fix it? I have tried switch(choice) as well and that didn't work either.

derpyherp
  • 455
  • 4
  • 9
  • 18

2 Answers2

2

You either need to declare ch above the switch (c), or declare ch in every case of the switch.

And since you seem to be wanting to use ch at a later point, you need this snippet:

char ch = '\u0000';
switch (c)
{
  case 1: 
    String choice = "r";
    ch = choice.charAt(0);
    break;
  case 2:
    String choice = "p";
    ch = choice.charAt(0);
    break;
  case 3:
    String choice = "s";
    ch = choice.charAt(0);
    break;

Note the declaring (char ch) at the top, and in the cases there are just assignments.

UPDATE: The same goes for String choice, however for this one it seems that it is better to declare it in every case.

A lot of the code could be improved, but I'm just answering your question here, you can for example just type ch = 'r' instead of String choice = "r"; ch = choice.charAt(0);

skiwi
  • 66,971
  • 31
  • 131
  • 216
  • 1
    A primitive cannot be `null`. – Sotirios Delimanolis Dec 08 '13 at 21:22
  • You cannot initialize a `char` as `null` (see [this post](http://stackoverflow.com/questions/14844793/comparing-a-character-to-check-if-it-is-null)). And if you don't initialize you'll get a compile error. – eebbesen Dec 08 '13 at 21:22
  • @SotiriosDelimanolis My mistake there (not typing it in IDE), fixed it. – skiwi Dec 08 '13 at 21:22
  • 1
    Note that you still have to initialize it to some default otherwise the compiler will complain when you try to use it outside the `switch`. – Sotirios Delimanolis Dec 08 '13 at 21:23
  • @SotiriosDelimanolis See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html , I don't think initializing primitives to a default is needed. However it is indeed the case in a `switch` with `Object`s, except `String`. – skiwi Dec 08 '13 at 21:26
  • You will get a `The local variable ch may not have been initialized` compilation error if you try to use an un-itialized variable. A `switch` block without a `default` might not initialize it if none of the cases match. – Sotirios Delimanolis Dec 08 '13 at 21:32
  • In other words, a variable needs to be definitely initialized before it is used. – Sotirios Delimanolis Dec 08 '13 at 21:36
  • @SotiriosDelimanolis Updated the answer, sorry for the confusion. – skiwi Dec 08 '13 at 21:39
  • @skiwi ok that fixed the error but know when i run it, it takes my input and does nothing – derpyherp Dec 08 '13 at 21:50
1

When declaring char "ch" in the case of a switch statement, it can only be used for that case with that switch statement, in order to fix this you must declare:

char ch;

outside of the switch; right after the declaration of string pc.

I suggest using an IDE to further help you if you, an IDE will automatically pick this up and tell you to correct the error.

user2494817
  • 697
  • 4
  • 12