我想知道如何压缩以下代码(在许多级别上是多余的):
import java.util.*;
public class Randomstems {
private static final STEM[] allStemsW1 = new STEM[25], allStemsW2 = new STEM[25], allStemsW3 = new STEM[25];
public static void main(String[] args) {
Scanner getAnswer = new Scanner(System.in);
Random getRandomSTEM = new Random();
allStemsW1[0] = new STEM("ante", "before", 1);
allStemsW1[1] = new STEM("anti", "against", 2);
allStemsW1[2] = new STEM("bi", "two", 3);
allStemsW1[3] = new STEM("circum", "around", 4);
allStemsW1[4] = new STEM("com", "together", 5);
allStemsW1[5] = new STEM("con", "together", 6);
allStemsW1[6] = new STEM("de", "down", 7);
allStemsW1[7] = new STEM("dis", "away", 8);
allStemsW1[8] = new STEM("equi", "equal", 9);
allStemsW1[9] = new STEM("extra", "beyond", 10);
allStemsW1[10] = new STEM("inter", "between", 11);
allStemsW1[11] = new STEM("intra", "within", 12);
allStemsW1[12] = new STEM("intro", "into", 13);
allStemsW1[13] = new STEM("mal", "bad", 14);
allStemsW1[14] = new STEM("mis", "bad", 15);
allStemsW1[15] = new STEM("non", "not", 16);
allStemsW1[16] = new STEM("post", "after", 17);
allStemsW1[17] = new STEM("pre", "before", 18);
allStemsW1[18] = new STEM("semi", "half", 19);
allStemsW1[19] = new STEM("sub", "under", 20);
allStemsW1[20] = new STEM("super", "over", 21);
allStemsW1[21] = new STEM("syn", "together", 22);
allStemsW1[22] = new STEM("sym", "together", 23);
allStemsW1[23] = new STEM("tri", "three", 24);
allStemsW1[24] = new STEM("un", "not", 25);
// week 2
allStemsW2[0] = new STEM("ad", "to", 1);
allStemsW2[1] = new STEM("antropo", "man", 2);
allStemsW2[2] = new STEM("archy", "government", 3);
allStemsW2[3] = new STEM("ard", "always", 4);
allStemsW2[4] = new STEM("aqua", "water", 5);
allStemsW2[5] = new STEM("auto", "self", 6);
allStemsW2[6] = new STEM("audi", "hear", 7);
allStemsW2[7] = new STEM("bell", "war", 8);
allStemsW2[8] = new STEM("biblio", "book", 9);
allStemsW2[9] = new STEM("bio", "life", 10);
allStemsW2[10] = new STEM("cap", "take", 11);
allStemsW2[11] = new STEM("cede", "go", 12);
allStemsW2[12] = new STEM("cent", "one hundred", 13);
allStemsW2[13] = new STEM("centri", "center", 14);
allStemsW2[14] = new STEM("cide", "kill", 15);
allStemsW2[15] = new STEM("cise", "cut", 16);
allStemsW2[16] = new STEM("cred", "believe", 17);
allStemsW2[17] = new STEM("dict", "say", 18);
allStemsW2[18] = new STEM("ician", "specialist", 19);
allStemsW2[19] = new STEM("itis", "inflammation", 20);
allStemsW2[20] = new STEM("logy", "science", 21);
allStemsW2[21] = new STEM("miss", "send", 22);
allStemsW2[22] = new STEM("neo", "new", 23);
allStemsW2[23] = new STEM("port", "carry", 24);
allStemsW2[24] = new STEM("scrib", "writer", 25);
// week 3
allStemsW1[0] = new STEM("duct", "lead", 1);
allStemsW1[1] = new STEM("ex", "out", 2);
allStemsW1[2] = new STEM("fer", "carry", 3);
allStemsW1[3] = new STEM("hema", "blood", 4);
allStemsW1[4] = new STEM("homo", "same", 5);
allStemsW1[5] = new STEM("hydro", "water", 6);
allStemsW1[6] = new STEM("hypo", "under", 7);
allStemsW1[7] = new STEM("micro", "small", 8);
allStemsW1[8] = new STEM("mono", "one", 9);
allStemsW1[9] = new STEM("neuro", "nerve", 10);
allStemsW1[10] = new STEM("omni", "all", 11);
allStemsW1[11] = new STEM("pan", "all", 12);
allStemsW1[12] = new STEM("pend", "hang", 13);
allStemsW1[13] = new STEM("penta", "five", 14);
allStemsW1[14] = new STEM("phon", "sound", 15);
allStemsW1[15] = new STEM("photo", "light", 16);
allStemsW1[16] = new STEM("poly", "many", 17);
allStemsW1[17] = new STEM("proto", "first", 18);
allStemsW1[18] = new STEM("pseudo", "false", 19);
allStemsW1[19] = new STEM("re", "again", 20);
allStemsW1[20] = new STEM("spec", "look", 21);
allStemsW1[21] = new STEM("tele", "far", 22);
allStemsW1[22] = new STEM("tomy", "cut", 23);
allStemsW1[23] = new STEM("vid", "look", 24);
allStemsW1[24] = new STEM("viv", "life", 25);
// starting the 'test'
// total stuff
int totalCorrect = 0;
// 1st group of 25
int allCorrect1 = 0;
int didBefore1 = 0;
boolean stop1 = false;
while(!stop1) {
int amCorrect1 = 0;
didBefore1++;
if(didBefore1>1) {
for(int i=0; i<25; i++) { // reset
allStemsW1[i].reset();
}
}
int[] incorrectAnswers = new int[25];
for(int i=0; i<25; i++) {
int randoStem = getRandomSTEM.nextInt(allStemsW1.length);
if(i!=0) {
boolean needTo = true;
while(needTo) {
if(allStemsW1[randoStem].hasDoneThis) {
randoStem = getRandomSTEM.nextInt(allStemsW1.length);
}
for(int n=0; n<25; n++) {
if(allStemsW1[randoStem].hasDoneThis) {
randoStem = getRandomSTEM.nextInt(allStemsW1.length);
n = 25;
} else {
needTo = false;
}
}
}
}
STEM randomStem = allStemsW1[randoStem];
randomStem.didThis();
System.out.println("Please type the meaning of the STEM \"" + randomStem.getName() + "\" below:");
String answer = getAnswer.nextLine();
randomStem.answerQ(answer, (25-1-i));
if(randomStem.isCorrect) {
randomStem.isCorrect = false;
amCorrect1++;
} else {
randomStem.isCorrect = false;
incorrectAnswers[i] = i+1;
}
}
int points = amCorrect1*4;
System.out.println("-----------------------");
System.out.println("Results:");
System.out.println("You got " + amCorrect1 + "/25 right! You got " + points + " points!");
allCorrect1 += amCorrect1;
if(points != 100) {
for(int i=0; i<incorrectAnswers.length; i++) {
if(incorrectAnswers[i] != 0) { // ACTUALLY got WRONG
System.out.print("you got " + allStemsW1[i].getName() + " wrong, ");
} else if(i==(incorrectAnswers.length-1)) {
System.out.println(""); // end the ^^^^ print
}
}
}
if(points == 100 && didBefore1>1) {
stop1 = true;
} else if(points != 100 && didBefore1>1) {
System.out.println("Do you wish to try to get 100 again? (true/false)");
boolean yN = getAnswer.nextBoolean();
if(!yN && didBefore1!=0) {
stop1 = true;
}
}
}
System.out.println("-------------------------------------");
System.out.println("Results:");
System.out.println("You got " + allCorrect1 + "/" + (didBefore1*25) + " correct!");
totalCorrect+=allCorrect1;
// 2nd group of 25
int allCorrect2 = 0;
int didBefore2 = 0;
boolean stop2 = false;
while(!stop2) {
int amCorrect2 = 0;
didBefore2++;
if(didBefore2>1) {
for(int i=0; i<25; i++) { // reset
allStemsW2[i].reset();
}
}
int[] incorrectAnswers = new int[25];
for(int i=0; i<25; i++) {
int randoStem = getRandomSTEM.nextInt(allStemsW2.length);
if(i!=0) {
boolean needTo = true;
while(needTo) {
if(allStemsW2[randoStem].hasDoneThis) {
randoStem = getRandomSTEM.nextInt(allStemsW2.length);
}
for(int n=0; n<25; n++) {
if(allStemsW2[randoStem].hasDoneThis) {
randoStem = getRandomSTEM.nextInt(allStemsW2.length);
n = 25;
} else {
needTo = false;
}
}
}
}
STEM randomStem = allStemsW2[randoStem];
randomStem.didThis();
System.out.println("Please type the meaning of the STEM \"" + randomStem.getName() + "\" below:");
String answer = getAnswer.nextLine();
randomStem.answerQ(answer, (25-1-i));
if(randomStem.isCorrect) {
randomStem.isCorrect = false;
amCorrect2++;
} else {
randomStem.isCorrect = false;
incorrectAnswers[i] = i+1;
}
}
int points = amCorrect2*2;
System.out.println("-----------------------");
System.out.println("Results:");
System.out.println("You got " + amCorrect2 + "/25 right! You got " + points + " points!");
allCorrect2 += amCorrect2;
if(points != 100) {
for(int i=0; i<incorrectAnswers.length; i++) {
if(incorrectAnswers[i] != 0) { // ACTUALLY got WRONG
System.out.print("you got " + allStemsW2[i].getName() + " wrong, ");
} else if(i==(incorrectAnswers.length-1)) {
System.out.println(""); // end the ^^^^ print
}
}
}
if(points == 100 && didBefore2>1) {
stop2 = true;
} else if(points != 100 && didBefore2>1) {
System.out.println("Do you wish to try to get 100 again? (true/false)");
boolean yN = getAnswer.nextBoolean();
if(!yN && didBefore2!=0) {
stop2 = true;
}
}
}
System.out.println("-------------------------------------");
System.out.println("Results:");
System.out.println("You got " + allCorrect2 + "/" + (didBefore2*25) + " correct!");
totalCorrect+=allCorrect2;
// 3rd group of 25
int allCorrect3 = 0;
int didBefore3 = 0;
boolean stop3 = false;
while(!stop3) {
int amCorrect3 = 0;
didBefore3++;
if(didBefore3>1) {
for(int i=0; i<25; i++) { // reset
allStemsW3[i].reset();
}
}
int[] incorrectAnswers = new int[25];
for(int i=0; i<25; i++) {
int randoStem = getRandomSTEM.nextInt(allStemsW3.length);
if(i!=0) {
boolean needTo = true;
while(needTo) {
if(allStemsW3[randoStem].hasDoneThis) {
randoStem = getRandomSTEM.nextInt(allStemsW3.length);
}
for(int n=0; n<25; n++) {
if(allStemsW3[randoStem].hasDoneThis) {
randoStem = getRandomSTEM.nextInt(allStemsW3.length);
n = 25;
} else {
needTo = false;
}
}
}
}
STEM randomStem = allStemsW3[randoStem];
randomStem.didThis();
System.out.println("Please type the meaning of the STEM \"" + randomStem.getName() + "\" below:");
String answer = getAnswer.nextLine();
randomStem.answerQ(answer, (25-1-i));
if(randomStem.isCorrect) {
randomStem.isCorrect = false;
amCorrect3++;
} else {
randomStem.isCorrect = false;
incorrectAnswers[i] = i+1;
}
}
int points = amCorrect3*2;
System.out.println("-----------------------");
System.out.println("Results:");
System.out.println("You got " + amCorrect3 + "/25 right! You got " + points + " points!");
allCorrect3 += amCorrect3;
if(points != 100) {
for(int i=0; i<incorrectAnswers.length; i++) {
if(incorrectAnswers[i] != 0) { // ACTUALLY got WRONG
System.out.print("you got " + allStemsW3[i].getName() + " wrong, ");
} else if(i==(incorrectAnswers.length-1)) {
System.out.println(""); // end the ^^^^ print
}
}
}
if(points == 100 && didBefore3>1) {
stop3 = true;
} else if(points != 100 && didBefore3>1) {
System.out.println("Do you wish to try to get 100 again? (true/false)");
boolean yN = getAnswer.nextBoolean();
if(!yN && didBefore3!=0) {
stop3 = true;
}
}
}
System.out.println("-------------------------------------");
System.out.println("Results:");
System.out.println("You got " + allCorrect3 + "/" + (didBefore3*25) + " correct!");
totalCorrect+=allCorrect3;
// total stuff
System.out.println("-------------------------------------");
System.out.println("Total results:");
System.out.println("You got " + totalCorrect + "/100" + " correct!");
getAnswer.close();
}
}我知道这是很多代码,但基本上是相同的事情3倍以上。我最初尝试了一种冷凝器的方式,但它造成了许多错误。那也是在我有三个不同的茎表之前。
编辑:
下面是我在控制台中得到的内容(它是works...shortened版本):
// each one is done at least 50 times
Please type the meaning of the STEM "(random STEM from week 1)" below:
Please type the meaning of the STEM "(random STEM from week 2)" below:
Please type the meaning of the STEM "(random STEM from week 3)" below:如果你做对了,它就会说正确。它跟踪你得到正确的数量,在每个周末和结束时,它显示你的总分数是多少。
发布于 2018-08-25 22:27:14
https://codereview.stackexchange.com/questions/202434
复制相似问题