选题一 | 算术运算测试 |
题目要求 | |
实现十道100以内加减法数学题(五道加法,五道减法),能根据题目计算出答案,与输入答案对比,判断做题是否正确,最后选手和分数和名次保存到文件中,可以选择是否查询排行榜功能。 | |
使用Java知识 | |
Java基本输入输出、运算符、循环。 | |
选题二 | 猜数游戏 |
题目要求 | |
计算机产生随机数,猜中即胜,猜不中,提示是大了还是小了,继续猜,直至猜到,可以选择保存和用户信息 。保留用户测试次数,做出成绩排行榜。排行榜存放到文件中。 | |
使用Java知识 | |
循环、分支、变量、常用类等。 |
选题一:
package cvb;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class suanfa1 {
static ArrayList<Integer> al=new ArrayList<Integer> ();
static Object[] counts;//排行榜数组
private static Scanner input;
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 n1=r.nextInt(100); //随机生成两个100以内的数
int n2=r.nextInt(100);
if(n1<n2){ //加法
System.out.println(n1+"+"+n2+"=?");
int answer=input.nextInt();
if(answer==n1+n2){ //判断是否正确
count++;
System.out.println("恭喜你!答对了!");
}else{
System.out.println("很遗憾,回答错误!");
}
}
if(n1>n2){ //减法
System.out.println(n1+"-"+n2+"=?");
int answer=input.nextInt();
if(answer==n1-n2){ //判断是否正确
count++;
System.out.println("真厉害!答对了哦!");
}else{
System.out.println("很遗憾,回答错误请继续努力!");
}
}
}
System.out.println("您的最终得分为:"+count+"分");
suanfa1.Ranking(count);
FileInputStream fos=null; //最终得分排名存取
InputStreamReader oss=null;
BufferedReader br=null;
try {
fos=new FileInputStream("panghangbiao.txt");
oss=new InputStreamReader(fos);
br=new BufferedReader(oss);
String count1=null;
while((count1=br.readLine())!=null) {
//一次读入一行数据
al.add(Integer.valueOf(count1));
counts=al.toArray();
}
}catch(Exception e) {
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;
try { //try catch捕获异常
//输入
fw= new FileWriter("panghangbiao.txt",true); //得分录入
bw=new BufferedWriter(fw);
String s=Integer.toString(count);
bw.write(s); //记录得分
bw.write("\n");
bw.flush(); //把缓存区内容压入文件
System.out.println("操作完毕!----");
bw.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
选题二:
package asd;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class caishu {
public static void main(String[] args) {
// TODO Auto-generated method stub
Writer w=null;
Writer fw=null;
//录入玩家人数并输入名字:
Scanner p=new Scanner(System.in);
System.out.println("请输入玩家人数:"); //游戏人数
int person=p.nextInt();
String[] name = new String[person];
long[] time=new long[person];
int guess; //所猜数字
int count = 0; //所猜次数
int[]people = new int[person];
int[]counts = new int[people.length];
long starttime = System.currentTimeMillis(); //猜测数字循环
for(int i=0;i<person;i++){
System.out.println("请输入您的代号:");
name[i] = p.next();
Scanner input = new Scanner(System.in);
int number = (int)(Math.random()*100); //随机产生数字
System.out.println("输入所猜数:");
do{
guess=input.nextInt();
if(number<guess){
System.out.println("猜大了,请继续猜哦");
count++;
}else if(number>guess){
System.out.println("猜小了,请继续猜哦");
count++;
}else{
count++;
break;
}
}while(true);
long endtime = System.currentTimeMillis();
long[] times=new long[person];
System.out.println("所猜数字为:"+number);
System.out.println("所猜次数为:"+count);
System.out.println("所用时间为:"+((endtime-starttime)/1000)+"秒");
time[i]=(int)((endtime-starttime)/1000);
//猜测评价
if(count==1){
System.out.println("一次就对了!你真聪明!");
}else if(count>=2&&count<=5){
System.out.println("不错,再接再厉!");
}else{
System.out.println("别灰心,继续加油哦!");
}
//信息录入
try {
w=new FileWriter("info.txt",true);
w.write("代号:");
w.write(name[i]);
w.write("\r\n");
w.write("猜数所用次数:");
String count1= Integer.toString(count);
w.write(count1);
w.write(" 次 \r\n");
w.write("花费时间:");
String time1=Double.toString(time[i]);
w.write(time1);
w.write(" 秒 \r\n\n");
System.out.println("玩家信息已读入");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("游戏结束!");
//排名:
try {
fw = new FileWriter("rank.txt",true);
fw.write("排名(倒序):\n");
Arrays.sort(time);
System.out.println("PAIHANGBANG如下:\n");
//for(double score:counts) {
//String s2= Double.toString(score);
//fw.write(s2);
for(int i=0;i<people.length;i++)
{
System.err.println("第"+(i+1)+"名\n代号为:"+name[i]+"\n猜数所用时间:"+time[i]+"秒\n");
fw.write("代号:");
fw.write(name[i]);
fw.write("\r\n");
fw.write("花费时间:");
String time1=Double.toString(time[i]);
fw.write(time1);
fw.write(" 秒 \r\n\n");
}
// }
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}