重新启动java程序
我希望得到一些帮助。我有一个关于战舰的java游戏,我想让玩家在游戏结束后再玩一次,但我真的不知道怎么玩。任何帮助都会很好重新启动java程序,java,application-restart,Java,Application Restart,我希望得到一些帮助。我有一个关于战舰的java游戏,我想让玩家在游戏结束后再玩一次,但我真的不知道怎么玩。任何帮助都会很好 public class Battle { public static void main(String[] args) throws IOException{ int[][] board = new int[5][5]; int[][] ships = new int[3][2]; int[] shoot = new int[2]; i
public class Battle {
public static void main(String[] args) throws IOException{
int[][] board = new int[5][5];
int[][] ships = new int[3][2];
int[] shoot = new int[2];
int size;//for the fleet, under construction!
int health= 3;
int attempts=0,
shotHit=0;
boolean sendout = false;
//reads the rules
File myFile = new File ("Rules.txt");
Scanner inputFile = new Scanner(myFile);
while (inputFile.hasNext())
{
String str = inputFile.nextLine();
System.out.println(str);
}
inputFile.close();
Scanner position = new Scanner(System.in);
whatShip ship = new whatShip(health);
health = ship.WhatShip(health);
System.out.println("\n");
System.out.println("Enter the name of your vessel:");
String name = position.nextLine();
//intro read
System.out.println("\n");
System.out.println("INTRO");
System.out.println("\n");
System.out.println("Onbard the "+name);
File myFile1 = new File ("Intro.txt");
Scanner inputFile1 = new Scanner(myFile1);
while (inputFile1.hasNext())
{
String str = inputFile1.nextLine();
System.out.println(str);
}
inputFile1.close();
System.out.println("\n");
int row = entryIntMinMax("Enter "+name+"'s longitude, row", 1, 5);
int col = entryIntMinMax("Enter "+name+"'s latitude, column",1,5);
System.out.println("ALL HANDS, MAN YOUR STATIONS");
initBoard(board);
initShips(ships);
System.out.println();
do{
showBoard(board);
shoot(shoot);
attempts++;
if(hit(shoot,ships)){
hint(shoot,ships,attempts);
shotHit++;
}
else
hint(shoot,ships,attempts);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
changeboard(shoot, ships, board);
}
while(shotHit!=3);
System.out.println("\n\n Enemy fleet destroyed sir! You sunk 3 ships in "+attempts+" attempts");
showBoard(board);
}
这是我的主要方法。任何批评都会很好 首先,在introPhase()
方法或其他方法中将(几乎)所有内容放在上述代码的第一部分(直到do while
语句)
public class Battle {
public static void main(String[] args) throws IOException{
int[][] board = new int[5][5];
int[][] ships = new int[3][2];
int[] shoot = new int[2];
int size;//for the fleet, under construction!
int health= 3;
int attempts=0,
shotHit=0;
boolean sendout = false;
//reads the rules
File myFile = new File ("Rules.txt");
Scanner inputFile = new Scanner(myFile);
while (inputFile.hasNext())
{
String str = inputFile.nextLine();
System.out.println(str);
}
inputFile.close();
Scanner position = new Scanner(System.in);
whatShip ship = new whatShip(health);
health = ship.WhatShip(health);
System.out.println("\n");
System.out.println("Enter the name of your vessel:");
String name = position.nextLine();
//intro read
System.out.println("\n");
System.out.println("INTRO");
System.out.println("\n");
System.out.println("Onbard the "+name);
File myFile1 = new File ("Intro.txt");
Scanner inputFile1 = new Scanner(myFile1);
while (inputFile1.hasNext())
{
String str = inputFile1.nextLine();
System.out.println(str);
}
inputFile1.close();
System.out.println("\n");
int row = entryIntMinMax("Enter "+name+"'s longitude, row", 1, 5);
int col = entryIntMinMax("Enter "+name+"'s latitude, column",1,5);
System.out.println("ALL HANDS, MAN YOUR STATIONS");
initBoard(board);
initShips(ships);
System.out.println();
do{
showBoard(board);
shoot(shoot);
attempts++;
if(hit(shoot,ships)){
hint(shoot,ships,attempts);
shotHit++;
}
else
hint(shoot,ships,attempts);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
health =enemyShoot(row, col, health);
changeboard(shoot, ships, board);
}
while(shotHit!=3);
System.out.println("\n\n Enemy fleet destroyed sir! You sunk 3 ships in "+attempts+" attempts");
showBoard(board);
}
其次,把你的do while
放在第二个gamePhase()方法中
最后,用某种类型的条件循环语句将两者包围起来
你应该以这样的方式结束:
boolean finishFlag = false;
while(!finishFlag) {
introPhase();
gamePhase();
finishFlag = getExitInfoFromUser();
}
祝你好运 下面是另一个更抽象、更简单的例子,说明它的样子
定义类、方法和变量,以便更容易地处理您正在编程的东西
// Game class, here you define what the game would look like
public class Game {
private Scanner scanner = new Scanner(System.in);
// Could probably create a class Board and work on this class.
// private Board board;
private int[][] board;
// Could probably create a class Ship and work on this class.
// Could also include the two dimensional array in the Board class
// private Ship[][] ships board;
private int[][] ships;
// Could probably create a class Player and work on this class.
// private Player board;
private int attemtps;
private int health;
// Define a status the game is currently in
private enum Status {
IDLE, STARTED, ENDED;
}
// Variable for the current game status
private Status gamestatus;
// Nothing done, default is idle when new game class is initialized
// Maybe you have options and stuff and a menu, where you IDLE at.
public Game() {
gamestatus = Status.IDLE;
}
// This method "starts" the game, though defines variables to
// have Game beeing started
public void start() {
System.out.println("Welcome to battleship");
// Change status, maybe you want to validate the current gamestatus
// at some point
gamestatus = Status.STARTED;
// this method could initialize some variables and stuff to default values in order to represent a fresh game
setInitialValuesOfGame();
// Random loop that gets 5 inputs and emulates a game due to this.
int i = 0;
while(gamestatus.equals(Status.STARTED)) {
System.out.println("Test input: ");
String input = scanner.nextLine();
// Just make it look like there would happen something
board[i%5][0] = i;
ships[i%3][0] = i;
// Make game look like it did end after a few iteration
++i;
if(i == 5) {
end();
}
}
// Loop is over, ask for restart
restart();
}
public void restart() {
System.out.println("Hey would you like to restart: yes"); // Making it look like input
String input = "yes"; // Input here, but make it a literal for showing purposes
if (input.equals("yes")) {
start();
} else {
System.out.println("bye bye");
}
}
public void end() {
gamestatus = Status.ENDED;
// Do more stuff that would make the game end
}
// Setting default values
private void setInitialValuesOfGame() {
board = new int[5][5];
ships = new int[3][2];
attemtps = 0;
health = 3;
}
// This is just your starting point, i´d programm as less as possible in here.
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
首先,我不会将所有内容都写入一个大的main
方法,而是创建对象
,并使用方法
和变量
开始、暂停和结束。重新启动游戏或设置您喜欢的状态。在某个时候调用main
只是为了重新启动整个过程,这可能不是您想要的,但目前必须执行。@KevinEsche是的,我正在升级它,并将所有内容都转换为方法,main过去有150多行代码。但是我不知道如何在技术上重新启动游戏。一旦游戏结束,你可以提出一个问题,要求下一轮。如果答案是肯定的,您可以调用main(args)代码>再次运行,如果答案是“否”,则只需结束它。如果您想使用方法,请在循环中运行游戏,并在结束时调用方法(如果他们想重新启动)。在该方法中,您设置了一些条件,根据答案,循环继续或结束not@XtremeBaumer你可以这样调用main方法?@ChunkierLizard是的,但不要这样做。这不是最佳的解决方案,它应该是有效的,但这不是你希望它最终看起来的样子。