If else statements
- if statements
- if-else statements
- Bool
- Rock paper Scissors using if-elseif-else
- Switch Case
- De Morgan's Law
int test = 1;
if (test == 1) {
System.out.println("test pass");
}
int test = 0;
if (test == 1) {
System.out.println("test pass");
}
else {
System.out.println("test failed");
}
boolean t = true;
boolean f = false;
if (t) {
System.out.println("1");
}
if (f) {
System.out.println("2");
}
if (f && !t) {
System.out.println("3");
}
if (t && !f) {
System.out.println("4");
}
if (t || f) {
System.out.println("5");
}
if (!t || !f) {
System.out.println("6");
}
if ((t && !f) && (t || f)) {
System.out.println("7");
}
if ((!t || (t && f)) || t ) {
System.out.println("8");
}
if (!((f || !t) || (f && t))) {
System.out.println("9");
}
if (!((f && !t) || (f || t))) {
System.out.println("10");
}
int test1 = 3;
int test2 = 1;
if (test1 == 0) {
int test2 = 10;
}
else if (test1 == 3) {
int test2 = 5;
int test1 = 0;
System.out.println("Nice");
}
else {
int test2 = 3;
}
if (test1 > test2) {
System.out.println("Work");
}
else if (test1 < test2) {
System.out.println("Job");
}
else {
System.out.println("Code");
}
public class Test {
public static void main(String[] args)
{
String s1 = "HELLO";
String s2 = "HELLO";
String s3 = new String("HELLO");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
}
}
Test.main(null);
public class RPS
{
public static final String ROCK = "ROCK";
public static final String PAPER = "PAPER";
public static final String SCISSORS = "SCISSORS";
//Scanner class to detect what move user plays
public static String getPlayerMove()
{
Scanner in = new Scanner(System.in);
String input = in.next();
String playermove = input.toUpperCase();
System.out.println("Player move is: "+ playermove);*
return playermove;
}
//Computer mobve is randomly generated through random function
public static String getComputerMove()
{
String computermove;
Random random = new Random();
int input = random.nextInt(3)+1;
if (input == 1)
computermove = RPS.ROCK;
else if(input == 2)
computermove = RPS.PAPER;
else
computermove = RPS.SCISSORS;
System.out.println("Computer move is: " + computermove);
System.out.println();
return computermove;
}
public static void main(String args[])
{
System.out.println("Enter any t of the following inputs: ");
System.out.println("ROCK");
System.out.println("PAPER");
System.out.println("SCISSORS");
System.out.println();
String playerMove = getPlayerMove();
String computerMove = getComputerMove();
//Tie
if (playerMove.equals(computerMove))
System.out.println("Game is Tie !!");
// Player = Rock, Computer = Paper
else if (playerMove.equals(RPS.ROCK) && computerMove.equals(RPS.PAPER))
System.out.println("Computer Wins");
// Player = Rock, Computer = Scissors
else if (playerMove.equals(RPS.ROCK) && computerMove.equals(RPS.SCISSORS))
System.out.println("Player Wins");
// Player = Paper, Computer = Scissors
else if (playerMove.equals(RPS.PAPER) && computerMove.equals(RPS.SCISSORS))
System.out.println("Computer Wins");
// Player = Paper, Computer = Rock
else if (playerMove.equals(RPS.PAPER) && computerMove.equals(RPS.ROCK))
System.out.println("Player Wins");
// Player = Scissors, Computer = Paper
else if (playerMove.equals(RPS.SCISSORS) && computerMove.equals(RPS.PAPER))
System.out.println("Player Wins");
// Player = Scissors, Computer = Rock
else if (playerMove.equals(RPS.SCISSORS) && computerMove.equals(RPS.ROCK))
System.out.println("Computer Wins");
// Player = Paper, Computer = Scissors
else if (playerMove.equals(RPS.PAPER))
System.out.println(computerMove.equals(RPS.SCISSORS) ? "Computer Wins": "Player wins");
// if input isn't rock, paper, or scissors
else
System.out.println("There was an error, please try again");
}
}
RPS.main(null);
public class RPS
{
public static final String ROCK = "ROCK";
public static final String PAPER = "PAPER";
public static final String SCISSORS = "SCISSORS";
//Scanner class to detect what move user plays
public static String getPlayerMove()
{
Scanner in = new Scanner(System.in);
String input = in.next();
String playermove = input.toUpperCase();
System.out.println("Player move is: "+ playermove);
return playermove;
}
public static void main(String args[])
{
System.out.println("Enter any t of the following inputs: ");
System.out.println("ROCK");
System.out.println("PAPER");
System.out.println("SCISSORS");
System.out.println();
String playerMove = getPlayerMove();
String computerMove = getComputerMove();
switch (playerMove) {
case ROCK:
// if user input ROCK
if (playerMove.equals(computerMove))
System.out.println("Game is Tie !!");
else if (playerMove.equals(RPS.ROCK) && computerMove.equals(RPS.PAPER))
System.out.println("Computer Wins");
else if (playerMove.equals(RPS.ROCK) && computerMove.equals(RPS.SCISSORS))
System.out.println("Player Wins");
else
System.out.println("There was an error, please try again");
break;
case PAPER:
// if user input is Paper
if (playerMove.equals(computerMove))
System.out.println("Game is Tie !!");
else if (playerMove.equals(RPS.PAPER) && computerMove.equals(RPS.SCISSORS))
System.out.println("Computer Wins");
else if (playerMove.equals(RPS.PAPER) && computerMove.equals(RPS.ROCK))
System.out.println("Player Wins");
else
System.out.println("There was an error, please try again");
break;
case SCISSORS:
// if user input is scissors
if (playerMove.equals(computerMove))
System.out.println("Game is Tie !!");
else if (playerMove.equals(RPS.SCISSORS) && computerMove.equals(RPS.ROCK))
System.out.println("Computer Wins");
else if (playerMove.equals(RPS.SCISSORS) && computerMove.equals(RPS.PAPER))
System.out.println("Player Wins");
else
System.out.println("There was an error, please try again");
break;
}
}
//Computer mobve is randomly generated through random function
public static String getComputerMove()
{
String computermove;
Random random = new Random();
int input = random.nextInt(3)+1;
if (input == 1)
computermove = RPS.ROCK;
else if(input == 2)
computermove = RPS.PAPER;
else
computermove = RPS.SCISSORS;
System.out.println("Computer move is: " + computermove);
System.out.println();
return computermove;
}
}
RPS.main(null);
boolean test1 = true;
boolean test2 = true;
if (!(test1 && test2)){
System.out.println("Test1 and Test2 are both false");
}
else{
System.out.println("They are both true");
}
boolean test1 = true;
boolean test2 = true;
if (!test1 || !test2){
System.out.println("Test1 and Test2 are both false");
}
else{
System.out.println("They are both true once again");
}