简体   繁体   English

如何以另一种方法使用扫描仪的结果?

[英]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我正在制作 PvP 战舰游戏,我有方法:1) initPlayerShips 2) showPlayerBoard

I have a scanner to register user input for where the ships are going to be placed in "initPlayerShips".我有一个扫描仪来注册用户输入的船只将被放置在“initPlayerShips”中的位置。 How do I use the scanner results from "initPlayerShips" and place them in "showPlayerBoard"?如何使用来自“initPlayerShips”的扫描结果并将它们放置在“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.将值作为参数传递或使用 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. a) 传递参数:如果在获取输入后,您将从该方法重定向到要显示所获取输入值的方法,然后在调用该方法时简单地传递参数。
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. b) 您设置了适合使用的值。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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