gpt4 book ai didi

java - 将值从构造函数传递到方法

转载 作者:行者123 更新时间:2023-12-02 05:36:49 24 4
gpt4 key购买 nike

我的项目要求我制作一个游戏,其中两艘 spaceship 在游戏板上移动。我不太确定如何将 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 字样。

最佳答案

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

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

关于java - 将值从构造函数传递到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56159752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com