错与对并不是绝对的

四则运算和验证码

package arithmetic;
/**
 * 30道100以内四则运算
 * 
 * */
public class arithmetic {

    public static int getRandom( int n, int m)
         {
        //产生m->n的随机数
                return (int) (Math.random() * (m - n) + n);
         }
    
    public static char getCharRandom()
    {
        //随机产生四种运算符
        char sign = 0;
        int Oc;
        Oc = getRandom(1,5);
    
        if(Oc==1) sign='+';
        else if(Oc==2) sign='-';
        else if(Oc==3) sign='×';
        else if(Oc==4) sign='÷';
        return sign;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

         
         for(int i = 0; i <30;i++)
         {
             int  x = (int)(Math.random()*100)+1;  //产生1-100的随机数
             int  y = (int)(Math.random()*100)+1;  //产生1-100的随机数
             char sign = getCharRandom();
             if(sign == '×')
             {
                 x = (int) (Math.random() * (10 - 1 )+ 1);//新生成x,y<9的随机数
                 y = (int) (Math.random() * (10 - 1 )+ 1);
                 
                     System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );    
                    
             }
            /* 加减法判断*/     
            if(sign == '+' || sign == '-')
             {
                 if(x < y)                        //判断减数与被减数的大小关系
                 {
                     int temp;
                     temp = x;
                     x = y;
                     y = temp;
                 }
                 System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                 
             }
             
             /*
              * 除法判断*/
            if(sign == '÷')
             {
                do                                             //循环生成除法
                 {
                     y = (int) (Math.random() * (10 - 1 )+ 1);
                     x = (int) (Math.random() * (9*y - 1 )+ 1);
                                            
                 }
                while(x % y != 0) ;
                System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                
             }
         }
    }

}
package yanzhengma;

import java.util.*;
import java.awt.*;
import javax.swing.*;


public class yanzhengma {

    public static char ran()
    {
        char []s= {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        char s1;
        Random rd = new Random();
        s1=s[rd.nextInt(17)];
        return s1;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

         boolean flag=true;
            Scanner scan=new Scanner(System.in);
            int i;
    char []a=new char[4];
    char []b=new char[4];
    System.out.println("验证码如下:");
    for(i=0;i<4;i++)
    {
        a[i]=ran();
        System.out.print(a[i]);
    }
    System.out.println("\n");
    System.out.println("请输入验证码 : ");
    for(i=0;i<4;i++)
    {
        String s=scan.next();
        b[i]=s.charAt(0);
        if(b[i]!=a[i])
            flag=false;
    }
    if(flag)
    {
        System.out.println("验证码正确");
    }
    else
        System.out.println("验证码错误");

    }

}

 

posted on 2018-10-08 09:37  错与对并不是绝对的  阅读(278)  评论(0编辑  收藏  举报

导航