重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public class test {
创新互联建站是一家专业提供湘桥企业网站建设,专注与成都做网站、网站制作、成都外贸网站建设、H5网站设计、小程序制作等业务。10年已为湘桥众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
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]);
}
}
//首先我们可以先把名字搞成一个数组,或者List都是可以的。
String[] arr = {"张三","李四","王五","xxx"....};
String random = arr[new Random().nextInt(arr.size())];
System.out.println("随机名字为:"+random);
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class test {
static String[] arr;
static boolean flag=true;
static ListString a;
static JTextField jtf;
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
frame.setLayout(new GridLayout(1,2));
JButton button = new JButton("抽奖");
jtf = new JTextField();
frame.add(jtf);
frame.add(button);
a = new ArrayListString();
arr = new String[]{"张三","李四","王五"};
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int i=0;
fond:
while(true){
String str = arr[(int)(Math.random()*arr.length)];
for(String b:a){
if(b.equals(str)){
if(i=arr.length){
jtf.setText("没有人了");
break fond;
}
continue fond;
}
i++;
}
a.add(str);
jtf.setText(str);
break;
}
}
});
frame.setSize(500, 500);
frame.setLocation(500, 500);
frame.setVisible(true);
}
}
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] + " "); } }}
import java.util.Random;
public class RandomD {
public static void main(String[] args) {
System.out.println("随机点名开始!我将要在5个同学中点名!");
String[] student={"张三","李四","王五","麻子","小猪"};
Random rand=new Random();
System.out.println("点名开始:");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(student[Math.abs(rand.nextInt()%5)]);
}
}