【问题标题】:Minesweeper program terminates as soon as I run it扫雷程序在我运行后立即终止
【发布时间】:2016-04-27 02:26:34
【问题描述】:
    import java.util.Random;
import java.util.Scanner;
public class field {
    private int[][] mines;
    private char[][]game;
    private int Row, Column;
    Random random = new Random();
    Scanner keyboard = new Scanner(System.in);

//create field array
public field (){
    mines = new int[10][10];
    game = new char [10][10];
    startMines();
    randomMines();
    fillNum();
    startField();
}
//set win conditions
public boolean win(){
    int count=0;
    for(int Row = 1; Row<9; Row++)
        for(int column = 1; column<9; column++)
            if(game[Row][column]=='_')
                count++;
    //if 10 mines are found you win
    if(count==10)
        return true;
    else
        return false;
}
public void openNeighbors(){
    for(int i=-1;i<2;i++)
        for(int j=-1;j<2;j++)
            if((mines[Row+i][Column+j] != -1) && (Row !=0 && Column != 0 && Column !=9))
                game[Row+i][Column+j]=Character.forDigit(mines[Row+i][Column+j], 10);
}
//get the suspected mines position
public int getPosition(int Row, int Column){
    return mines[Row][Column];
}
//have the user input the position of the suspected mine
public boolean setPosition(){

    do{
        System.out.print("\nRow: ");
        Row = keyboard.nextInt();
        System.out.print("Column: ");
        Column = keyboard.nextInt();

        if( (game[Row][Column] != '_')&&((Row < 9 && Row > 0) && (Column< 9 && Column >0)))
            System.out.println("Field already shown");

        if(Row < 1 || Row >8 || Column < 1 || Column>8)
            System.out.println("Chose a number between 1 and 8.");

    }while((Row <1 || Row>8 || Column<1 || Column>8 || (game[Row][Column] != '_')));

    if(getPosition(Row, Column)==-1)
        return true;
    else
        return false;

}
//show the mine field
public void show(){
    System.out.println("\n    Rows");
    for(int Row = 8; Row>0;Row++){
        System.out.print("     "+Row+" ");

        for(int Column=1; Column<9;Column++){
            System.out.print("   "+game[Row][Column]);
        }
        System.out.println();

    }
    System.out.println("\n            1  2  3  4  5  6  7  8");
    System.out.println("                    Columns");

}
//show the hints for the game when selected
public void fillNum(){
    for(int row=1; row<9;row++)
        for(int column=1; column<9; column++){

            for(int i=-1;i<=1;i++)
                for(int j=-1;j<=1;j++)
                    if(mines[row][column]!=-1)
                        if(mines[row+i][column+j]==-1)
                            mines[row][column]++;


        }
}
//show the mines on the mine field
public void showMines(){
    for(int i=1; i<9;i++)
        for(int j=1;j<9;j++)
            if(mines[i][j]==-1)
                game[i][j]='*';

    show();

}
//generate the field
public void startField(){
    for(int i=1; i<mines.length; i++)
        for(int j=1; j<mines.length; j++)
            game[i][j]='_';

}
//start the mines
public void startMines(){
    for(int i=0; i<mines.length ; i++)
        for(int j=0; j<mines.length; j++)
            mines[i][j]=0;

}
//randomly place the 10 mines
public void randomMines(){
    boolean raffled;
    int Row, Column;
    for(int i=0;i<10;i++){

        do{
            Row = random.nextInt(8) +1;
        Column = random.nextInt(8) +1;
        if(mines[Row][Column]==-1)
            raffled=true;
            else
                raffled=false;

        }while(raffled);
        mines[Row][Column]=-1;
    }
}
}

矿场代码

 public class Play {
    boolean end = false;
    boolean win = false;
    private field field;
    int turn=0;

    public void Begin(){
        field = new field();
        Start(field);
    }
    public void Start(field field){
        do{
            turn++;
            System.out.println("Turn " +turn);
            field.show();
            end = field.setPosition();

            if(!end){
                field.openNeighbors();
                end = field.win();
            }

        }while(!end);
        if(field.win()){
            System.out.println("You found all 10 of the hidden mines in the minefield in " +turn +"turns.");
        field.showMines();
        }else{
            System.out.println("You hit a mine. You lose.");
        field.showMines();
        }
    }
    }

玩游戏代​​码

这些是我创建雷区和玩游戏的程序。当我编译并运行代码时,程序会立即终止,并且不会在控制台中显示任何其他内容。该代码似乎没有任何编译错误。任何人都可以提供解决此问题的任何帮助,我们将不胜感激。

【问题讨论】:

  • 你了解Java main method了吗?
  • 你想如何运行它?我不知道不会给出主要方法而不是发现错误。
  • public static void main(String[] args){new Play().Begin();}

标签: java terminate minesweeper


【解决方案1】:

修改你的 Play 类,使开头看起来像这样:

public class Play {
boolean end = false;
boolean win = false;
private field field;
int turn=0;

// All Java programs require  the following method to run, it must
// be declared public, static, void, named "main" and take a 
// String array as an argument.
public static void main(String[] args) {
    //  Because main must be static (defined at compile time, once per class)
    // you need to create an instance of your play class before you can
    // call Begin()
    new Play().Begin();
}

public void Begin(){
    field = new field();
    Start(field);
}

注意:按照 Java 约定,方法名以小写字母开头,因此 Begin() 应为 begin(),类名以大写字母开头,因此字段应为 Field(和 Field.java)。变量名通常也是小写的。不是必需的,但从长远来看,它使代码更易于阅读。

一旦你添加了你的主要方法。您的“显示” for 循环中也存在错误。我认为您想根据循环的设置方式减少 Row 而不是增加行。

//show the mine field
public void show(){
    System.out.println("\n    Rows");
    for(int Row = 8; Row>0;Row++){ // <-- Try Row-- here instead.

编程愉快!

【讨论】:

  • 感谢您的帮助,该程序现已完美运行。
  • @hhayes96 我很高兴这很有帮助!您介意将答案标记为正确吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
相关资源
最近更新 更多