使用Random类随机生成1-100之间的整数,再通过if语句进行判断,不同的区间的数字输出不同的结果,完整代码如下:
package Whaoe;
import java.util.Random;
/**
* <p>Project: Work1 - Ceshi</p>
* <p>Powered by whaoe On 2023-07-10 20:45:30</p>
* <p>描述:<p>
*
* @author whaoe [webrx@126.com]
* @version 1.0
* @since 17
*/
public class Ceshi {
public static void main(String[] args) {
Random rand = new Random();
int a =rand.nextInt(1, 101);
if (a<=30){
System.out.println(a+"恭喜您,中奖了,奖金20元");
}else if(a>30&&a<60){
System.out.println(a+"恭喜您,中奖了,奖金50元");
}else if(a>=60){
System.out.println(a+"很遗憾,您没有中奖");
}
}
}
这里需要注意rand.nextInt(1, 101)中的值,在这里括号中可是不能取到后面的值,因此我们要想在1-100(包含100)之间随机生成整数数字,我们需要设置上限+1