Class__One HomeWork 实验报告

石家庄铁道大学信息科学与技术学院

 

 

 

实验报告

2018----2019  学期

 

 

 

 

 

 

 

题目   四则运算和验证码        

课程名称:  JAVA语言程序设计   

   级:        1705-2                

   名:    黄珺瑜    学号:   20173557      

指导教师:       王建民                  

1,四则运算

import java.util.Random;

public class PupilWork {

public static void main(String[] args) {

// TODO 自动生成的方法存根

        Random m =new Random(100);

        int n,s,j;

        String nn[]= {"+","-","*","/"};

        

        for(int i=0;i<30;i++) {

         n=m.nextInt(19)+1;

         s=m.nextInt(4);

         j=m.nextInt(19)+1;

         if(nn[s]=="-"&&n<j) {

         i--;

         }

         else {

         if(nn[s]=="/")

          System.out.print((n*s)+nn[s]+n+"= ");

         else

          System.out.print(n+nn[s]+j+"= ");

        

              if((i+1)%5==0)

              System.out.println(" ");}

                }

        

}

}

              if((i+1)%5==0)

              System.out.println(" ");}

                }

        

}

}

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

小学二年级的数据范围大约是在二十以内的数字 ,除法也是可以必须得除尽的。

2,验证码

 添加标签,文本框,按钮  运用JLabel等类型定义,验证码框用Graphics类操作定义

运用到setBounds定义每个组件的大小和位置

public class EntryBoundary extends JFrame{

    private Identify mp;

    private JButton entry,enrollfast;

    private JLabel Id,password,vercode;

    private JTextField ID,vertext;

    private JPasswordField passwordtext;

    

    

public EntryBoundary() {

setTitle("请登录");

setBounds(200, 200, 400, 250);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setBackground(Color.WHITE);

Container c = getContentPane();

c.setLayout(null);

//定义登录标签和登录文本框

    Id = new JLabel("登录名:");

    Id.setFont(new Font("楷书", Font.BOLD, 15));

    Id.setSize(80, 50);

    Id.setLocation(30, 10);

    c.add(Id);

    ID = new JTextField();

    ID.setFont(new Font("宋体", Font.ITALIC, 15));

    ID.setText("用户名/邮箱");

//     ID.setFont(new Font("Arial", Font.BOLD, 20));

    ID.setSize(200, 30);

    ID.setLocation(90, 20);

    c.add(ID);

    

    

    //定义密码标签和密码文本框

    password = new JLabel("密码:");

    password.setSize(70, 50);

    password.setLocation(45, 45);

    c.add(password);

    password.setFont(new Font("楷书", Font.BOLD, 15));

    passwordtext = new JPasswordField();

    passwordtext.setFont(new Font("Arial", Font.BOLD, 20));

    passwordtext.setSize(200, 30);

    passwordtext.setLocation(90, 55);

    c.add(passwordtext);

    

    

    //声明并定义提示标签

    JLabel jt = new JLabel("忘记用户名/密码?");

    jt.setFont(new Font("草书", Font.PLAIN, 11));

    jt.setSize(100, 30);

    jt.setLocation(295, 55);

    c.add(jt);

    

    

    //定义验证码标签,验证码框以及验证码图

    vercode = new JLabel("验证码:");

    vercode.setFont(new Font("楷书", Font.BOLD, 15));

    vercode.setSize(80, 50);

    vercode.setLocation(30, 80);

    c.add(vercode);

    vertext = new JTextField();

    vertext.setSize(150, 30);

    vertext.setLocation(90, 90);

    c.add(vertext);

    

    

    //定义验证码面板

    mp = new Identify();

    mp.setSize(90, 30);

    mp.setLocation(245, 90);

    c.add(mp);

    

//     //刷新键

//     JLabel re = new JLabel("");

//     Icon icon = new ImageIcon("src/com.mr.email/Java.jpg");

//     re.setIcon(icon);

//     re.setBounds(340, 65, 30, 40);

//     c.add(re);

    

    

//定义登录和快速登录按钮

    entry = new JButton("登录");

    entry.setBackground(Color.ORANGE);

    enrollfast = new JButton("快速注册");

    entry.setBounds(90, 130, 200, 30);

    c.add(entry);

    enrollfast.setBounds(90, 170, 200, 30);

    c.add(enrollfast);

    

setResizable(false);

setVisible(true);

}

public static void main(String[] args) {

new EntryBoundary();

}

}

import java.awt.*;

import java.util.*;

public class Identify extends Panel{

private StringBuilder code;

public StringBuilder getCode() {

return code;

}

public void paint(Graphics g) {

//设置验证框填充色并设置其大小

g.fillRect(0, 0, 100, 150);

g.setColor(Color.BLUE);

g.drawRect(0, 0, 100, 150);

Random r = new Random();

//设置干扰点

int x,y;

for(int i=0;i<80;i++) {

      x = r.nextInt(100);

      y = r.nextInt(90);

      g.drawOval(x, y, 2, 2);

}

g.setFont(new Font("楷体", Font.BOLD , 20));//设置验证码字体大小

g.setColor(Color.RED);//设置验证码颜色

//随机生成验证码

char[] chr= ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();

code = new StringBuilder();

char a;

for(int i=1;i<=4;i++) {

a = chr[r.nextInt(chr.length)];

code.append(a);

}

g.drawString(code.toString(), 25, 20);

}

}

原文地址:https://www.cnblogs.com/goubb/p/9753584.html