Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java游戏并非仅在CMD中在Java中启动_Java_Jar - Fatal编程技术网

Java游戏并非仅在CMD中在Java中启动

Java游戏并非仅在CMD中在Java中启动,java,jar,Java,Jar,我是一个初学者,我已经用教程代码构建了这个战舰游戏,并且我已经改变了一些基本的东西,比如船的数量、列数和行数。Java不会以.jar文件启动我的游戏。它只从CMD开始。请注意,我的笔记本电脑是jarfixed的,我可以正常打开任何.jar文件。是因为我的游戏没有真正的UI吗?或者问题在我的清单文件中?昨天是我第一次编译成.jar文件 清单 Main-Class: BattleShip Java代码 import java.util.Random; import java.util.Scanne

我是一个初学者,我已经用教程代码构建了这个战舰游戏,并且我已经改变了一些基本的东西,比如船的数量、列数和行数。Java不会以.jar文件启动我的游戏。它只从CMD开始。请注意,我的笔记本电脑是jarfixed的,我可以正常打开任何.jar文件。是因为我的游戏没有真正的UI吗?或者问题在我的清单文件中?昨天是我第一次编译成.jar文件

清单

Main-Class: BattleShip
Java代码

import java.util.Random;
import java.util.Scanner;

public class BattleShip {

public static void main(String[] args) {
    int[][] board = new int[7][7];
    int[][] ships = new int[5][2];
    int[] shoot = new int[2];
    int attempts=0,
        shotHit=0;

    //method initBoard is triggered to create the board with the number '-1' 
   in all positions
    initBoard(board);
    //method initShips is triggered to fill the position of the 5 ships (row 
   and column)
    initShips(ships);

    System.out.println();

    //the game begins using a do...while loop, game goes on until the player 
hits the 5 ships
    do{
        showBoard(board);
        shoot(shoot);
        attempts++;

        if(hit(shoot,ships)){
            hint(shoot,ships,attempts);
            shotHit++;
        }                
        else
            hint(shoot,ships,attempts);

        changeboard(shoot,ships,board);

    //condition of the loop is "shotHit!=5'
    }while(shotHit!=5);

    System.out.println("\n\n\nWell done soldier! You've destroyed 5 enemy 
   ships in "+attempts+" attempts");
    showBoard(board);
}
//sets the value -1 in all blocks of the board
public static void initBoard(int[][] board){
    for(int row=0 ; row < 7 ; row++ )
        for(int column=0 ; column < 7 ; column++ )
            board[row][column]=-1;
}
//gets the int matrix and shows the board game
public static void showBoard(int[][] board){
    System.out.println("\t1 \t2 \t3 \t4 \t5 \t6 \t7");
    System.out.println();

    for(int row=0 ; row < 7 ; row++ ){
        System.out.print((row+1)+"");
        for(int column=0 ; column < 7 ; column++ ){
            if(board[row][column]==-1){
                System.out.print("\t"+"~");
            }else if(board[row][column]==0){
                System.out.print("\t"+"*");
            }else if(board[row][column]==1){
                System.out.print("\t"+"X");
            }

        }
        System.out.println();
    }

}

//this method randomly select 5 pairs of integers numbers, which are the 
location of the 5 ships
public static void initShips(int[][] ships){
    Random random = new Random();

    for(int ship=0 ; ship < 5 ; ship++){
        ships[ship][0]=random.nextInt(7);
        ships[ship][1]=random.nextInt(7);

        //let's check if that shot was already tried 
        //if it was, just finish the do...while when a new pair was randomly 
        selected
        for(int last=0 ; last < ship ; last++){
            if( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == 
          ships[last][1]) )
                do{
                    ships[ship][0]=random.nextInt(7);
                    ships[ship][1]=random.nextInt(7);
                }while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] 
                 == ships[last][1]) );
        }

    }
}

//gets a shot (row and column) of the user, and stores in variable shot []
 public static void shoot(int[] shoot){
    Scanner input = new Scanner(System.in);

    System.out.print("Row: ");
    shoot[0] = input.nextInt();
    shoot[0]--;

    System.out.print("Column: ");
    shoot[1] = input.nextInt();
    shoot[1]--;

}

