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

实验二--四则运算-升级版--2

时间:2015-04-09 01:00:10      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

import java.util.*;
import java.util.Random;
import java.text.DecimalFormat;
public class SiZe {
    public static void main(String[] args) {
        
        String[] str_1 = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        String[] str_2 = new String[] { "+", "-", "*", "/"};
        int index_1 = 0;
        int index_2 = 0;
        int[] a = new int[2];
        double data = 0;
        double answer = 0;
        int num = 0;
        int  num_1 = 0;
        DecimalFormat dFormat=new DecimalFormat("#.00");
        
        while(num_1 > 5 || num_1 <= 0)
        {
            System.out.print("请输入题目的数量(题目数量最多不能超过5个): ");
            Scanner sc = new Scanner(System.in);
            num_1 = sc.nextInt();
        }
        System.out.println("注意!除法计算结果保留两位,含四舍五入!!");
        long t1=System.currentTimeMillis();
        for(int k = 0; k < num_1; k++)
        {
            System.out.print("第"+(k+1)+"道题:");
            for(int i = 0; i < 2; i++)
            {
                index_1 = new Random().nextInt(9);
            
                if(str_2[index_2].equals("/") && index_1 != 0)
                {
                    while(index_1 == 0)
                    {  System.out.println("+++++++++++");
                        index_1 = new Random().nextInt(9);
                    }
                }                    
                a[i] = index_1;     
                if(i != 1)
                {
                    index_2 = new Random().nextInt(4);
                }
            }

            System.out.print(str_1[a[0]]+str_2[index_2]+str_1[a[1]]+"=");
            Scanner input = new Scanner(System.in);
            data =input.nextDouble();
            
            switch(index_2)
            {
                case 0:
                    answer = a[0] + a[1];
                    break;
                case 1:
                    answer = a[0] - a[1];
                    break;
                case 2:
                    answer = a[0] * a[1];
                    break;
                case 3:
                    answer = (double)a[0] / a[1];
                    answer = (Math.round(answer*100)/100.0);
                    break;
            }
            
            if(data == answer)
            {
                System.out.println("你输入的答案正确!");
                num++;
            }
            else
            {
                System.out.println("你输入的答案错误!正确答案是: "+answer);
           
            }
            
            System.out.println();
            if((System.currentTimeMillis()-t1)/1000 >= 120)
            {
                System.out.println("时间已到,不能答题!");
                break;
            }
        }
        System.out.println(num_1+"道题目,你一共答对"+num+"道题目!");
        System.out.println("完成"+num+"道题目所用时间为: "+ (System.currentTimeMillis()-t1)/1000 + "秒!");
    }

}
技术分享技术分享技术分享技术分享

 

实验二--四则运算-升级版--2

标签:

原文地址:http://www.cnblogs.com/OuZeBo/p/4405474.html

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