答案家

 找回密码
 立即注册
查看: 910|回复: 0

重复加法怎么用java实现?答案如下

[复制链接]

1万

主题

1万

帖子

80万

积分

校长

Rank: 9Rank: 9Rank: 9

积分
809492
发表于 2016-12-24 15:56:52 | 显示全部楼层 |阅读模式
package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         final int NUMBER_OF_ADDITION =10;//产生10道题
         int corretcount=0;
         int count=0;
         String output="";
         long startTime=System.currentTimeMillis();
         Scanner input=new Scanner(System.in);
         while(count<NUMBER_OF_ADDITION){
             int number1=(int)(Math.random()*16+1);
             int number2=(int)(Math.random()*16+1);
             System.out.print("what is "+number1+"+"+number2+"?");
             int answer=input.nextInt();
         if(number1+number2==answer){
             System.out.println("you are correct!");
             corretcount++;
         }else
             System.out.println("you answer is wrong.\n"+number1+"+"+number2+"should be"+(number1+number2));
            count++;
            output+="\n"+number1+"+"+number2+"="+answer+((number1+number2==answer)?"correct":"wrong");  
         }
        long endTime=System.currentTimeMillis();
        long testTime=endTime-startTime;
        System.out.println("Correct count is"+corretcount+"\nTest time is "+testTime/1000+" seconds\n"+output);
    }
}

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

CopyRight(c)2016 www.daanjia.com All Rights Reserved. 本站部份资源由网友发布上传提供,如果侵犯了您的版权,请来信告知,我们将在5个工作日内处理。
快速回复 返回顶部 返回列表