随机数的运算

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);
    
}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容