有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

重启java程序

我希望能得到一些帮助。我有一个关于战舰的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];
    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);
}

这是我的主要方法。任何批评都会很好


共 (1) 个答案

  1. # 1 楼答案

    首先,将(几乎)所有内容放在上述代码的第一部分(直到do-while语句)的introPhase()方法或其他地方

    第二,把你的do-while放进第二个gamePhase()方法或者别的什么

    最后,用某种类型的条件循环语句将两者包围起来

    你应该以这样的方式结束:

    boolean finishFlag = false;
    while(!finishFlag) {
        introPhase();
        gamePhase();
        finishFlag = getExitInfoFromUser();
    }
    

    祝你好运