1. 程式人生 > 其它 >java課堂測試3第一部分(未完善)

java課堂測試3第一部分(未完善)

package test3;
import java.util.*;

public class Grade2 {

static String[][] mis=new String[500][4]; //將錯誤的題放入其中

public static int generateRandom(int range) //生成隨機數
{
Random random=new Random();
int R=random.nextInt(range);
return R;
}

public static char generateOperator() //生成運算子
{
int i;
char a='0';
i=generateRandom(4);
switch(i)
{
case 0:a='+';break;
case 1:a='-';break;
case 2:a='*';break;
case 3:a='/';break;
}
return a;

}

public static String[][] generateProblem(int number,int range,int count) //生成算題,不帶括號的
{
int i,j,k,k1=0,k2=0;
char p;
String[][] str=new String[number][count*2];
for(i=0;i<number;i++)
{
for(j=0;j<count*2-1;j+=2)
{
k=generateRandom(range);
str[i][j]=String.valueOf(k);
}
for(j=1;j<count*2-1;j+=2)
{
if(count==2)
{
k1=Integer.valueOf(str[i][0]);
k2=Integer.valueOf(str[i][2]);
if((k1%k2)==0)
{
p=generateOperator();
str[i][j]=String.valueOf(p);
}
else
{
do {
p=generateOperator();
}while(p=='/');
str[i][j]=String.valueOf(p);
}
}
else
{
p=generateOperator();
str[i][j]=String.valueOf(p);
}
}
str[i][count*2-1]="=";
}
return str;
}

public static boolean ifRepeat(int number,int range,int count) { //判斷題目是否重複
int i,j,k=1;
String[][] str=new String[number][count*2];
str=generateProblem(number,range,count);
for(i=0;i<number;i++)
{
for(j=0;j<i;j++)
{
if(str[j].equals(str[i]))
{
return false;

}
else continue;
}
if(k==0) break;
else continue;
}
return true;

}

public static void put(int number,int range,int count) { //輸出
int i,j,k1,k2=0;
int x=0; //錯題數為x+1
int count1=0,count2=0,count3=0,count4=0;
float len=0;

String[][] str=new String[number][count*2];


Scanner sc=new Scanner(System.in);
str=generateProblem(number,range,count);

for(i=0;i<number;i++)
{
if(count==2)
{
count1=Integer.valueOf(str[i][0]);
count2=Integer.valueOf(str[i][2]);
}

for(j=0;j<count*2;j++)
{
System.out.print(str[i][j]);
}
k1=sc.nextInt(); //做題者輸入答案
for(j=1;j<count*2;j+=2)
{
if(count==2)
{
switch(str[i][j])
{
case "+":k2=count1+count2;break;
case "-":k2=count1-count2;break;
case "*":k2=count1*count2;break;
case "/":k2=count1/count2;break;
default:break;
}
if(k1==k2)
{
System.out.print("答案正確");
break;
}
else //存放進錯題本
{
len++;
for(j=0;j<count*2;j++)
{
mis[x][j]=str[i][j];
}
x++;
System.out.print("答案錯誤");
break;
}
}

}
System.out.println();
}

System.out.println("您本次答題的錯誤率為"+(len/number)*100+"%");
System.out.println("您是否需要再做一套題:1.是,2.否");
int s1=sc.nextInt();
if(s1==1)
{
put(number,range,count);
}
System.out.println("您是否針對錯題本練習一遍:1.是,2.否");
int s2=sc.nextInt();
if(s2==1)
{
putMisGrade2(number,range,count,x);
}
}

public static void putMisGrade2(int number,int range,int count,int x) //2年級的錯題本輸出
{
int count1=0, count2=0,k1,k2=0,i,j;
Scanner sc=new Scanner(System.in);
for(i=0;i<x;i++)
{
if(count==2)
{
count1=Integer.valueOf(mis[i][0]);
count2=Integer.valueOf(mis[i][2]);
}

for(j=0;j<count*2;j++)
{
System.out.print(mis[i][j]);
}
k1=sc.nextInt(); //做題者輸入答案
for(j=1;j<count*2;j+=2)
{
if(count==2)
{
switch(mis[i][j])
{
case "+":k2=count1+count2;break;
case "-":k2=count1-count2;break;
case "*":k2=count1*count2;break;
case "/":k2=count1/count2;break;
}
if(k1==k2)
System.out.println("答案正確");
}
}
}
}

public static void main(String[] args)
{
int number,range,count=2; //count 為運算元的個數
Scanner sc=new Scanner(System.in);

System.out.println("小學二年級計算題");

System.out.println("請輸入您所需要的題目個數:");
number=sc.nextInt();

System.out.println("請輸入您需要的操作題的範圍:");
range=sc.nextInt();

String[][] str=new String[number][count*2];
str=generateProblem(number,range,count);
put(number,range,count);
}

}