简体   繁体   中英

How do I use the results of a Scanner in another method?

I'm making a PvP Battleship game, and I have methods: 1) initPlayerShips 2) showPlayerBoard

I have a scanner to register user input for where the ships are going to be placed in "initPlayerShips". How do I use the scanner results from "initPlayerShips" and place them in "showPlayerBoard"?

Heres what I have right now:

public static void initPlayerShips(int[][]Playerships, int[][] playerBoard){
Scanner input = new Scanner(System.in);

for(int shipCount=0;shipCount<10; shipCount++){
System.out.println("Row: ");
int rowCoordinate = input.nextInt();
System.out.println("Column: ");
int columnCoordinate = input.nextInt();
playerBoard[rowCoordinate-1][columnCoordinate-1]='S';
}
System.out.println(playerBoard);
}

public static void showPlayerBoard(int[][] playerBoard){
    Scanner input = new Scanner(System.in);
    System.out.println("\t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8 \t9 \t10");
    System.out.println();

    for(int row=0 ; row < 10 ; row++ ){
        System.out.print((row+1)+"");
        for(int column=0 ; column < 10 ; column++ ){
            if(playerBoard[row][column]==-1){
                System.out.print("\t"+"~");
            }else if(playerBoard[row][column]==0){
                System.out.print("\t"+"*");
            }else if(playerBoard[row][column]==1){
                System.out.print("\t"+"X");
            }

        }
        System.out.println();
    }// for
}

Either pass the values as parameter or use setter/getter.
You have two options.
a) pass the parameter: If after the input taken, from the method you will redirect to the method where you want to show taken input's value then simple pass the parameter while calling the method.
Example:

for(int shipCount=0;shipCount<10; shipCount++){
System.out.println("Row: ");
int rowCoordinate = input.nextInt();
System.out.println("Column: ");
int columnCoordinate = input.nextInt();
playerBoard[rowCoordinate-1][columnCoordinate-1]='S';
}
// System.out.println(playerBoard);
// Pass your parameter to show
showPlayerBoard(playerBoard)
}


b) You set the value suitable to use. Then simply get the value to show. This applies great if you want to use the value(s) in later (not immediately in/after the current method).
Example:

private int[][] playerBoard;

public void setPlayerBoard(int[][] playerBoard){
  this.playerBoard = playerBoard;
}

public int[][] getPlayerBoard(){
   return this.getPlayerBoard;
}

public static void initPlayerShips(int[][]Playerships, int[][] playerBoard){
Scanner input = new Scanner(System.in);

for(int shipCount=0;shipCount<10; shipCount++){
System.out.println("Row: ");
int rowCoordinate = input.nextInt();
System.out.println("Column: ");
int columnCoordinate = input.nextInt();
playerBoard[rowCoordinate-1][columnCoordinate-1]='S';
}
//System.out.println(playerBoard);
}

public static void showPlayerBoard(){
    Scanner input = new Scanner(System.in);
    System.out.println("\t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8 \t9 \t10");

    int[][] playerBoard = getPlayerBoard();
    for(int row=0 ; row < 10 ; row++ ){
        System.out.print((row+1)+"");
        for(int column=0 ; column < 10 ; column++ ){
            if(playerBoard[row][column]==-1){
                System.out.print("\t"+"~");
            }else if(playerBoard[row][column]==0){
                System.out.print("\t"+"*");
            }else if(playerBoard[row][column]==1){
                System.out.print("\t"+"X");
            }

        }
        System.out.println();
    }// for
}

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