简体   繁体   中英

5x5 array of boxes on Java?

I am a new programmer and I have to complete a program that will establish a 5 x 5 array of boxes. The rows should be numbered 1-5 and the columns should also be numbered 1-5. The computer should pick a random column number and row number, and in this cell it should hide the number 7. The user should be started with 10 points and will be asked to pick a row and column. The computer should check the box indicated to see if it contains the number 7. If it does, it should print the user's score, the board and end the game. If the box chosen does not contain the number 7, it should subtract one from the users score and show the board. The player picks another box to continue the game. The game should continue this way until the player either finds the box with the number 7 or their score is zero.

I just cannot get this program, can someone help me out and tell me what I should add to my program/ what's wrong?

import java.io.*;
import java.util.Scanner;
public class Program6

{
public static void main (String args[])
{
Scanner scanner = new Scanner(System.in);
int num, score=10, b, a, x,y,r=0,c=0;

int columnrow[][]= new int[5][5];
char board[][]= new char[5][5];
char show[][]=new char[5][5];


x=(int)(Math.random()*5);
y=(int)(Math.random()*5);
columnrow[x][y]=7;


while(r!=x&&c!=y){
System.out.println("Pick row: ");
r=scanner.nextInt();
System.out.println("Pick column: ");
c=scanner.nextInt();
for(a=0; a<=4; a++){
  for(b=0; b<=4; b++){
    columnrow[a][b]=0;
    board[a][b]='O';
    show[a][b]='O';
    System.out.println(show[a][b]);
}

}

if(r==x&&y==c){
System.out.println("You win! Score: "+score);
}
if(r>5||r<1){
score=score-1;
System.out.print("Row is not between 1 and 5. Score: "+score);
}
if(c>5||c<1){
score=score-1;
System.out.print("Column is not between 1 and 5. Score: "+score);
}
}}}

A problem I am facing is that after the user pick a column and row and it is not between 1 and 5, the program just asks the question again and does not output "Row is not between 1 and 5. Score: " or "Column is not between 1 and 5. Score: ".

Use System.out.print for each column and print a \\n at the end of each row, something like:

for(a=0; a<=4; a++){
    for(b=0; b<=4; b++){
        columnrow[a][b]=0;
        board[a][b]='O';
        show[a][b]='O';
        System.out.print(show[a][b]);
    } 
    System.out.println("");
}

Since you didn't give us any idea what are your problems, ill write here all the mistakes i see in your code(maybe i missed a few):

1) use this code, to print the array(its better if you put in a different method):

    for(a=0; a<=4; a++){
      for(b=0; b<=4; b++){
         columnrow[a][b]=0;
         board[a][b]='O';
         show[a][b]='O';
         System.out.print(show[a][b]);
      } 
      System.out.println("");
    }

2) When the user enter a column or a row number, you should subtract 1 from it(since java arrays starts from 0)

3)Give vars better name, its really hard to understand the code with vars like "c"

Other than that the logic of the code looks ok, a few places i would use println and not print.

good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM