随机数的运算

2016-09-24  本文已影响0人  myz666

package newpac;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Count {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int num1,num2,count=0,a,b;
    String aStr;
    
    final int LOW_BOUND=0;
    final int UP_BOUND=9;
    
    
    
    aStr=JOptionPane.showInputDialog("你想做几题数学题:");
    a=Integer.parseInt(aStr);
    
    
    
    for(int i=0;i<a;i++){
        num1=(int)((UP_BOUND-LOW_BOUND+1)*Math.random());
        num2=(int)((UP_BOUND-LOW_BOUND+1)*Math.random());
        b=(int)(1+Math.floor(4*Math.random()));
        switch(b){
            case 1:{
                int answer;
                Scanner input=new Scanner(System.in);
                System.out.print(num1+"+"+num2+"= ");
                answer=input.nextInt();
                if(answer==(num1+num2)){
                    JOptionPane.showMessageDialog(null,"you are right!","message",JOptionPane.INFORMATION_MESSAGE);
                    count++;
                }
                else{
                    JOptionPane.showMessageDialog(null,"you are wrong!","message",JOptionPane.INFORMATION_MESSAGE);
                }
            }
                break;
            case 2:{
                int answer;
                Scanner input=new Scanner(System.in);
                System.out.print(num1+"-"+num2+"= ");
                answer=input.nextInt();
                if(answer==(num1-num2)){
                    JOptionPane.showMessageDialog(null,"you are right!","message",JOptionPane.INFORMATION_MESSAGE);
                    count++;
                }
                else{
                    JOptionPane.showMessageDialog(null,"you are wrong!","message",JOptionPane.INFORMATION_MESSAGE);
                }
            }
                break;
            case 3:{
                int answer;
                Scanner input=new Scanner(System.in);
                System.out.print(num1+"*"+num2+"= ");
                answer=input.nextInt();
                if(answer==(num1*num2)){
                    JOptionPane.showMessageDialog(null,"you are right!","message",JOptionPane.INFORMATION_MESSAGE);
                    count++;
                }
                else{
                    JOptionPane.showMessageDialog(null,"you are wrong!","message",JOptionPane.INFORMATION_MESSAGE);
                }
            }
                break;
            case 4:{
                int answer;
                Scanner input=new Scanner(System.in);
                System.out.print(num1+"/"+num2+"= ");
                answer=input.nextInt();
                if(answer==(num1/num2)){
                    JOptionPane.showMessageDialog(null,"you are right!","message",JOptionPane.INFORMATION_MESSAGE);
                    count++;
                }
                else{
                    JOptionPane.showMessageDialog(null,"you are wrong!","message",JOptionPane.INFORMATION_MESSAGE);
                }
            }
                break;
            default:System.out.print("ERROR");                      
        }
    }
    JOptionPane.showMessageDialog(null,"you answer "+a+",and"+count+"is right!","message",JOptionPane.INFORMATION_MESSAGE);
    
}

}

上一篇下一篇

猜你喜欢

热点阅读