7 Replies - 750 Views - Last Post: 03 December 2009 - 03:28 PM Rate Topic: -----

#1 verajuno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Paper, rock, and scissors with computer

Posted 01 December 2009 - 09:33 AM

I am working in code for "paper,rock and scissors", when copputer generates these words and compare with input from user. I can't figure out what I am doing wrong, as compiller is giving me "incompatible types
found : java.lang.String
required: char"". Please, help!



import java.util.Random;//import for random generator
import java.util.Scanner;//import for user input
import java.util.regex.Pattern;

public class Five {

	public static void main(String[] args){

Scanner scan = new Scanner(System.in);
System.out.println("Enter rock, paper or scissors: ");
String word = scan.nextLine();

Random rand = new Random();
int a = genOperator(rand.nextInt(3));


}


public static char genOperator(int a){
		switch(a){
			case 0: return "scissors";
			case 1: return "paper";
			case 2: return "rock";
			default: return "";
		}
	}

public static String evaluateWord(String operator){

	while(scan.hasNext(){}

			do{
				switch(operator){
			case "scissors": return "\nYou picked scissors.\nComputer picked scissors.\nNodoby won";
			case "paper": return "\nYou picked scissors.\nComputer picked paper.\nYou won";
			case "rock": return "\nYou picked scissors.\nComputer picked rock.\nComputer won";}
			}while (word.hasNext("scissors"));

			do{
				switch(operator){
			case "scissors": return "\nYou picked rock.\nComputer picked scissors.\nYou won";
			case "paper": return "\nYou picked rock.\nComputer picked paper.\nComputer won";
			case "rock": return "\nYou picked rock.\nComputer picked rock.\nNobody won";}
			}while (word.hasNext("rock"));

			do{
				switch(operator){
			case "scissors": return "\nYou picked paper.\nComputer picked scissors.\nComputer won";
			case "paper": return "\nYou picked paper.\nComputer picked paper.\nNobody won";
			case "rock": return "\nYou picked paper.\nComputer picked rock.\nYou won";}
			}while (word.hasNext("paper"));
   }
}


This post has been edited by verajuno: 01 December 2009 - 09:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Paper, rock, and scissors with computer

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Paper, rock, and scissors with computer

Posted 01 December 2009 - 09:35 AM

The reason you're getting that error is because you can't use a switch-case structure on Strings.

Bummer. I've wanted to do that before, too. :)
Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Paper, rock, and scissors with computer

Posted 01 December 2009 - 10:03 AM

@Locke we will be able to do it once Java 7 is finalized and released :P
Was This Post Helpful? 0
  • +
  • -

#4 verajuno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Re: Paper, rock, and scissors with computer

Posted 01 December 2009 - 11:07 AM

View PostLocke, on 1 Dec, 2009 - 08:35 AM, said:

The reason you're getting that error is because you can't use a switch-case structure on Strings.

Bummer. I've wanted to do that before, too. :)




If I cannot use switch-statement to String, than how else I can generate randomly words to be able to comnpare with user output?
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Paper, rock, and scissors with computer

Posted 01 December 2009 - 11:17 AM

View Posterik.price, on 1 Dec, 2009 - 11:03 AM, said:

@Locke we will be able to do it once Java 7 is finalized and released :P


Really? Awesome. I haven't read into Java 7 at all yet.
Was This Post Helpful? 0
  • +
  • -

#6 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Paper, rock, and scissors with computer

Posted 01 December 2009 - 12:48 PM

At least that's what the link in the featured topic about Java 7 said
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,963
  • Joined: 15-July 08

Re: Paper, rock, and scissors with computer

Posted 01 December 2009 - 04:34 PM

instead of what you have:
 switch(operator){
			case "scissors": return "\nYou picked scissors.\nComputer picked scissors.\nNodoby won";
			case "paper": return "\nYou picked scissors.\nComputer picked paper.\nYou won";
			case "rock": return "\nYou picked scissors.\nComputer picked rock.\nComputer won";}  



You have to use if statements:
			if (operator.equals("scissors) 
				return "\nYou picked scissors.\nComputer picked scissors.\nNodoby won";
			else if (operator.equals("paper")
				return "\nYou picked scissors.\nComputer picked paper.\nYou won";
			else if (operator.equals("rock")
				return "\nYou picked scissors.\nComputer picked rock.\nComputer won";}


Was This Post Helpful? 0
  • +
  • -

#8 verajuno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 09

Re: Paper, rock, and scissors with computer

Posted 03 December 2009 - 03:28 PM

oh, ok. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1