十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
很多种方式可以实现,例如①给所有名字加上一个布尔类型的flag表示是否被抽过,每抽选一个名字将其对应的flag改为true,如果抽中flag为true的人则重抽;②将所有名字放在数组a中,设数组长度为n,第一次抽选从下标0~n-1中任选一个,将该下标位置的元素与a[0]交换;第二次抽选从下标1~n-1中任选一个,将该下标位置的元素与a[1]交换,以此类推。
创新互联公司2013年成立,是专业互联网技术服务公司,拥有项目成都做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元长葛做网站,已为上家服务,为长葛各地企业和个人服务,联系电话:18980820575
public class test {\x0d\x0a public static void main(String[] args) {\x0d\x0a //定义人名数组\x0d\x0a String [] name = {"张三","李四","王五","八神庵","不知火舞","大蛇","景天","唐雪见","李逍遥","赵灵儿"};\x0d\x0a//随机生成数组下标、\x0d\x0a int num = (int)(Math.random() * 1000);\x0d\x0a//对生成的随机数进行判断,如果小于数组下标,就跳出循环\x0d\x0awhile (numname.length-1) {\x0d\x0a if (num
回答于 2022-11-16
源程序如下:
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class LoginValidation extends JFrame implements ActionListener{
private Panel pan = new Panel();
private Button login = new Button("login");
private TextField username = new TextField(10);
private JLabel opl1 = new JLabel("please login with username");
public LoginValidation() {
super("Login");
initialize();
}
public void initialize() {
this.setLayout(null);
this.setSize(360, 200);
this.setResizable(false);
this.add(pan);
pan.setBounds(10, 10, 330, 250);
pan.setLayout(new GridLayout(5, 1));
pan.add(opl1);
pan.add(username);
login.addActionListener(this);
pan.add(login);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==login){
String str = username.getText().trim();
if(str.equals("")|| str.equals(null))
{
JOptionPane.showMessageDialog(null,"username can be empty!!","username",JOptionPane.ERROR_MESSAGE);
return;
}
Pattern pt = Pattern.compile("^\\d{10}$");
Matcher m = pt.matcher(str);
if(m.matches()){
char c = str.charAt(0);
if(String.valueOf(c).equals("0")){
JOptionPane.showMessageDialog(null,"the first number can't be 0!!!","username",JOptionPane.ERROR_MESSAGE);
username.setText("");
repaint();
return;
}
else{
JOptionPane.showMessageDialog(null,"login success!!!","username",JOptionPane.INFORMATION_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null,"username must be 10 numbers!!","username",JOptionPane.ERROR_MESSAGE);
username.setText("");
repaint();
}
}
}
public static void main(String[] args) {
new LoginValidation();
}
}