我的问题在于玩家船舶方法。我的for循环不重复我的扫描仪的字符串输入,但适用于int输入。扫描仪要求我输入方向,它可以工作,但它不适用于第二个循环。
/**
* Write a description of class SuperBattleShip here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.Random;
import java.util.Scanner;
public class SuperBattleShip
{
static final int[][]Computer = new int[5][5];
static final int[][]Player = new int[5][5];
static Scanner scanner = new Scanner(System.in);
static int Player1;
static int Player2;
static String Player3;
Random random = new Random();
public static void Intiateboard()
{
for(int x=0; x < 5; x++)
{
for(int y=0; y < 5; y++)
{
Player[x][y] = -1;
Computer[x][y] = -1;
}
}
}
public static void ShowboardPlayer()
{
System.out.println(" Your Ocean ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" | 0 | 1 | 2 | 3 | 4 | ");
System.out.println(" _____________________ ");
System.out.println(" ");
for(int x=0 ; x<5; x++)
{
System.out.print(x+" |");
for(int y=0 ; y<5; y++)
{
if(Player[x][y]==-1)
{
System.out.print(" ~ |");
}
else if(Player[x][y]==0)
{
System.out.print(" * |");
}
else if(Player[x][y]==1)
{
System.out.print(" X |");
}
else if(Player[x][y]==2)
{
System.out.print(" S |");
}
}
System.out.print("\n");
System.out.println(" _____________________ ");
System.out.println(" ");
}
//System.out.println(" 0 | "+ Player[0][0]+" | "+ Player[0][1]+" | "+ Player[0][2]+" | "+ Player[0][3]+" | "+ Player[0][4]+" | ");
//System.out.println(" _____________________ ");
//System.out.println(" ");
//System.out.println(" 1 | "+ Player[1][0]+" | "+ Player[1][1]+" | "+ Player[1][2]+" | "+ Player[1][3]+" | "+ Player[1][4]+" | ");
//System.out.println(" _____________________ ");
//System.out.println(" ");
//System.out.println(" 2 | "+ Player[2][0]+" | "+ Player[2][1]+" | "+ Player[2][2]+" | "+ Player[2][3]+" | "+ Player[2][4]+" | ");
//System.out.println(" _____________________ ");
//System.out.println(" ");
//System.out.println(" 3 | "+ Player[3][0]+" | "+ Player[3][1]+" | "+ Player[3][2]+" | "+ Player[3][3]+" | "+ Player[3][4]+" | ");
//System.out.println(" _____________________ ");
//System.out.println(" ");
//System.out.println(" 4 | "+ Player[4][0]+" | "+ Player[4][1]+" | "+ Player[4][2]+" | "+ Player[4][3]+" | "+ Player[4][4]+" | ");
//System.out.println(" _____________________ ");
//System.out.println(" ");
}
public static void ShowboardComputer()
{
System.out.println(" Computer's Ocean ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" | 0 | 1 | 2 | 3 | 4 | ");
System.out.println(" _____________________ ");
System.out.println(" ");
for(int x=0 ; x<5; x++)
{
System.out.print(x+" |");
for(int y=0 ; y<5; y++)
{
if(Computer[x][y]==-1)
{
System.out.print(" ~ |");
}
else if(Computer[x][y]==0)
{
System.out.print(" * |");
}
else if(Computer[x][y]==1)
{
System.out.print(" X |");
}
}
System.out.print("\n");
System.out.println(" _____________________ ");
System.out.println(" ");
}
}
public static void Showfield()
{
Intiateboard();
ShowboardPlayer();
System.out.println("\n\n ");
ShowboardComputer();
}
public static void Playerships()
{
System.out.println("Please choose the location of you ships.\n\n\n");
for( int s = 0; s<3;s++)
{
System.out.println("Ship "+(s+1)+"\n\n");
if( s>= 0 && s<3)
{
System.out.println("Type in Left,Right,Up, or Down for the direction of ship.");
Player3 = scanner.nextLine();
System.out.println("Type the x coordinate.");
Player1 = scanner.nextInt();
System.out.println("Type the y coordinate.");
Player2 = scanner.nextInt();
{
if(Player3.equals("Left"))
{
if(Player1 >= 2)
{
for(int x = Player1; x>Player1-3 ; x--)
{
Player[Player2][x] = 2;
}
}
else if(Player1 < 2)
{
System.out.println("Not possible due to your x coordinate, so choose a larger coordinate.");
}
}
else if(Player3.equals("Right"))
{
if(Player1 <=2)
{
for(int x = Player1; x<Player1+3 ; x++)
{
Player[Player2][x] = 2;
}
}
else if(Player1 > 2)
{
System.out.println("Not possible due to your x coordinate, so choose a smaller coordinate.");
}
}
else if(Player3.equals("Up"))
{
if(Player2 >=2)
{
for(int y = Player2; y>Player2-3 ; y--)
{
Player[y][Player1] = 2;
}
}
else if(Player2 < 2)
{
System.out.println("Not possible due to your y coordinate, so choose a larger coordinate.");
}
}
else if(Player3.equals("Down"))
{
if(Player2 <=2)
{
for(int y = Player2; y<Player2+3 ; y++)
{
Player[y][Player1] = 2;
}
}
else if(Player2 > 2)
{
System.out.println("Not possible due to your x coordinate, so choose a smaller coordinate.");
}
}
}
ShowboardPlayer();
}
}
}
public static void Playershoot()
{
}
public static void main(String[]args)
{
Intiateboard();
Showfield();
Playerships();
ShowboardPlayer();
}
}