zoukankan      html  css  js  c++  java
  • 简单四则运算一

    import java.util.Random;
    import java.util.Scanner;

    public class Num {

    public static void main(String[] args) {
    	System.out.println("练习题数:");
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	System.out.println("---------------------");
    	
    	int c =0;
    	int result = 0;//计算结果
    	int count = 0;//正确题数
    
    	for (int j = 0; j <n; j++) {
    		Random random = new Random();//运算数
    		int a = random.nextInt(100);
    		int b = random.nextInt(100);
    		
    		char[] ch = {'+','-','*','/'};
    		Random ran = new Random();//运算符
    		int i = ran.nextInt(ch.length);
    		
    		switch(i){
    		case 0:
    			System.out.println(a+"+"+b+"= ?");
    			result=a+b;
    			c++;
    			break;
    		case 1:
    			System.out.println(a+"-"+b+"= ?");
    			result=a-b;
    			c++;
    			break;
    		case 2:
    			System.out.println(a+"*"+b+"= ?");
    			result=a*b;
    			c++;
    			break;
    		case 3:
    			System.out.println(a+"/"+b+"= ?");
    			//除法运算结果取整
    			float x = a;
    			float y = b;
    			if(y!=0){
    				result =(int)(x/y);    
    			    }
    			else{
    				System.out.println("分母有误,跳过上题 ");
    				break;
    			}
    			c++;
    			break;
    		}
    
    	System.out.println("请输入答案: ");
    	float an = sc.nextFloat();//输入计算结果
    	
    	if(an==result){
        	  System.out.println("你真棒o( ̄▽ ̄)d -------");
        	  count++;
            }else{
                System.out.println("回答错误"+"--正确结果为: "+result+"---继续努力ヾ(◍°∇°◍)ノ゙------");
            }
    	}
    
    	System.out.println("输出的题数共:"+c+"------共答对"+count+"道题");
    }
    

    }

  • 相关阅读:
    HDU 5528 Count a * b 欧拉函数
    HDU 5534 Partial Tree 完全背包
    HDU 5536 Chip Factory Trie
    HDU 5510 Bazinga KMP
    HDU 4821 String 字符串哈希
    HDU 4814 Golden Radio Base 模拟
    LA 6538 Dinner Coming Soon DP
    HDU 4781 Assignment For Princess 构造
    LA 7056 Colorful Toy Polya定理
    LA 6540 Fibonacci Tree
  • 原文地址:https://www.cnblogs.com/tingjuanli/p/8545317.html
Copyright © 2011-2022 走看看