简体   繁体   English

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

[英]Passing Values from Constructor to a Method

My project requires me to make a game where two space ships move around on a game board. 我的项目要求我制作一个游戏,其中两艘太空船在游戏板上移动。 I'm not too sure on how to get my X and Y position values from my constructors to my method in my main program. 我不太确定如何从我的构造函数中获取我的X和Y位置值到我的主程序中的方法。

I got a bit of help from my professor and he said to pass the X and Y values into my print board method I tried to use ship1.XPos, ship1.YPos, ship2.XPos, ship2.YPos in my print board declaration but I got an error about VariableDeclaratiorId. 我得到了我的教授的一些帮助,他说将X和Y值传递给我的印刷板方法我试图在我的印刷板声明中使用ship1.XPos,ship1.YPos,ship2.XPos,ship2.YPos但是我有关VariableDeclaratiorId的错误。

Here is my main as it is currently as is right now 这是我目前的主要内容

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;
  }

 }
}


Here is my Spaceship1 constructor. 这是我的Spaceship1构造函数。 It is the same as my Spaceship2 constructor so there's no need to add it 它与我的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);
   }
   }
}

I expect there to be the words Ship1 and Ship2 on any space on my game board that is declared as 6x22. 我希望在我的游戏板上宣称为6x22的任何空间都有Ship1和Ship2这两个词。

When you define a method, you need to define the arguments it accepts using their types and names that the method will use to refer to those arguments. 定义方法时,需要使用它们用于引用这些参数的类型和名称来定义它接受的参数。 For example, your code: 例如,您的代码:

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

should actually be written like so: 应该像这样写:

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

The reason your code doesn't work is because you're trying to define the method using the values you want to pass into it (eg, ship1.XPos ). 您的代码不起作用的原因是您尝试使用要传递给它的值来定义方法(例如, ship1.XPos )。 When you want to call the method, then you can give it the values that you want it to use, like so: 当你想调用方法时,你可以给它你想要它使用的值,如下所示:

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

Keep in mind that you also have the following line of code which won't work because you're not passing a value for all of the arguments it expects: 请记住,您还有以下代码行无法正常工作,因为您没有为它所期望的所有参数传递值:

printBoard(f);

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

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