rock paper scissors java
Java
import java.util.Scanner;
import java.util.Random;
public class rps {
public static void main (String args[]){
int input;
int b = 1;
Scanner sage = new Scanner(System.in);
Random rnd = new Random();
System.out.println("Rock Paper Scissors, by Sage!");
System.out.println("Select 1, 2, 3, for Rock, Paper, Scissors");
//Menu Present, pretty bad still
while (b != 0){
int rock = 1, paper = 2, scissors = 3;
input = sage.nextInt();
int randomNumber = rnd.nextInt(3-1+1)+1;
if(randomNumber == rock){
if(input == rock){
System.out.println("Rock vs. Rock, ITS A TIE!");
} else if(input == paper){
System.out.println("Rock vs. Paper! You win!" );
} else if(input == scissors){
System.out.println("Rock vs. Scissors! You lose!");
} //These blocks establish options if the computer got Rock
else if(randomNumber == paper){
if(input == rock){
System.out.println("Paper vs. Rock! You lose!");
} else if(input == scissors){
System.out.println("Paper vs. Scissors! You win!");
} else if(input == paper){
System.out.println("Paper vs. Paper! Its a tie!");
} //These blocks establish the options if comp. got paper
else if(randomNumber == scissors){
if(input == rock){
System.out.println("Scissors vs. Rock! You win!");
} else if(input == scissors){
System.out.println("Scissors vs. Scissors, ITS A TIE!");
} else if(input == paper){
System.out.println("Scissors vs Paper! You lose!");
} //These blocks establish if the computer got scissors.
}
}
rps2 rps2Object = new rps2();
rps2Object.rps2();
}
}
}
}import java.util.Scanner;
public class rps2 {
public void rps2(){
Scanner sage = new Scanner(System.in);
int b;
b = 1;
System.out.println("Play again? Y(8), N(9)?");
int yes= 8, no = 9;
int input;
input = sage.nextInt();
if(input == yes){
System.out.println("Rock,Paper,Scissors!");
}else{
System.out.println("Thanks for playing!");
}
}
}import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
private static boolean playAgain(Scanner scanner) {
System.out.println("Play again? Y(8), N(9)?");
switch (scanner.nextInt()) {
case 8:
System.out.println("Rock, Paper, Scissors!");
return true;
default:
System.out.println("Thanks for playing!");
return false;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
RPSPlayer computer = new RandomComputerPlayer(new Random());
RPSPlayer human = new HumanPlayer(scanner);
System.out.println("Rock Paper Scissors, by 200_success!");
do {
String comp = computer.play();
String you = human.play();
System.out.printf("%s vs. %s", comp, you);
if (you.equals(comp)) {
System.out.println(", IT'S A TIE!");
} else if ( ("Rock".equals(you) && "Scissors".equals(comp)) ||
("Scissors".equals(you) && "Paper".equals(comp)) ||
("Paper".equals(you) && "Rock".equals(comp)) ) {
System.out.println("! You win!");
} else {
assert (("Rock".equals(comp) && "Scissors".equals(you)) ||
("Scissors".equals(comp) && "Paper".equals(you)) ||
("Paper".equals(comp) && "Rock".equals(you)));
System.out.println("! You lose!");
}
} while (playAgain(scanner));
}
}import java.util.Scanner;
public class HumanPlayer implements RPSPlayer {
private final Scanner scanner;
public HumanPlayer(Scanner scanner) {
this.scanner = scanner;
}
public String play() {
System.out.println("Select 1, 2, or 3 for Rock, Paper, Scissors");
int choice = this.scanner.nextInt();
// Keeping things simple, not doing any validation here
return CHOICES[choice - 1];
}
}import java.util.Random;
public class RandomComputerPlayer implements RPSPlayer {
private final Random random;
public RandomComputerPlayer(Random random) {
this.random = random;
}
public String play() {
return CHOICES[this.random.nextInt(CHOICES.length)];
}
}
Also in Java:
- Title
- generate objects with for loop java
- Category
- Java
- Title
- hide elements android
- Category
- Java
- Title
- how to stop a void java
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- java convert String to int
- Category
- Java
- Title
- how to find the divisors of a number in java
- Category
- Java
- Title
- add two numbers bitwise
- Category
- Java
- Title
- convert array to list java
- Category
- Java
- Title
- how to create an array in java
- Category
- Java
- Title
- generic method lambda java
- Category
- Java
- Title
- java 8 add whole array to jsonarray
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- java valeur absolue
- Category
- Java
- Title
- count number of matches in two strings java
- Category
- Java
- Title
- java read file bufferedreader
- Category
- Java
- Title
- change brightness of image in java
- Category
- Java
- Title
- sc.nextline skips
- Category
- Java
- Title
- java loop
- Category
- Java
- Title
- java program to get all cat indices in elasticsearch javadoc
- Category
- Java
- Title
- remove action bar android studio
- Category
- Java
- Title
- array in java
- Category
- Java
- Title
- how to create a JFrame in java
- Category
- Java
- Title
- priorityqueue poll
- Category
- Java
- Title
- java create directory
- Category
- Java
- Title
- java double 2 decimal
- Category
- Java
- Title
- java "->"
- Category
- Java
- Title
- java parse json
- Category
- Java
- Title
- Java system load from resources
- Category
- Java
- Title
- initialize applet in java
- Category
- Java
- Title
- how to open a folder in java swing project
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- java file exists
- Category
- Java
- Title
- java map get the key from value
- Category
- Java
- Title
- convert hashset to array
- Category
- Java
- Title
- caesar cipher java
- Category
- Java
- Title
- java random number
- Category
- Java
- Title
- traversal of list in java
- Category
- Java
- Title
- how to call a static method in java
- Category
- Java
- Title
- removeeventlistener
- Category
- Java
- Title
- java append to array
- Category
- Java
- Title
- system.arraycopy java
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- java loop through arraylist
- Category
- Java
- Title
- how to make array of objects in java and use it
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- how to convert string to double in android studio
- Category
- Java
- Title
- java 8 list stream delete by name
- Category
- Java
- Title
- Java arraylist if you don't want to use add()
- Category
- Java
- Title
- sort array java
- Category
- Java
- Title
- how to create a constructor in java
- Category
- Java
- Title
- java setroundingmode
- Category
- Java
- Title
- leap year checker java
- Category
- Java
- Title
- same method name with different arguments
- Category
- Java
- Title
- java get int from double without rounding
- Category
- Java
- Title
- get random number from enum in java
- Category
- Java
- Title
- what is enum in java
- Category
- Java
- Title
- adb: command not found
- Category
- Java
- Title
- are inner classes inherited
- Category
- Java
- Title
- java array check duplicates
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- how to interrupt a void java
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- how to make stage dragable in javafx
- Category
- Java
- Title
- how to add multiple filter condition in Java stream filter chain
- Category
- Java
- Title
- using class in java
- Category
- Java
- Title
- java how to compare strings
- Category
- Java
- Title
- how to pass parameters to xsl file
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- icon label java
- Category
- Java
- Title
- spigot sounds
- Category
- Java
- Title
- javafx polygon
- Category
- Java
- Title
- java min function
- Category
- Java
- Title
- double round java integer
- Category
- Java
- Title
- how to iterate hashset in java 8
- Category
- Java
- Title
- java stream collect to string
- Category
- Java
- Title
- spigot deal damage
- Category
- Java
- Title
- java singleton
- Category
- Java
- Title
- java flood fill
- Category
- Java
- Title
- java count substring occurrences in string
- Category
- Java
- Title
- python to java converter
- Category
- Java
- Title
- java concatenate strings
- Category
- Java
- Title
- Filebody in java
- Category
- Java
- Title
- java how to get all threads
- Category
- Java
- Title
- html top padding
- Category
- Java
- Title
- java get icon for file type
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- get day name from date in java
- Category
- Java
- Title
- create notification android
- Category
- Java
- Title
- java string length validation regex
- Category
- Java
- Title
- java script print date in YYYY-MM-DD HH:MM:SS format
- Category
- Java
- Title
- bukkit java get player count
- Category
- Java
- Title
- how to pass a float between activities in android studio
- Category
- Java
- Title
- sqrt in java
- Category
- Java
- Title
- java strin to int
- Category
- Java
- Title
- create instance object java
- Category
- Java
- Title
- stackoverflow java enum with constructor
- Category
- Java
- Title
- selection sort in java
- Category
- Java
- Title
- how to play an audio in java
- Category
- Java
- Title
- changing double space to single in java script
- Category
- Java