//checks if given shot hit a ship
public static boolean hit(int[] shoot, int[][] ships){

    for(int ship=0 ; ship<ships.length ; ship++){
        if( shoot[0]==ships[ship][0] && shoot[1]==ships[ship][1]){
            System.out.printf("What a SOLDIER! You hit an enemy ship located 
     in (%d,%d) with a hellstorm missle\n",shoot[0]+1,shoot[1]+1);
            return true;
        }
    }
    return false;
}

//give a hint of how many ships are in that row and that column where the 
shot was given
public static void hint(int[] shoot, int[][] ships, int attempt){
    int row=0,
        column=0;

    for(int line=0 ; line < ships.length ; line++){
        if(ships[line][0]==shoot[0])
            row++;
        if(ships[line][1]==shoot[1])
            column++;
    }

    System.out.printf("\nHint %d: \nRow %d -> %d ships\n" +
                             "Column %d -> %d 
ships\n",attempt,shoot[0]+1,row,shoot[1]+1,column);
}

//after the shot is given, the board is changed, showing that the shot was 
give (if hit or missed)
public static void changeboard(int[] shoot, int[][] ships, int[][] board){
    if(hit(shoot,ships))
        board[shoot[0]][shoot[1]]=1;
    else
        board[shoot[0]][shoot[1]]=0;
}
}
import java.util.Random;
导入java.util.Scanner;
公共级战舰{
公共静态void main(字符串[]args){
int[][]板=新int[7][7];
国际贸易[][]船舶=新国际贸易[5][2];
int[]shot=新int[2];
int=0,
shotHit=0;
//方法initBoard被触发以创建编号为'-1'的板
在所有位置
董事会(董事会);
//触发方法initShips以填充5艘船(世界其他地区)的位置
(和栏)
船舶(船舶);
System.out.println();
//游戏开始使用do…while循环,直到玩家
击中5艘船
做{
展板;
射(射);
尝试++;
如果(命中(射击,飞船)){
提示(射击、发射、尝试);
shotHit++;
}                
其他的
提示(射击、发射、尝试);
配电盘(发射、运输、配电盘);
//循环条件为“shotHit!=5”
}while(shotthit!=5);
System.out.println(“\n\n\n干得好,士兵!你摧毁了5个敌人
以“+尝试+尝试”方式发货;
展板;
}
//在电路板的所有块中设置值-1
公共静态无效初始化板(int[][]板){
用于(int行=0;行<7;行++)
for(int列=0;列<7;列++)
线路板[行][列]=-1;
}
//获取整数矩阵并显示棋盘游戏
公共静态无效展示板(int[][]板){
System.out.println(“\t1\t2\t3\t4\t5\t6\t7”);
System.out.println();
用于(int行=0;行<7;行++){
System.out.print((行+1)+“”);
for(int列=0;列<7;列++){
if(板[行][列]=-1){
系统输出打印(“\t”+“~”);
}else if(板[行][列]==0){
系统输出打印(“\t”+“*”);
}else if(板[行][列]==1){
系统输出打印(“\t”+“X”);
}
}
System.out.println();
}
}
//此方法随机选择5对整数,它们是
5艘船的位置
公共静态无效初始船舶(int[][]船舶){
随机=新随机();
对于(int ship=0;ship<5;ship++){
ships[ship][0]=random.nextInt(7);
ships[ship][1]=随机。nextInt(7);
//让我们看看那枪是否已经试过了
//如果是的话,就完成这个动作……当一对新的随机出现时
挑选出来的
对于(int last=0;last对于(int ship=0;ship,您的输出是默认的
System.out
。这意味着需要有一些实体捕捉该输出并将其显示给用户

如果在cmd中运行
.jar
文件,则cmd将充当该实体

如果您只需双击
.jar
,则没有人捕获输出-因此它被简单地丢弃。您的应用程序仍在运行,输出不会显示

如何修复它?可以使用非
System.out
的输出方法,例如文本文件,或者明确说明应用程序只能从命令行运行(这是一件非常好的事情)


你也可以创建一个简单的
.bat
文件,打开
cmd
并在其中运行你的
.jar
,如果你不想让用户手动执行命令行命令。

IMO-你的最后一段就可以了。谢谢你,Ben,我理解它,因为它是system.out,它只在cmd中打开。它有自己的GUI类我可以按照我的要求工作。但我对Java还不太了解,所以这是我稍后在Java方面进一步研究的内容。这听起来是一个完美的解决方案:)一步一步地进行,没有理由跳过。尝试以下操作:在行
主类:战列舰
之后,按enter键开始一行并保存。清单文件需要一行新行才能工作。然后看看Ben的回答我的清单文件已经用一行额外的行保存了