1.Random使用步骤:
package com.itheima.Random; import java.util.Random; public class Random1 { public static void main(String[] args) { Random random=new Random(); for (int i=1;i<=10;i++) { int data=random.nextInt(10);//nextInt(n)生成0-n-1的随机数 System.out.println(data); } } }
2.Random案例:
猜数字案例:
package com.itheima.Random; import java.util.Random; import java.util.Scanner; public class Random2 { public static void main(String[] args) { Random random=new Random(); int luckNumber=random.nextInt(100)+1; Scanner sc=new Scanner(System.in); while(true){ System.out.println("请输入猜测数据"); int gussNumber=sc.nextInt(); if(gussNumber>luckNumber){ System.out.println("猜测数字过大"); } else if (gussNumber<luckNumber) { System.out.println("猜测数字过小"); } else { System.out.println("猜对了"); break; } } } }
标签:Random,java,int,random,System,随机数,public From: https://www.cnblogs.com/pengsuoqun123/p/17865624.html