output:1~10
import java.util.Random; public class Random_test { public static void main(String[] args){ Random r=new Random(); int num; num=r.nextInt(10); System.out.println(num+1); } }
2.gambling numbers
package Gambling_numbers; import java.util.Random; import java.util.Scanner; public class 赌数字 { public static void main(String [] args){ Random r=new Random(); int num1=r.nextInt(15); System.out.println("Start gambling!"); Scanner sc=new Scanner(System.in); int num2=sc.nextInt(); if(num1<5) { System.out.println("smaller than 5"); }else if(num1>=5&&num1<10){ System.out.println("smaller than 10 and bigger than 5"); }else{ System.out.println("else"); } while(num1!=num2){ System.out.println("false"); System.out.println("next"); int num3=sc.nextInt(); num2=num3; } if(num1==num2){ System.out.println("bingo"); } } }
3.example