java - Java 游戏不会在 Java 中仅在 CMD 中启动
问题描述
我是一个初学者,我已经使用教程代码构建了这个战舰游戏,并且我已经更改了基本的东西,例如船只数量和列号和行号。Java 不会将我的游戏作为 .jar 文件启动。它仅在 CMD 中启动。请注意,我的笔记本电脑是 jarfixed,我可以正常打开任何 .jar 文件。是因为我的游戏没有真正的用户界面吗?或者是我的清单文件中的问题?昨天是我第一次编译成 .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;
}
}
解决方案
您的输出是默认值System.out
。这意味着需要有一些实体捕获该输出并将其显示给用户。
如果您在 cmd 中运行.jar
文件,则 cmd 充当该实体。
如果你只是双击.jar
没有人捕捉到输出 - 因此它只是被丢弃了。您的应用程序仍在运行,只是未显示输出。
如何解决?要么使用除 之外的输出方法System.out
,例如文本文件,要么明确表明应用程序只能从命令行运行(这是一件非常好的事情)。
如果您不想让用户手动执行命令行命令,您还可以创建一个简单的.bat
文件来打开cmd
并在其中运行您的文件。.jar
推荐阅读
- tensorflow - 健身房(Open AI)环境状态改变形状
- maven - 将 pom 版本号添加到 Artifactory 存储库路径
- react-hooks - 问题 React Hooks 项目中的反应草稿所见即所得包无法集成
- reactjs - 反应列表重复项
- mongodb - MongoDb - 触发相同查询后的内存读取与磁盘读取
- prisma - 将 2.29.0 prisma 版本更改为 1.12.0
- amazon-web-services - 在 yaml 中调整根卷大小执行但在 ec2 中不调整大小
- owasp - 在鼓励使用特殊字符的密码等字段上的 REST API 中,避免 WAF 误报的最佳编码实践是什么
- javascript - API 自动化 - 条件测试
- reactjs - 使用“npm run start”在生产环境中运行 create-react-app 是不是很糟糕?