Guys, kindly help me.

How can user enter a character?
or
How can I use the Enumerated Type for this?

Thanks:)

import java.util.Scanner;

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

char x,y;

System.out.print("Player 1:");
    x=input.nextLine();
System.out.print("Player 2:");
    y=input.nextLine();



        switch(x)
        {
            case 'P':
                switch (y)
                {
                    case 'R':
                        System.out.print("Player 1 Wins!");
                        break;
                    case 'S':
                        System.out.println("Player 2 Wins!");
                        break;
                    case 'P':
                        System.out.println("Draw!");
                        break;
                }
        }

        switch (x)
        {
            case 'R':
                switch(y)
                {
                    case 'S':
                        System.out.println("Player 1 Wins!");
                        break;
                    case 'P':
                        System.out.println("Player 2 Wins!");
                        break;
                    case 'R':
                        System.out.println("Draw!");
                        break;
                }
        }

        switch (x)
        {
            case 'S':
                switch (y)
                {
                    case 'P':
                        System.out.println("Player 1 Wins!");
                        break;
                    case 'R':
                        System.out.println("Player 2 Wins!");
                        break;
                    case ('S'):
                        System.out.println("Draw!");
                        break;
                }
        }
    }
}

You have three switch (x) statements, each with one case inside. Have one switch(x) statement with three case statements instead.

I don't see a nextChar command in Scanner, so grab a whole line, then parse the String and grab the fist and hopefully only character in it. You might look at Scanner and/or String and see if there is something that skips any leading white space in the input.

http://java.sun.com/javase/6/docs/api/java/util/Scanner.html


I don't know where you are going with the enumerated types, so here's a link:

http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html

I imagine you are trying to assign 0 to paper, 1 to rock, 2 to scissor and use enumerated types that way. You could then do some math if you chose (i.e. multiply player 1's choice times 3 and add player 2's choice, which results in a number from 0 to 8, so you could have one switch statement).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.