0
ボードに2D配列を使用するJavaでテキストゲームを作成しています。 "scan"コマンドを入力すると、同じ8x8ボードが毎回印刷されますので、このようにしたいと思います。プログラムはうまくコンパイルされました。私は "スキャン"コマンドを実行するたびに生成する同じ8x8ボードを印刷する方法を知る必要があります。これは単純なプログラムがある場合は2次元配列を格納し、後でJavaでそれらを呼び出す
import java.util.*;
import java.math.*;
import java.util.Random;
import java.util.Scanner;
import java.text.*;
import java.io.*;
public class Main {
static Scanner in = new Scanner(System.in);
public static void Start() {
System.out.println("Would you like a Short, Medium, or Long game?");
String x = in.nextLine();
System.out.println("Are you a Novice, Fair, Good, Expert, or Emeritus player?");
String y = in.nextLine();
System.out.println("Good Luck!");
System.out.println("What would you like to do?");
}
public static void main() {
//create the grid
String x;
String y;
String z;
z = in.nextLine();
if (z.equals("scan")) {
final int rowWidth = 8;
final int colHeight = 8;
Random rand = new Random();
boolean a = true;
boolean b = true;
boolean c = true;
boolean d = true;
String [][] board = new String[rowWidth][colHeight];
//fill the grid
for (char row = 0; row < board.length; row++) {
for (char col = 0; col < board[row].length; col++) {
double r = Math.random();
if(a == true && r <= .02) {
board[row][col] = "P";
a = false; }
else if(b == true && r <= .04 && r > .02){
board[row][col] = "K";
b = false; }
else if(c == true && r <= .06 && r > .04){
board[row][col] = "B";
c = false; }
else if(d == true && r <= .08 && r > .06){
board[row][col] = " ";
d = false; }
else
board[row][col] = "*";
}
}
//display output
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j] + " ");
//System.out.println();
}
System.out.println();
}
System.out.println("");
z = in.nextLine();
main();
}else if(z.equals("exit")) {
System.exit(0);
}else {
System.out.println("Parton");
main();
}
}
public static void run() {
Start();
main();
}
}//end of main
//end of class Main
1)スキャンコマンドが表示されません。 –
@blahfunk私は3つの文字列を宣言した後、私は入力を取得します。その入力が「スキャン」に等しい場合、8x8が生成されます。 –