私は、Javaに新たなんだと、私はこのコードのビットとのトラブルを抱えている:私はこのエラーをコンパイルしよう初心者:ユーザー入力を返す方法は?
import java.util.Scanner;
public class BattleshipLogic {
private final int rows;
private final int cols;
private final boolean[][] hits;
private final boolean[][] ships;
public BattleshipLogic(int numRows, int numCols) {
this.rows = numRows;
this.cols = numCols;
this.hits = new boolean[rows][cols];
this.ships = new boolean[rows][cols];
}
public void startGame() {
placeShips();
boolean gameOver = false;
// draw field for the first time
drawUI();
while (!gameOver) {
Target target = askForTarget();
processTarget(target);
drawUI();
}
}
private Target askForTarget() {
Target t = new Target();
Scanner input = new Scanner(System.in);
System.out.print("Please enter your target:");
t = input.nextInt();
return t;
}
/*
* This is just an example. You have to draw the ships by yourself.
* If you don't like the way the UI is drawn, make your own stuff!
*/
private void drawUI() {
// draw the ui, see blatt01/oberfläche.txt for an example
System.out.println("Schiffeversenken");
System.out.println("================");
System.out.println();
// columns
System.out.print(" ");
for (int col = 0; col < this.cols; col++) {
System.out.print(col + " ");
}
System.out.println();
System.out.print(" _");
for (int col = 0; col < this.cols; col++) {
System.out.print("__");
}
System.out.println();
for (int row = 0; row < this.rows; row++) {
System.out.print(Character.toChars(3 + row));
System.out.print(" |");
System.out.println();
}
}
private void placeShips() {
// do useful stuff to place the ships here
// see Blatt01 for rules
}
private void processTarget(Target target) {
}
/**
* This class only holds the position
* of a target.
*/
private class Target {
public int row;
public int col;
}
}
毎回がアップします:
エラー:互換性のない型を:int型は変換できませんBattleshipLogic.Target
に私はタイプが異なっていることを知っているが、対象は、タイプの種類ですか? は、どのように私は、ユーザー入力をtに割り当てられるように得ることができますか?
事前のおかげでそんなに!あなたのトンにあなたが
getter and setter method
が必要
t of type Target
にint型の値を割り当てる
このターゲットクラスとは何ですか? intをとるセッターメソッドはありますか?またはintをとるコンストラクタ?また問題があります。入力が必要なたびに、System.inに基づいて新しいScannerオブジェクトを作成し続けることは望ましくありません。 1つを作成し、必要な場所に渡します。 –
私はあなたが返す必要があると仮定したい '新しいターゲット(input.nextInt())'が、そのためにあなたは、ターゲットクラスの適切なコンストラクタが必要になります。 Tarhetのコードは非常に高く評価されます。 – Gyrotank
あなたが行うときは、型ターゲットの変数tにint型を割り当てる「T = input.nextInt()」。ターゲットのクラス定義を表示すると便利です。 –