java经典随机加减法游戏

一、程序要求:

系统随机出10道2个100以内的加减计算题,用户在控制台输入答案,如果答案正确,加10分,否则,不加分.最后计算得分,显示在控制台如果当用户输入200的时候,结束程序,输入得分计算用时,显示在控制台,以秒的形式。


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

public class Calculation {

	public static void main(String[] args) {
		int result = 0; 
		int score = 0;
		Random rand = new Random();
		Scanner scan = new Scanner(System.in);
		long beginTime = System.currentTimeMillis();//获取当前系统时间
		for(int i=0;i<10;i++){
			int num1 = rand.nextInt(100);//0-99随机的数字
			int num2 = rand.nextInt(100);//0-99随机的数字
			int operator = rand.nextInt(2);//0-1 0:加法,1:减法
			if(operator==0){
				result = num1 + num2;
				System.out.println(num1+"+"+num2+"=?");
			}else{
				result = num1 - num2;
				System.out.println(num1+"-"+num2+"=?");
			}
//			System.out.println("正确结果:"+result);//作弊
			int input = scan.nextInt();//用户输入的结果
			if(input==200){
				break;
			}else if(input==result){
				score+=10;
			}
//			System.out.println("用户得分:"+score);
		}
		long endTime = System.currentTimeMillis();//结束时间
		System.out.println("用户得分:"+score
				+",用户用时:"+(endTime-beginTime)/1000.0+"秒");
	}

}

 

 

 

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值