package com.itranswarp.learnjava;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("please choice:");
System.out.println(" 1: Rock");
System.out.println(" 2: Scissors");
System.out.println(" 3: Paper");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
Random rd = new Random();
int random = rd.nextInt(1, 4);
System.out.printf("choice:%d\t", choice);
System.out.println("random:" + random);
String end = null;
end = switch (choice) {
case 1 -> random == 1 ? "draw" : (random == 2 ? "win" : "lose");
case 2 -> random == 1 ? "lose" : (random == 2 ? "draw" : "win");
case 3 -> random == 1 ? "win" : (random == 2 ? "lose" : "draw");
default -> "exception";
};
System.out.println(end);
}
Sign in to make a reply
Az
package com.itranswarp.learnjava;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("please choice:");
System.out.println(" 1: Rock");
System.out.println(" 2: Scissors");
System.out.println(" 3: Paper");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
Random rd = new Random();
int random = rd.nextInt(1, 4);
System.out.printf("choice:%d\t", choice);
System.out.println("random:" + random);
String end = null;
end = switch (choice) {
case 1 -> random == 1 ? "draw" : (random == 2 ? "win" : "lose");
case 2 -> random == 1 ? "lose" : (random == 2 ? "draw" : "win");
case 3 -> random == 1 ? "win" : (random == 2 ? "lose" : "draw");
default -> "exception";
};
System.out.println(end);
}
}