四则运算及验证码源代码(第二周)

四则运算

建立两个类,suansu跟size,size负责进行两个随机数及四则运算的选取,并添加范围限制。suansu负责调用类size进行算术显示及语言提示。

  1. 实验总结

(1)随机数函数Random()

2)valueOf()方法可以接收两个参数一个是字符串,一个是基数。

()试验过程中曾经没有出现过除法,经排查后是size类中限制条件出了问题:if(a%b!=0){      

                continue;

            }

应为:if(a%b==0){      

                continue;

            }

import java.util.Scanner;

public class suanshu {
 
    public static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
     
        int i=30;
        
        int correct=0;
        
        size abc;
        
        while(i>0){
         
        abc=new size();
        
        System.out.print(abc.getString());
        
        String s=scanner.nextLine();
        
        if(abc.panduan(s)){
        	
            System.out.println("回答正确!");
            
            correct++;
            }
        else{
            System.err.println("回答错误!");
        }
        i--;
        System.out.println("还有"+i+"道题未回答");
        }
        
        System.out.println("恭喜你!,回答完毕,回答正确的为"+correct+"道");

    } 
}

  

import java.util.Random;//random函数返回一个0~num-1之间的随机数

public class size {

    private int a, b;
    private int i;
    private String operator[]={"+","-","×","÷"};

    public size(){

      while(true){

        a=new Random().nextInt(100);
        b=new Random().nextInt(100);
        i=new Random().nextInt(4);

        if(i==1&&a<b){       
            continue;
        }

        if(i==3){             
            if(b==0){
                continue;
            }
            if(a%b==0){      
                continue;
            }
        }
        break;      
      }
    }

    public String getString(){
    	
       return new String(a+operator[i]+b+"=");
    }

    public  boolean panduan(String s){//boolean类只有两个值true和false,可以把它看做是一个“开关”来使用。
        int i,result = 0;
        try{
            i=Integer.valueOf(s).intValue();//该方法是静态方法。该方法可以接收两个参数一个是字符串,一个是基数。
        }catch(Exception e){
            return false;
        }

        switch(this.operator().toCharArray()[0]){//字符串对象中的字符转换为一个字符数组
           case '+':result=this.getA()+this.getB();break;
           case '-':result=this.getA()-this.getB();break;
           case '×':result=this.getA()*this.getB();break;
           case '÷':result=this.getA()/this.getB();break;
        }

        if(result==i){
            return true;
        }return false;
    }

    public String operator(){
        return operator[this.i];
    }
    
    public int getA() {
        return a;
    }

    public int getB() {
        return b;
    }
}

  验证码

  1. 程序设计思想

先用数组进行随机数6位抽取,再用消息提示框来进行图形化。

流程图:

总结:之前一直不知如何简单的进行图形化,后来查询到消息提示框的用法,可以进行简单的图形化验证码操作。

package yanzhenma;

import java.util.Random;
import javax.swing.JOptionPane;  // import class JOptionPane
public class Action {

    /**
     * @param args
     */
    public static void main(String[] args) {
		int[] array = {0,1,2,3,4,5,6,7,8,9};
      //随机对象
      		Random rand = new Random();
      //循环产生
      		for (int i = 10; i > 1; i--) {
      			int index = rand.nextInt(i);
      			int tmp = array[index];
      			array[index] = array[i - 1];
      			array[i - 1] = tmp;
      		}
      //拼接结果为字符串
      		int result = 0;
      		for(int i = 0; i < 6; i++)
      			result = result * 10 + array[i];
      		String sixString =  Integer.toString(result);
      //有可能出现5位数,前面加0补全
      		if (sixString.length() == 5) {
      			sixString = "0" + sixString;
      		}
      		
        String input=JOptionPane.showInputDialog(sixString+"\n"+"请输入验证码:");//显示验证码,并提示用户输入验证码
        if(input.equals(sixString))//判断验证码是否正确
        {
            JOptionPane.showMessageDialog(null,"验证码正确,验证成功!","Results",JOptionPane.PLAIN_MESSAGE );
        }
        else
        {
            JOptionPane.showMessageDialog(null,"验证码错误,验证失败!","Results",JOptionPane.PLAIN_MESSAGE );
        }
    }

}

  实验结果截图:
1.四则运算:

算术运算截图:

 

 

 

30道题目完成后截图:

 

2.验证码:

 

posted @ 2018-10-08 08:27  夜神风  阅读(345)  评论(0编辑  收藏  举报