package js;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class js1 {
static ArrayList<Integer> al=new ArrayList<Integer>();
static Object[] counts;
public static void main(String[] args) {
int count = 0;//计算正确次数
Random r = new Random();
Scanner input = new Scanner(System.in);
for(int i=1;i<=10;i++) {
//生成两个随机数
int num1=r.nextInt(100);
int num2=r.nextInt(100);
//加法
if(num1<num2) {
System.out.println(num1+"+"+num2+"=?");
int answer = input.nextInt();
if(answer==num1+num2) {//判断是否正确
count++;
System.out.println("回答正确!**********您共答对"+count+"次");
}else {
System.out.println("回答错误!**********您共答对"+count+"次");
}
}
//减法
if(num1>num2) {
System.out.println(num1+"-"+num2+"=?");
int answer = input.nextInt();
if(answer==num1-num2) {
count++;
System.out.println("回答正确!***********您共答对"+count+"次");
}else {
System.out.println("回答错误!***********您共答对"+count+"次");
}
}
}
System.out.println(" 您的分数是:"+count);
js1.Ranking( count);
try {
BufferedReader br=new BufferedReader(new FileReader("D:\\paihang.txt"));
String count1=null;
while((count1=br.readLine())!=null) {
al.add(Integer.valueOf(count1));
counts =al.toArray();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Arrays.sort(counts);
for(int i=0;i<counts.length;i++) {
if((int)counts[i]==count) {
System.out.println("您的排名是"+(counts.length- i));
}
}
}
public static void Ranking(int count) {
FileWriter fw=null;
BufferedWriter bw=null;
// FileReader reader=null;
try { //try catch捕获异常
//输入
fw=new FileWriter("D:\\paihang.txt",true); //分数录入
bw=new BufferedWriter(fw);
String s=Integer.toString(count);
bw.write(s); //记录分数
bw.write("\n");
bw.flush();
System.out.println("操作完毕!---"+count);
bw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
原文:https://www.cnblogs.com/zhn2000520/p/13216164.html