java随机点名源码(编写一个随机点名程序JAVA)
本文目录一览:
Java求助 ,Java用Scanner以及遍历数组还有for循环写一个随机点名的代码?
import java.io.BufferedInputStream;import java.util.Scanner; public class test1 { public static void main(String[] args) { Scanner cin = new Scanner(new BufferedInputStream(System.in)); int n = cin.nextInt(); // 输入n个数 int[] a = new int[n]; for (int i = 0; i n; ++i) { a[i] = cin.nextInt(); } cin.close(); for (int i = a.length - 1; i = 0; --i) { // 逆序输出 System.out.print(a[i] + " "); } }}
Java随机抽取人名完整代码是什么?
public class test {
public static void main(String[] args) {
//定义人名数组
String [] name = {"张三","李四","王五","八神庵","不知火舞","大蛇","景天","唐雪见","李逍遥","赵灵儿"};
//随机生成数组下标、
int num = (int)(Math.random() * 1000);
//对生成的随机数进行判断,如果小于数组下标,就跳出循环
while (numname.length-1) {
if (num=name.length-1) {
break;
}
num = (int)(Math.random() * 1000);
}
//将数组下标设置成随机数,就可以实现人名的随机抽取
System.out.println(“被抽到的同学是:”+name[num]);
}
}
javaweb写个随机点名的代码
//首先我们可以先把名字搞成一个数组,或者List都是可以的。
String[] arr = {"张三","李四","王五","xxx"....};
String random = arr[new Random().nextInt(arr.size())];
System.out.println("随机名字为:"+random);