简体   繁体   中英

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.

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.

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. It is the same as my Spaceship2 constructor so there's no need to add it

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.

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 ). 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);

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