import java.util.Scanner;
成都创新互联是一家集网站建设,柘荣企业网站建设,柘荣品牌网站建设,网站定制,柘荣网站建设报价,网络营销,网络优化,柘荣网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
public class LuckyNumber {
/**
* 幸运抽奖
*/
public static
void main(String[] args) {
String answer
= "y"; // 标识是否继续
String
userName = ""; // 用户名
String
password = ""; // 密码
int cardNumber
= 0; // 卡号
boolean
isRegister = false; // 标识是否注册
boolean
isLogin = false; // 标识是否登录
int max =
9999;
int min =
1000;
Scanner input
= new Scanner(System.in);
do {
System.out.println("*****欢迎进入奖客富翁系统*****");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.抽奖");
System.out.println("***************************");
System.out.print("请选择菜单:");
int choice =
input.nextInt();
switch
(choice) {
case 1:
System.out.println("[奖客富翁系统
注册]");
System.out.println("请填写个人注册信息:");
System.out.print("用户名:");
userName =
input.next();
System.out.print("密码:");
password =
input.next();
//
获取4位随机数作为卡号
cardNumber =
(int)(Math.random()*(max-min))+min;
System.out.println("\n注册成功,请记好您的会员卡号");
System.out.println("用户名\t密码\t会员卡号");
System.out.println(userName
+ "\t" + password + "\t" + cardNumber);
isRegister =
true; // 注册成功,标志位设置为true
break;
case 2:
System.out.println("[奖客富翁系统
登录]");
if
(isRegister) { // 判断是否注册
//
3次输入机会
for (int i
= 1; i = 3; i++) {
System.out.print("请输入用户名:");
String
inputName = input.next();
System.out.print("请输入密码:");
String
inputPassword = input.next();
if
(userName.equals(inputName) password.equals(inputPassword)) {
System.out.println("\n欢迎您:"
+ userName);
isLogin =
true; // 登录成功,标志位设置为true
break;
} else if
(i 3) {
System.out.println("用户名或密码错误,还有"
+ (3 - i) + "次机会!");
} else
{
System.out.println("您3次均输入错误!");
}
}
} else
{
System.out.println("请先注册,再登录!");
}
break;
case 3:
System.out.println("[奖客富翁系统
抽奖]");
if
(!isLogin) { // 判断是否登录
System.out.println("请先登录,再抽奖!");
} else
{
//生成5个4位随机数字,并保存在数组中
int[]
luckynums = new int[5];
for(int i
= 0; i luckynums.length; i++){
luckynums[i] =
(int)(Math.random()*(max-min))+min;
}
System.out.print("请输入您的卡号:");
int
yourcard = input.nextInt();
int
i;
System.out.print("\n本日的幸运数字为:");
for (i = 0;
i luckynums.length; i++) {
System.out.print(luckynums[i]
+ " ");\
}
for (i = 0;
i luckynums.length; i++) {
if
(luckynums[i] == yourcard) {
System.out.println("\n恭喜!您是本日的幸运会员!");
break;
}
}
if (i ==
luckynums.length) {
System.out.println("\n抱歉!您不是本日的幸运会员!");
}
}
break;
default:
System.out.println("[您的输入有误!]");
break;
}
System.out.print("继续吗?(y/n):");
answer =
input.next();
System.out.println("");
} while
("y".equals(answer));
if
("n".equals(answer)) {
System.out.println("系统退出,谢谢使用!");
}
}
}
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
public class MainFrame extends JFrame implements ActionListener{
InsertPanel ip = null;
SelectPanel sp = null;
JPanel pframe;
JButton jb1,jb2,jb3;
JMenuItem jm11,jm21,jm22,jm23,jm31,jm32,jm41,jm42;
CardLayout clayout;
public MainFrame(String s){
super(s);
JMenuBar mb = new JMenuBar();
this.setJMenuBar(mb);
JMenu m1 = new JMenu("系统");
JMenu m2 = new JMenu("基本信息");
JMenu m3 = new JMenu("成绩");
JMenu m4 = new JMenu("奖惩");
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
jm11 = new JMenuItem("退出系统");
jm21 = new JMenuItem("输入");
jm22 = new JMenuItem("查询");
jm23 = new JMenuItem("更改");
jm31 = new JMenuItem("输入成绩");
jm32 = new JMenuItem("查询成绩");
jm41 = new JMenuItem("奖励");
jm42 = new JMenuItem("处分");
m1.add(jm11);
m2.add(jm21);
m2.add(jm22);
m2.add(jm23);
m3.add(jm31);
m3.add(jm32);
m4.add(jm41);
m4.add(jm42);
Icon i1 = new ImageIcon();
Icon i2 = new ImageIcon();
Icon i3 = new ImageIcon();
jb1 = new JButton(i1);
jb1.setToolTipText("输入");
jb2 = new JButton(i2);
jb2.setToolTipText("查询");
jb3 = new JButton(i3);
jb3.setToolTipText("退出");
JToolBar tb = new JToolBar("系统工具");
tb.add(jb1);
tb.add(jb2);
tb.add(jb3);
add(tb,BorderLayout.NORTH);
jm11.addActionListener(this);
jm21.addActionListener(this);
jm22.addActionListener(this);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
clayout = new CardLayout();
pframe = new JPanel(clayout);
add(pframe);
JPanel mainp = new JPanel(new BorderLayout());
JLabel mainl = new JLabel("学生信息管理平台",SwingConstants.CENTER);
mainl.setFont(new Font("serif",Font.BOLD,30));
mainp.add(mainl);
pframe.add(mainp,"main");
clayout.show(pframe, "main");
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jm21 || e.getSource() == jb1){
if(ip == null){
ip= new InsertPanel();
pframe.add(ip,"insert");
}
clayout.show(pframe, "insert");
this.setTitle("输入学生信息");
}
else if(e.getSource() == jm22 || e.getSource() == jb2){
if(sp == null){
sp= new SelectPanel();
pframe.add(sp,"select");
}
clayout.show(pframe, "select");
this.setTitle("查询学生信息");
}
else if(e.getSource() == jm11 || e.getSource() == jb3){
System.exit(0);
}
}
}
第二个:
import javax.swing.JFrame;
public class MainTest {
public static void main(String [] args){
MainFrame f = new MainFrame("学生信息管理平台");
f.setSize(400,300);
f.setLocation(350,250);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
第二个:
import java.sql.Connection;
import java.sql.DriverManager;
public class MySQLConnection {
static Connection getCon(){
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","123");
}
catch(Exception e){
System.out.println("建立数据库连接遇到异常!");
}
return con;
}
}
第四个:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class SelectPanel extends JPanel implements ActionListener{
JButton jb;
JTextField jt;
JTextField jt1,jt2,jt3,jt4;
public SelectPanel(){
JLabel jl = new JLabel("请输入学号:",SwingConstants.CENTER);
jt = new JTextField();
jb = new JButton("确定");
JPanel jp1 = new JPanel(new GridLayout(1,3));
jp1.add(jl);
jp1.add(jt);
jp1.add(jb);
JLabel j1,j2,j3,j4;
j1 = new JLabel("学号:",SwingConstants.CENTER);
j2 = new JLabel("姓名:",SwingConstants.CENTER);
j3 = new JLabel("性别:",SwingConstants.CENTER);
j4 = new JLabel("年龄:",SwingConstants.CENTER);
jt1 = new JTextField(6);
jt1.setEditable(false);
jt2 = new JTextField(6);
jt2.setEditable(false);
jt3 = new JTextField(6);
jt3.setEditable(false);
jt4 = new JTextField(6);
jt4.setEditable(false);
JPanel jp2 = new JPanel(new BorderLayout());
JPanel jp3 = new JPanel(new GridLayout(4,2));
jp2.add(new JLabel(""),BorderLayout.NORTH);
jp3.add(j1);
jp3.add(jt1);
jp3.add(j2);
jp3.add(jt2);
jp3.add(j3);
jp3.add(jt3);
jp3.add(j4);
jp3.add(jt4);
jp2.add(jp3);
this.setLayout(new BorderLayout());
add(jp1,BorderLayout.NORTH);
add(jp2);
jb.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jb){
String stuNo = jt.getText().trim();
Student s = new Student();
boolean b = true;
try{
b = s.selectByStuNo(stuNo);
}
catch(Exception ex){
System.out.println("查询学生信息遇到异常!");
}
if(b){
jt1.setText(s.getStuNo());
jt2.setText(s.getName());
jt3.setText(s.getGender());
int a = s.getAge();
Integer i = new Integer(a);
jt4.setText(i.toString());
}
else{
JOptionPane.showMessageDialog(null, "无此学生!");
}
}
}
}
第五个:
import javax.swing.JFrame;
public class SelectTest {
public static void main(String [] args){
JFrame f = new JFrame("查询学生信息");
SelectPanel p = new SelectPanel();
f.add(p);
f.setSize(400,300);
f.setLocation(300,250);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
第六个:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class Student {
String stuNo;
String name;
String gender;
int age;
public Student(){}
public Student(String stuNo,String name,String gender, int age){
this.stuNo = stuNo;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getStuNo(){
return stuNo;
}
public void setStuNo(String stuNo){
this.stuNo = stuNo;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getGender(){
return gender;
}
public void setGender(String gender){
this.gender = gender;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public boolean insertStudent(){
boolean b = true;
try{
Connection con = MySQLConnection.getCon();
Statement statement = con.createStatement();
String sql = "insert into student values('" + stuNo + "','" + name +"','" + gender + "'," + age + ")";
sql = new String(sql.getBytes("gb2312"),"ISO8859_1");
statement.executeUpdate(sql);
con.close();
}
catch(Exception e){
b = false;
System.out.println("插入数据库遇到异常!");
}
return b;
}
public boolean selectByStuNo(String stuNo)throws Exception{
boolean b = true;
Connection con = MySQLConnection.getCon();
Statement statement = con.createStatement();
String sql = "select * from student where stuNo =" + stuNo;
ResultSet rs = statement.executeQuery(sql);
if(rs != null rs.next()){
String no = rs.getString(1);
this.setStuNo(no);
String n = rs.getString(2);
n = new String(n.getBytes("ISO8859_1"),"gb2312");
this.setName(n);
String g = rs.getString(3);
g = new String (g.getBytes("ISO8859_1"),"gb2312");
this.setGender(g);
this.setAge(rs.getInt(4));
b = true;
}
rs.close();
statement.close();
con.close();
return b;
}
}
数据库你自己弄吧,我没时间弄了!初学得多动手哦
抽取问题, 重点是 同一个学号不能重复被抽取.
解决办法很多,
比如数组可以使用下标来标记,号码是否被使用,使用了就继续下一次抽取
也可以使用集合来抽取,把集合顺序打乱,然后随便抽几个就可以了
参考代码:数组法
import java.util.Random;
public class Test {
public static void main(String[] args) {
int stuNums=30;
int[] nums=new int[stuNums];//存储学号的数组
boolean[] flags=new boolean[stuNums];//标记,用于标记对应下标的学号是否已经被抽取过了
for (int i = 0; i stuNums; i++) {
nums[i]=i+1;//给学号赋值
}
Random r=new Random();
while(true){
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("A等:"+nums[index]);
flags[index]=true; //标记已经被使用过了
break;
}
}
for (int i = 0; i 2; i++) {
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("B等:"+nums[index]);
flags[index]=true;
}else{
i--;//如果已经被抽取过了 ,那么i建议,再次循环
}
}
for (int i = 0; i 3; i++) {
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("c等:"+nums[index]);
flags[index]=true;
}else{
i--;
}
}
}
}
集合法
import java.util.ArrayList;
import java.util.Collections;
public class Test2 {
public static void main(String[] args) {
int stuNums=20;
ArrayListInteger list=new ArrayListInteger();
for (int i = 0; i stuNums; i++) {
list.add(i+1);
}
System.out.println("有序"+list);
Collections.shuffle(list);//打乱顺序
System.out.println("乱序"+list);
System.out.println("A等"+list.get(0));
System.out.println("B等"+list.get(1));
System.out.println("B等"+list.get(2));
System.out.println("C等"+list.get(3));
System.out.println("C等"+list.get(4));
System.out.println("C等"+list.get(5));
}
}
public class Student {
private int kemu1, kemu2, kemu3, kemu4, kemu5;
public int getKemu1() {
return kemu1;
}
public void setKemu1(int kemu1) {
this.kemu1 = kemu1;
}
public int getKemu2() {
return kemu2;
}
public void setKemu2(int kemu2) {
this.kemu2 = kemu2;
}
public int getKemu3() {
return kemu3;
}
public void setKemu3(int kemu3) {
this.kemu3 = kemu3;
}
public int getKemu4() {
return kemu4;
}
public void setKemu4(int kemu4) {
this.kemu4 = kemu4;
}
public int getKemu5() {
return kemu5;
}
public void setKemu5(int kemu5) {
this.kemu5 = kemu5;
}
}
上边的是Student类
import java.util.ArrayList;
import java.util.Random;
public class Test {
public static void main(String[] args) {
ArrayListStudent list = new ArrayListStudent();
// 初始化成绩
for (int n = 0; n 20; n++) {
Student st = new Student();
st.setKemu1(new Random().nextInt(100));
st.setKemu2(new Random().nextInt(100));
st.setKemu3(new Random().nextInt(100));
st.setKemu4(new Random().nextInt(100));
st.setKemu5(new Random().nextInt(100));
list.add(st);
}
// 计算平均成绩
int avg = 0;
for (int n = 0; n 20; n++) {
avg += list.get(n).getKemu1();
avg += list.get(n).getKemu2();
avg += list.get(n).getKemu3();
avg += list.get(n).getKemu4();
avg += list.get(n).getKemu5();
}
double avg2 = avg / 20;
System.out.println("全班平均成绩" + avg2);
// 计算谁得奖
for (int n = 0; n 20; n++) {
double avg3;
int avg4 = 0;
avg4 += list.get(n).getKemu1();
avg4 += list.get(n).getKemu2();
avg4 += list.get(n).getKemu3();
avg4 += list.get(n).getKemu4();
avg4 += list.get(n).getKemu5();
avg3 = avg4;
System.out.println((n + 1) + "成绩为" + avg3);
if (avg3 (avg2 * 1.2)) {
System.out.println((n + 1) + "该生获得1等将");
}
if (avg3 (avg2 * 1.1)) {
System.out.println((n + 1) + "该生获得2等将");
}
}
}
}
好了 可以了
ArrayList是动态数组