Here's what I have (it's just an excerpt from my entire code):
int num = (int) Math.random()*100;
switch(num)
{
case 0 : compChoice = "R";break;
case 1 : compChoice = "P";break;
case 2 : compChoice = "S";break;
}
How can I get it to only come up with a random number of 0, 1, or 2?
in a return statement I have later on, it says that the letter that is acquired here results in a "null"
Here's the entire code if it helps:
import java.util.Scanner;
import static java.lang.System.*;
public class RockPaperScissors
{
private String playChoice;
private String compChoice;
public RockPaperScissors()
{
}
public RockPaperScissors(String player)
{
playChoice = player;
}
public void setPlayers(String player)
{
playChoice = player;
int num = (int) Math.random()*100 %3;
switch(num)
{
case 0 : compChoice = "R";break;
case 1 : compChoice = "P";break;
case 2 : compChoice = "S";break;
}
out.print(compChoice);
}
public String determineWinner()
{
String winner="";
if(playChoice == "R")
{
switch(compChoice)
{
case "R" : winner = "!Draw Game!";break;
case "P" : winner = "!Computer wins <<Paper Covers Rock>>!";break;
case "S" : winner = "!Player wins <<Rock Breaks Scissors>>!";break;
}
}
else if(playChoice == "P")
{
switch(compChoice)
{
case "R" : winner = "!Player wins <<Paper Covers Rock>>!";break;
case "P" : winner = "!Draw Game!";break;
case "S" : winner = "!Computer wins <<Scissors Cuts Paper>>!";break;
}
}
else if(playChoice == "S")
{
switch(compChoice)
{
case "R" : winner = "!Computer wins <<Rock Breaks Scissors>>!";break;
case "P" : winner = "!Player wins <<Scissors Cuts Paper>>!";break;
case "S" : winner = "!Draw Game!";break;
}
}
return winner;
}
public String toString()
{
String output="";
output = "player had " + playChoice + "\n computer had " + compChoice + "\n " + determineWinner();
return output;
}
}
Here's my runner class since someone pointed out I'm not calling any method anywhere:
import java.util.Scanner;
import static java.lang.System.*;
public class Lab10d
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
char response;
//add in a do while loop after you get the basics up and running
String player = "";
out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
//read in the player value
player = keyboard.next();
RockPaperScissors game = new RockPaperScissors(player);
game.determineWinner();
out.println(game);
while(response == response)
{
out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
player = keyboard.next();
game.setPlayers(player);
game.determineWinner();
out.println(game + "\n");
out.println("would you like to play again? (y/n):: ");
response = (char) keyboard.next();
}
}
}
Use the Modulo operator :
int num = (int) Math.random()*100 % 3;
Math.random() returns a pseudo-random value between 0 and 1.
Multiplying by 100 makes it a number (with decimal values) between 0 and 100.
Casting to (int)
drops the decimal value - same as Math.floor() if you have that in your language.
Modulo 3 is the remander of diving by 3: For example, 55 / 3 = 18 remainder 1 (in other words, (18 * 3) + 1 = 55).
Math.random()
returns a floating point value between 0 and 1.
If you multiply that by 3, you get a floating point value between 0 and 3.
If you cast the floating point value to an integer, you get 0, 1, or 2.
ie int v = (int) (Math.random() * 3);
If simple multiplication scares you, there is a java.util.Random class. You can do:
/** Re-usable source of random numbers */
private Random rand = new Random();
/** Choose a value of 0, 1, or 2 */
public int myChoice() {
int v = rand.nextInt(3);
return v;
}
Using a Random instance allows you to plug-in a more advanced random number generator, which would be necessary if you intended to do stochastic experiments.
Based on the different comments I wanted to try another approach. I just copied your code into my IDE, and ran it.
public class NewMain {
public static void main(String[] args) {
int num;
String compChoice = "";
for (int ii = 0; ii < 10; ii++) {
num = (int) (Math.random() * 100) % 3;
switch (num) {
case 0:
compChoice = "R";
break;
case 1:
compChoice = "P";
break;
case 2:
compChoice = "S";
break;
}
System.out.println(num + " " + compChoice);
}
}
}
Gregmac's approach is OK, it's just lacking a set of (). If your code is actually the code you gave above and compChoice is always null there's something horribly wrong.
I thought a good way to do this would just be to round the number after multiplying it. I couldn't get it to work the way a few of you answered but how about this?:
var a=Math.random()*3;
var num=Math.round(a);
switch(num)
{
case 0 : compChoice = "R";break;
case 1 : compChoice = "P";break;
case 2 : compChoice = "S";break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.