码迷,mamicode.com
首页 > 其他好文 > 详细

随机生成 10道四则运算题目 累计得分 输出文件

时间:2018-10-26 12:06:07      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:text   out   成绩   rand   ted   技术   and   case   技术分享   

00

技术分享图片
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Demo {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        Counter counter=new Counter();
        PrintWriter out = null;
        try {
            out = new PrintWriter("text.txt");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int sum = 0;

        
        
        for (int i = 1; i <=10; i++) {
            int a = (int) Math.round(Math.random() * 100);
            int b = (int) Math.round(Math.random() * 100);
            int m= (int) Math.round(Math.random() * 3);

            
           switch(m)
           {
           case 0:
               System.out.println(i+": "+a+"/"+b+"=");
               
               while(b==0){  b = (int) Math.round(Math.random() * 100); }
               
            int c0 = in.nextInt();
            out.println(a+"/"+b+"="+c0);
            if (c0 == counter.division(a, b)) {
                sum += 10;
                System.out.println("恭喜答案正确");
            }
            else {
                System.out.println("抱歉,答案错误");
            }
            
            break;
            
           case 1:
               System.out.println(i+": "+a+"*"+b+"=");
               int c = in.nextInt();
               out.println(a+"*"+b+"="+c);
               if (c == counter.multiplication(a, b)) {
                   sum += 10;
                   System.out.println("恭喜答案正确");
               }
               else {
                   System.out.println("抱歉,答案错误");
               }
               break;
           case 2:
               System.out.println(i+": "+a+"+"+b+"=");
               int c1 = in.nextInt();
               out.println(a+"+"+b+"="+c1);
               if (c1 == counter.add(a, b)) {
                   sum += 10;
                   System.out.println("恭喜答案正确");
               }
               else {
                   System.out.println("抱歉,答案错误");
               }
               
               break ;
           case 3:
               System.out.println(i+": "+a+"-"+b+"=");
               int c2 = in.nextInt();
               out.println(a+"-"+b+"="+c2);
               if (c2 == counter.reduce(a, b)) {
                   sum += 10;
                   System.out.println("恭喜答案正确");
               }
               else {
                   System.out.println("抱歉,答案错误");
               }
               break ;

               } 
    
          }
        System.out.println("成绩"+sum);
        out.println("成绩:"+sum);
         out.close();

         
    }
    }
main
技术分享图片
public class Counter {
   private int a;
   private int b;
    public int  add(int a,int b)
    {
        return a+b;
    }
    public int   reduce(int a,int b)
    {
        return a-b;
    }
    public int   multiplication(int a,int b)
    {
        return a*b;
    }
    public int   division(int a,int b)
    {
        if(b!=0)
        return a/b;
        else return 0;
    }

    
}
Counter

技术分享图片

技术分享图片

随机生成 10道四则运算题目 累计得分 输出文件

标签:text   out   成绩   rand   ted   技术   and   case   技术分享   

原文地址:https://www.cnblogs.com/2017xinghui/p/9851358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!