import java.util.Random;
import java.util.*;
public class WhackAMole {
int score;
int molesLeft;
int attemptsLeft;
char[][] molegrid;
int actualDimension;
WhackAMole(int numAttempts,int gridDimension){
this.attemptsLeft=numAttempts;
this.actualDimension=gridDimension;
score=0;
molesLeft=10;
molegrid=new char[10][10];
for(int i=0;i<gridDimension-1;i++) {
for(int j=0;j<gridDimension-1;j++) {
molegrid[i][j]='*';
}
}
}
boolean place(int x,int y) {
if((x<actualDimension && x>=0)&& (y<actualDimension && y>=0)) {
molegrid[x][y]='M';
return true;
}
else {
return false;
}
}
void whack(int x,int y) {
if(molegrid[x][y]=='M'){
score++;
molesLeft--;
attemptsLeft--;
molegrid[x][y]='W';
System.out.println("You have made a whack");
System.out.println("You have" +attemptsLeft+"remaining tries");
}
else {
attemptsLeft--;
System.out.println("You have" +attemptsLeft+"remaining tries");
System.out.println("("+x+","+y+") doesnt have a mole");
}
}
void printGridToUser() {
for(int i=0;i<actualDimension;i++)
{
for(int j=0;j<actualDimension;j++) {
if(molegrid[i][j]=='M') {
molegrid[i][j]='*';
System.out.print(molegrid[i][j] );
molegrid[i][j]='M';
}
else {
System.out.print(molegrid[i][j] );
}
}
System.out.print("\n");
}
}
void printGrid() {
for(int i=0;i<actualDimension;i++) {
for(int j=0;j<actualDimension;j++) {
System.out.print(molegrid[i][j]);
}
System.out.print("\n");
}
}
public static void main(String[] args) {
WhackAMole a=new WhackAMole(50,10);
for(int i=0;i<10;i++) {
Random randomGenerator = new Random();
Random randomGenerator1 = new Random();
int molelocationx= randomGenerator.nextInt(9);
int molelocationy= randomGenerator1.nextInt(9);
boolean b= a.place(molelocationx, molelocationy);
if(b==true) {
System.out.println("Mole placed");
}
else {
System.out.println("Mole not placed");
}
}
System.out.println("You have maximum 50 chances");
for(int j=0;j<50;j++) {
Scanner scanner=new Scanner(System.in);
System.out.println("Enter first coordinate");
int userlocx=scanner.nextInt();
Scanner scanner1=new Scanner(System.in);
System.out.println("Enter second coordinate");
int userlocy=scanner1.nextInt();
if(userlocx==-1 && userlocy==-1)
{
System.out.println("Exiting");
System.out.println("Your score is"+a.score);
a.printGrid();
System.exit(1);
}
else if(userlocx>9 || userlocx<-1 || userlocy>9 || userlocy<-1 ){
System.out.println("Invalid");
continue;
}
else {
a.whack(userlocx, userlocy);
a.printGridToUser();
if(a.molesLeft==0) {
System.out.println("You have won!!");
System.exit(2);
}
}
}
System.out.println("Game over try again next time");
}
}
显示的错误 /WhackAMoleTestGrader.java:98:错误:找不到符号 int actualRow = whack.moleGrid.length; ^ 符号:变量moleGrid 位置:类型为WhackAMole的变量whack /home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:102:错误:找不到符号 int actualCol = whack.moleGrid [i] .length; ^ 符号:变量moleGrid 位置:类型为WhackAMole的变量whack /home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:107:错误:找不到符号 charactualChar = whack.moleGrid [i] [j]; ^ 符号:变量moleGrid 位置:类型为WhackAMole的变量whack /home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:123:错误:找不到符号 charactualOne = whack.moleGrid [1] [1]; ^ 符号:变量moleGrid 位置:类型为WhackAMole的变量whack /home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:125:错误:找不到符号 assertEquals(“在(1,2)处预期字符:M,但实际:” + whack.moleGrid [1] [2],'M',whack.moleGrid [1] [2]); ^ 符号:变量moleGrid 位置:类型为WhackAMole的变量whack /home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:125:错误:找不到符号 assertEquals(“在(1,2)处预期字符:M,但实际:” + whack.moleGrid [1] [2],'M',whack.moleGrid [1] [2]); ^ 符号:变量moleGrid 位置:类型为WhackAMole的变量whack /home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:129:错误:找不到符号 assertEquals(“痣放在错误的地方:(” + i +“,” + j +“)”,'*',whack.moleGrid [i] [j]); ^ 符号:变量moleGrid 位置:类型为WhackAMole的变量whack /home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:139:错误:找不到符号 assertEquals(“(1,1)没有痣”,'M',whack.moleGrid [1] [1]); ^ 符号:变量moleGrid 位置:类型为WhackAMole的变量whack 8个错误 (失败) 命令以非零状态1退出
答案 0 :(得分:0)
我可以编译您的代码,并且没有错误。
1)您的代码中没有ActualRow,但这是第一个错误。其他都一样...
我认为您需要编写测试程序将调用的代码。并且他们假设您有ActualRow等。再次阅读您的任务...
2)它看起来不像Java编译器输出。看起来像是用C ++或Python编译器编译的代码。检查您必须使用哪种语言进行提交,或者可能存在用于选择提交语言的复选框。