将值从构造方法传递给方法

时间:2019-05-16 01:53:25

标签: java

我的项目要求我制作一个游戏,其中有两个太空船在游戏板上移动。我不太确定如何在我的主程序中将X和Y位置值从构造函数获取到方法。

我从教授那里得到了一些帮助,他说要将X和Y值传递到我的打印板方法中,我试图在我的打印板中使用ship1.XPos,ship1.YPos,ship2.XPos,ship2.YPos声明,但出现有关VariableDeclaratiorId的错误。

这是我目前的主要工作,

Java

package ship;
import java.util.*;

public class ShipGame {

 public static String[][] makeBoard() {

  String[][] f = new String[6][22];

  for (int i = 0; i < f.length; i++) {

   for (int j = 0; j < f[i].length; j++) {

    if (j % 2 == 0)

     f[i][j] = "|";

    else

     f[i][j] = "      ";

   }

  }

  return f;

 }

 public static void printBoard(String[][] f, ship1.XPos, ship1.YPos, ship2.XPos, ship2.YPos) {

  for (int i = 0; i < f.length; i++) {

   for (int j = 0; j < f[i].length; j++) {

     if(x == ship1.XPos && y == ship1.YPos){
       System.out.print(ship1);
     }

     else if (x == ship2.XPos && y == ship2.YPos){
       System.out.ptint(ship2);
     }

     else{

       System.out.print(f[i][j]);

     }

   System.out.println();

  }
 }
 }

 public static void main(String[] args) {

  int engine;

  System.out.println("Welcome First Captian! What kind of ship would you like to create: ");
  System.out.println("1. Battlecruiser");
  System.out.println("2. Destroyer");
  Scanner scan = new Scanner(System.in);
  engine = scan.nextInt();
  scan.nextLine();
  String engineType;

  if (engine == 1) {
   engineType = "Battlecrusier";
  } 
  else {
   engineType = "Destroyer";
  }

  System.out.println("What would you like to name your vessel?");

  String shipName1 = scan.nextLine();

  Spaceship1 ship1 = new Spaceship1(shipName1, engineType);

  System.out.println("Welcome Second Captian! What kind of ship would you like to create: ");
  System.out.println("1. Battlecruiser");
  System.out.println("2. Destroyer");
  engine = scan.nextInt();
  scan.nextLine();
  if (engine == 1) {
   engineType = "Battlecrusier";
  } 
  else {
   engineType = "Destroyer";
  }

  System.out.println("What would you like to name your vessel?");
  String shipName2 = scan.nextLine();


  Spaceship2 ship2 = new Spaceship2(shipName2, engineType);

  String[][] f = makeBoard();

  int count = 0;

  printBoard(f);
  boolean gaming = true;

  while (gaming) {

   if (count % 2 == 0) {
   ship1.movement1(f);

   }
   else {

   ship2.movement2(f);
   }
   count++;

   printBoard(f, ship1.XPos, ship1.YPos, ship2.XPos, ship2.YPos );

   gaming = false;
  }

 }
}


这是我的Spaceship1构造函数。它与我的Spaceship2构造函数相同,因此无需添加它

Java

package ship;
import java.util.Random;
import java.util.Scanner;

public class Spaceship1 extends ship {
  private String ship1;

  public Spaceship1(String shipName, String engineType) {
   super(shipName, engineType);

   double maxSpeed = Math.random() * 2 + 1;

   int shipHealth = (int) (Math.random() * 100 + 50);

   int attackPower = (int) (Math.random() * 20 + 5);

   Random rand = new Random();
   int newXPos = rand.nextInt(9);

   int newYPos = rand.nextInt(9);

   setShipHealth(shipHealth);
   setMaxSpeed(maxSpeed);
   setAttackPower(attackPower);
   setXPos(newXPos);
   setYPos(newYPos);
  }

  public void movement1(String[][] f) {

   System.out.println("W Move Up");
   System.out.println("S Move Down");
   System.out.println("A Move Left");
   System.out.println("D Move Right");

   Scanner scan = new Scanner(System.in);
   String move = scan.nextLine();

   int standX = getXPos();
   int standY = getYPos();
   double standS = getMaxSpeed();

   if(move == "W") 
   {
     standY += standS;
     setYPos(standY);
   }
   else if(move == "S") 
   {
     standY += standS;
     setYPos(standY);
   }
   else if(move == "A") 
   {
     standY += standS;
     setYPos(standY);
   }
   else if(move == "D") 
   {
     standY += standS;
     setYPos(standY);
   }
   }
}

我希望游戏板上任何声明为6x22的空间上都有Ship1和Ship2这两个词。

1 个答案:

答案 0 :(得分:0)

定义方法时,需要使用其类型和名称(该方法将用于引用这些参数的名称)来定义其接受的参数。例如,您的代码:

public static void printBoard(String[][] f, ship1.XPos, ship1.YPos, ship2.XPos, ship2.YPos)

实际上应该这样写:

public static void printBoard(String[][] f, int ship1Xpos, int ship1Ypos, int ship2Xpos, int ship2Ypos)

代码不起作用的原因是,您试图使用要传递给它的值(例如,ship1.XPos)来定义该方法。当您要调用该方法时,可以为它提供要使用的值,如下所示:

printBoard(f, ship1.XPos, ship1.YPos, ship2.XPos, ship2.YPos);

请记住,您还有下面的代码行将不起作用,因为您没有为其期望的所有参数传递值:

printBoard(f);