gpt4 book ai didi

Java 游戏无法仅在 CMD 中在 Java 中启动

转载 作者:行者123 更新时间:2023-11-30 06:08:06 26 4
gpt4 key购买 nike

我是初学者,我使用教程代码构建了这个战舰游戏,并且更改了基本内容,例如舰船数量以及列数和行数。 Java 不会以 .jar 文件的形式启动我的游戏。它仅在 CMD 中启动。请注意,我的笔记本电脑已修复,我可以正常打开任何 .jar 文件。是因为我的游戏实际上没有 UI 吗?或者问题出在我的 list 文件中?昨天是我第一次编译为 .jar 文件。

list

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让用户手动执行命令行命令。

关于Java 游戏无法仅在 CMD 中在 Java 中启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50855125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com