• 締切済み

式の型は配列型で int に解決済み。が解けませ。

こんにちは、学校の課題で重力3目並べを作っていますが、式の型、int型、配列型というところでまったくつまずいてしまっています。どうか課題提出できますようお助けいただけますでしょうか。お願いいたします。私はJUNO eclipsをつかっております。 エラーは、34,50,53,66,118行目で”式の型は配列方でINTに解決済みである必要があります”です。 (申し訳ありません。文字数ぎりぎりで詰めて質問させていただきした。読みづらですがご容赦ください。 package Colonne5; public class Colonne5 { final static int jouer1 = 0; final static int jouer2 = 1; final static char [] pion = new char [] {'*','0', ' '}; final static int vide = 2; static final int PionEtoile = 1; static final int PionRond = -1; final static int Dwidth = 5; final static int Dheight = 5; final static int PreCOL = 0; final static int DerCOL = Dwidth - 1; final static int Hautligne = Dheight - 1; static int Basligne = 0; final static int H = 0 ; final static int V = 1 ; final static int[] HOR = new int[] {1, 0} ; final static int[] VER = new int[] {0, 1} ; final static int[] UP = new int[] {1, 1} ; final static int[] DOWN = new int[] {1, -1} ; final static int[][] DIRECTION = new int[][] {HOR, VER, UP, DOWN} ; private static int[][] damier ; // grid private static int name ; // player name private static char player ; // turn of gamer private static int result ; private static int impactLine ; public Colonne5 (int john){ damier = new int[Dwidth][Dheight]; for (int i = 0 ; i < Dwidth; i++) { for (int j = 0; j < Dheight; j++) { damier[i][j] = vide; } } this.name = john; player = jouer1; result = vide; } private boolean Libre(int column){ //グリッドが空いているか判断 return damier[column][Hautligne]==vide; } private boolean damierRempli() { int column = PreCOL; while (column <= DerCOL) { if (Libre(column)) return true ; column++ ; } return false ; } private void jouerCoup() throws IOException { //ゲーム開始 while (result == vide && damierRempli()) { afficher() ; int column =name[player]; System.out.print("Enter " + (PreCOL + 1) + " to " +(DerCOL +1) + " ?") ; System.in.read(); sommetColonne(column) ; if (coupGagnant(player, impactLine)) result = player ; else demanderCoup() ; } afficher() ; if (result == vide) System.out.print(" partie nulle ") ; else System.out.println(name[result] + " gagne") ; } private boolean coupGagnant (int column, int ligne) { //勝者判断 int i = 0 ; while (i < DIRECTION.length) { if (coupGagnant(column, ligne, DIRECTION[i])) return true ; i++ ; } return false ; } private static boolean coupGagnant(int column, int ligne ,int[] dir) { int nextColumn = column + dir[H] ; int nextLine = ligne + dir[V] ; int forward = 0 ; while (damier[nextColumn][nextLine] == player) { nextColumn += dir[H] ; nextLine += dir[V] ; forward++ ; } nextColumn = column - dir[H] ; nextLine = ligne - dir[V] ; int backward = 0 ; while (damier[nextColumn][nextLine] == player) { nextColumn -= dir[H] ; nextLine -= dir[V] ; backward++ ; } return backward + 1 + forward >= 3 ; } private static int demanderCoup() { //どちらのプレイヤーの順番か判断 if(player==jouer1){ player = jouer2; } else player = jouer1; return player; } private void sommetColonne(int column) { //現在の駒の一番上を取得判断 if(!Libre(impactLine)) throw new RuntimeException(" colonne pleine ") ; impactLine = Hautligne ; while (impactLine > Basligne && damier[Dheight-1][impactLine-1] == vide) impactLine-- ; damier[Basligne][impactLine] = player ; } private void afficher() { //ゲームの表示 for (int column = PreCOL ; column <= DerCOL ; column++) System.out.print("| " + (column + 1) + " "); System.out.println("|"); for (int line = Hautligne ; line >= Basligne ; line--) { for (int column = PreCOL ; column <= DerCOL ; column++) System.out.print("| " + pion[damier[column][line]] + " ") ; System.out.println("|"); } } public static void main(String[] args) { Colonne5 joue = new Colonne5(name) ; joue.jouerCoup(); } }

みんなの回答

  • hirotn
  • ベストアンサー率59% (147/246)
回答No.3

mainメソッドの Colonne5 joue = new Colonne5(name) ; これをみる限り、nameは配列ではないんですよね(変数nameフィールドがそのまま引数に入ってしまっているという問題※はありますが)。 一方、 System.out.println(name[result] + " gagne") ; となっており、利用するときは配列になっているという矛盾があります。 このあたりの仕様がためをして、プログラムすることが必要だと思います。 ではどうなるべきかと考えてみると、jouer1,2 で先攻後攻が定義されていて、playerに現在の先攻後攻が格納されるようなので、次のようになると考えられます。(一案です) 変数nameの定義: String [2] name; Colonne5の生成: Colonne5 joue = new Colonne5(new String[]{"John", "Jane"}); コンストラクタでのnameの定義: this.name[0] = john[0]; 右辺 new String(john[0]);かもしれない this.name[1] = john[1]; sommetColonneメソッドから引数columnを外す(内部で使っていないから) ※前提として質問のプログラムから読み取れる限り あと、クラス変数(static)を使わなくてはならない理由が分からない変数があります。 damier以下です。以下URLを参考ください。 http://www.javaroad.jp/java_class9.htm

gyrate
質問者

お礼

ありがとうございます。そうですね、おっしゃる通りです。仕様を固めてからといいますか、もう1度仕様を考え直してみます。ご親切に参考URLまで教えていただき感謝しております。

全文を見る
すると、全ての回答が全文表示されます。
  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.2

「芋ずる式にエラーになってしまって困っています」ってのは, 具体的には ・どのようなプログラムで ・どのようなエラーが出ている のでしょうか? あと, ここに至るまでコンパイルしたことはなかったんですか? それにしても, ここまで英語とフランス語がごっちゃごちゃになると, なんというか笑える. ど~せなら全部フランス語で押し通してほしかった.

全文を見る
すると、全ての回答が全文表示されます。
  • hirotn
  • ベストアンサー率59% (147/246)
回答No.1

throws IOExceptionは、特にthrow指定なかったので、(エラーが出るためこれを省いた上で)javacで履かれた以下のエラー  Colonne5.java:53: 配列が要求されましたが、int が見つかりました。  int column =name[player]; ^  Colonne5.java:66: 配列が要求されましたが、int が見つかりました。  System.out.println(name[result] + " gagne") ; に対して、定義を見ると、次のようになっています。  private static int name ; // player name nameを配列にしないといけません。

gyrate
質問者

お礼

ありがとうございます。ビギナーですのでかなり難しい課題なのですが、教えていただいたように配列にしてみましたが書き方が悪かったようでやはりエラーをはいてしまいます。private static int name ;を配列として宣言してint[] name;にしまして、本体の中もこの書き方に適したようにしたのですが.....そうすると芋ずる式にエラーになってしまって困っています。確かにnameは配列のような感じで、53と66行目でname[player],name[result]という形で、この部分がやはりとてもややこしく難しいです。 もうすこし、色々変えてみます。早々のお答え本当にありがとうございます。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • 数値の定数を付ける時

    数値の定数名を付ける時、悩みます。 String[]の入力数の状態により処理が異なるとき この書き方では格好悪いです。 private static final int ZERO = 0; private static final int ONE = 1; private static final int TWO = 2; 上記に変わるもの private static final int NOT_VALUE = 0; private static final int ●●● = 1; private static final int △△△ = 2; ●●●と△△△に適切な定数名が思いつきません。 どういう風に考えたらいいのでしょうか?

    • ベストアンサー
    • Java
  • JComboBoxの配列の作り方について

    class PdMenu extends JPanel implements ActionListener { private static final long serialVersionUID = 5962740427029989204L; /*-------------------------* * Variables. * *-------------------------*/ /*----- Pull-down menu -----*/ JComboBox[] pd=new JComboBox[10]; String[] arrDate={"00", "01", "02", "03"}; List<String> date=new ArrayList<String>(); static String[] result; /*-------------------------* * Cons. * *-------------------------*/ /*----- Creation -----*/ PdMenu() { date=Arrays.asList(arrDate); result=date.subList(0, 3).toArray(new String[]{}); for(int i=0; i<pd.length; i++){ pd[i]=new JComboBox<String>(); } pd[0]=new JComboBox<String>(result); } public void actionPerformed(ActionEvent e) { /*------*/ } } Eclipseを使用しています。 JComboBox[] pd=new JComboBox[10];の部分において、ジェネリックタイプを付けるようにと の警告が表示されている状態です。 JComboBoxの配列を作りたいのですが、ジェネリックタイプを付けると配列が作れません。 JComboBoxの配列を作る場合は、ジェネリックタイプを付ける事ができません。 警告の表示されない良い方法はありませんでしょうか? みなさま、よろしくお願いいたします。

    • ベストアンサー
    • Java
  • 2つの配列を1つの配列にする。

    JAVA初心者です。1日に何度も質問してしまってすいません。 問題 private static int[] concat(int[] ia1, int[] ia2) のメソードを使いarray1とarray2[の2つの配列が両方intのとき、array1 array2と続く配列を表示する。 private static int[] concat(int[] ia1, int[] ia2) { int e; for (e=0; e < ia1.length; e++) { } for (e=0; e < ia2.length; e++) { }     return new int[0]; } public static void main(String[]args){ int[] testIntArray1 = {1, 2, 8, 4}; int[] testIntArray2 = {99, 88, 77, 66}; // You should also test the case where the first // array is empty, the second array is empty, and // both arrays are empty. System.out.println("First test array for concat is: "); int e; for (e=0; e < testIntArray1.length; e++) { System.out.print(testIntArray1[e] + " "); } System.out.println(); System.out.println("Second test array for concat is: "); for (e=0; e < testIntArray2.length; e++) { System.out.print(testIntArray2[e] + " "); } System.out.println(); int[] result = concat(testIntArray1, testIntArray2); System.out.println("Result of concat is: "); for (e=0; e < result.length; e++) { System.out.print(result[e] + " "); } System.out.println(); //*********************************************************** 結果 First test array for concat is: 1 2 8 4 Second test array for concat is: 99 88 77 66 Result of concat is: 1 2 8 4 99 88 77 66 ←2つの配列分の値。 メインはなんとかできたのですが、まだ始めたばかりで配列などの仕組みも基本的なことしか分りません。どのようにしたら2つの配列をひとつの配列に1番目の配列、2番目の配列の順に収める事ができるのでしょうか。キーワードなどのアドバイスよろしくお願いします。

    • ベストアンサー
    • Java
  • C#で独自の型を定義したい

    C# 2010 version 4.0を使用しています。 次のようなint型とstring型を混合したMixedという型を定義したいのですが、 public class Mixed { private int _TheInt = 0; public int TheInt { get { return _TheInt; } set { _TheInt = value; } } private string _TheString = null; public string TheString { get { return _TheString; } set { _TheString = value; } } public static implicit operator int(Mixed m) { return m.TheInt; } public static implicit operator Mixed(int x) { Mixed m = new Mixed(); m.TheInt = x; return m; } public static implicit operator string(Mixed m) { return m.TheString; } public static implicit operator Mixed(string x) { Mixed m = new Mixed(); m.TheString = x; return m; } } //Mixed m = 1; //Console.WriteLine(m); //これだとコンパイルエラーになる //Console.WriteLine(m.TheInt.ToString()); //「1」と表示される int i = 1; Console.WriteLine(i); //「1」と表示される object o = 1; Console.WriteLine(o); //「1」と表示される Console.WriteLineなどで表示する場合に 例えばint型なら int i = 1; Console.WriteLine(i); //「1」と表示される という具合にそのまま「1」と表示されます。 でもこのMixedの場合、 Mixed m = 1; Console.WriteLine(m.TheInt.ToString()); //「1」と表示される これでは「1」と表示されるのですが、 Console.WriteLine(m); //これだとコンパイルエラーになる このやり方だとエラーになります。 なんとかしてint型やobject型のようにToString()を使わずに 表示させることはできないでしょうか?

  • システムプロパティのキーの定数について

    * 暇なときにでも システムプロパティのキーの定数について これは、定数にする必要があるのでしょうか? "user.dir"は、javaで用意されているシステムプロパティのキーで, もともとこの形で用意されているのにも関わらず、定数にする必要はあるのでしょうか? (1) private final static String USER_DIR = "user.dir"; private final static String CURRENT_DIRECTORY = System.getProperty(USER_DIR); (2) private final static String CURRENT_DIRECTORY = System.getProperty("user.dir"); (1)、(2)でどちらがいいのかわかりません。 クオリティが高い書き方は、やはり(1)の書き方なのでしょうか?

    • ベストアンサー
    • Java
  • CSVファイルを多次元配列に格納する

    CSVファイルをopenCSVを読み込んでその行と列の要素数の多次元配列を作りその配列にデータを格納したいです。 しかし、データが格納できません。2回目の格納するためにwhileから何かおかしいのではないかと思っています。 なにかわかる方、アドバイスが欲しいです。 public class ReadCSV { public static void main(String[] args){ try{ CSVReader reader = new CSVReader( new FileReader("/home/masa/Desktop/WameiSample.csv")); //配列の宣言 String[] nextLine; //データを配列に入れる要素数を見る int j = 0; nextLine = reader.readNext(); int k = nextLine.length; System.out.println("列数[i]"+k); System.out.println("nextLine"+nextLine); while((nextLine = reader.readNext()) != null){ for (int i=0; i<nextLine.length; i++){ //System.out.print(nextLine[i] + "|" + i + "|"); } //System.out.println(); j++; } System.out.println("行数[j]"+j); //記憶する配列 String[][] Wamei = new String[k][j]; System.out.println("きてるよ"); //データを配列に格納していく int x = 0; while((nextLine = reader.readNext()) != null){ System.out.println("きてるよ");  <---こっから、表示してくれない. for (int y=0; y<nextLine.length; y++){ Wamei[x][y] = nextLine[y]; //多次元配列の要素を表示する System.out.print(Wamei[x][y]+"Wamei"+x+y); } System.out.println(); x++; } } catch (IOException e) { e.printStackTrace(); } } }

    • ベストアンサー
    • Java
  • 抽象クラスとオブジェクトを格納する配列(java)

    独習java第4版でわからない所があります。 abstract class Widget { String color; abstract int getMass(); public String toString() { return getClass().getName() + ": " + color + ", " + getMass(); } } class WidgetA extends Widget { final static int MASS = 4; WidgetA(String color) { this.color = color; } int getMass() { return MASS; } } class WidgetB extends Widget { final static int MASS = 1; WidgetB(String color) { this.color = color; } int getMass() { return MASS; } } class WidgetC extends Widget { final static int MASS = 5; WidgetC(String color) { this.color = color; } int getMass() { return MASS; } } class WidgetD extends Widget { final static int MASS = 17; WidgetD(String color) { this.color = color; } int getMass() { return MASS; } } class WidgetTypes { static int NUMWIDGETS = 6; public static void main(String args[]) { // 部品を格納する領域を宣言して割り当てる Widget widgets[] = new Widget[NUMWIDGETS]; // 部品を作成する widgets[0] = new WidgetC("Red"); widgets[1] = new WidgetA("Green"); widgets[2] = new WidgetD("Yellow"); widgets[3] = new WidgetB("Magenta"); widgets[4] = new WidgetA("Black"); widgets[5] = new WidgetC("White"); // 部品を処理する int totalMass = 0; for(int i = 0; i < NUMWIDGETS; i++) { Widget w = widgets[i]; System.out.println(w); totalMass += w.getMass(); } // 総重量を表示する System.out.println("Total mass = " + totalMass); } } これはある問題の解答ですが、僕にはどうしても理解出来ない部分があります。 mainのforループ内で Widget w = widgets[i]; totalMass += w.getMass(); となっています。 この仕組がわかりません。 まず Widget w = widgets[i] でWidget型の変数wにWidget型のwidget[i]を代入しているのにw.getMass()がエラーにならない理由がわかりません。(Widgetクラスは抽象クラスなのにwがWidgetクラスのインスタンスになっている?) これは 抽象メソッド( abstract int getMass() ) があるためでしょうか? ちなみにWidgetクラスとそのサブクラスからこの抽象メソッドを削除したらコンパイルエラーが出ました。 ではなぜ抽象クラスの抽象メソッドから、そのサブクラスのメソッドまで範囲が伸びるのでしょうか? どういう仕組でしょうか? この質問を書きながら思ったのですが、どうも配列の仕組みや抽象クラス・メソッドの仕組み、「オブジェクト」と「インスタンス」の違いがよくわかってないようです。 多分問題の本質はそこにあると思うんです。 駄文で申し訳ないです。 よろしくお願いします。

    • ベストアンサー
    • Java
  • 簡単な配列の作り方

    やりたいことがあるのですが、原始的なやり方しか思いつきません。 簡単な方法があればご教授願いたく思い質問いたしました。 よろしくお願いします! <やりたいこと> 明細行が5件ありまして、それぞれの行に有効かどうかフラグがある。 5件のうち有効になっている数だけの配列を作成する。 --------------------------------------------- 例1)1,2,3,4,5のうち2,3,5の3行が有効の場合   String[] str = String[]{2,3,5} 例2)1,2,3,4,5のうち4の1行が有効の場合   String[] str = String[]{4} --------------------------------------------- <現在やっていること> // 有効行の判断用 private boolean yukoFlg1 = false; private boolean yukoFlg2 = false; private boolean yukoFlg3 = false; private boolean yukoFlg4 = false; private boolean yukoFlg5 = false; // 有効行の数 private long yukoCnt = 0; /** * 該当行分の配列にする String[] * @param str 配列にしたい値 */ public String[] setStrArray ( String str1, String str2, String str3, String str4, String str5) throws Exception { String[] result = null; // 有効行の数が1の場合 if (yukoCnt == 1) { if (yukoFlg1) { result = new String[]{str1}; } else if (yukoFlg2) { result = new String[]{str2}; } else if (yukoFlg3) { result = new String[]{str3}; } else if (yukoFlg4) { result = new String[]{str4}; } else { result = new String[]{str5}; } // 有効行の数が2の場合 } else if (yukoCnt == 2) { if (yukoFlg1) { if (yukoFlg2) { result = new String[]{str1, str2}; } else if (yukoFlg3) { result = new String[]{str1, str3}; } else if (yukoFlg4) { result = new String[]{str1, str4}; } else { result = new String[]{str1, str5}; } } else if(yukoFlg2) { if (yukoFlg3) { result = new String[]{str2, str3}; } else if (yukoFlg4) { result = new String[]{str2, str4}; } else { result = new String[]{str2, str5}; } } else if (yukoFlg3) { if (yukoFlg4) { result = new String[]{str3, str4}; } else { result = new String[]{str3, str5}; } } else { result = new String[]{str4, str5}; } // 有効行の数が3の場合 } else if (yukoCnt == 3){ if (yukoFlg1) { if (yukoFlg2) { if (yukoFlg3) { result = new String[]{str1, str2, str3}; } else if (yukoFlg4) { result = new String[]{str1, str2, str4}; } else { result = new String[]{str1, str2, str5}; } } else if (yukoFlg3) { if (yukoFlg4) { result = new String[]{str1, str3, str4}; } else { result = new String[]{str1, str3, str5}; } } else { result = new String[]{str1, str4, str5}; } } else if (yukoFlg2) { if (yukoFlg3) { if (yukoFlg4) { result = new String[]{str2, str3, str4}; } else { result = new String[]{str2, str3, str5}; } } } else { result = new String[]{str3, str4, str5}; } // 有効行の数が4の場合 } else if (yukoCnt == 4) { if (!yukoFlg1) { result = new String[]{str2, str3, str4, str5}; } else if (!yukoFlg2) { result = new String[]{str1, str3, str4, str5}; } else if (!yukoFlg3) { result = new String[]{str1, str2, str4, str5}; } else if (!yukoFlg4) { result = new String[]{str1, str2, str3, str5}; } else { result = new String[]{str1, str2, str3, str4}; } // 有効行の数が5の場合 } else { result = new String[]{str1, str2, str3, str4, str5}; } return result; }

    • ベストアンサー
    • Java
  • 文が指定されていませんエラーについて

    public class Stack2 { private static int[] stack = new int[50]; private static int ip = -1; //スタックポインター, -1ならデーターなし public static int push ( int inData ) { // データー格納 if (ip == (stack.length - 1)) { System.out.println(\"スタックがいっぱいです\"); } else { stack[++ip] = inData; } } public static int pop ( ) { // データー取り出し if ( ip < 0 ) { System.out.println (\"スタックにデーターがありません\"); return -2147483648; } else { return stack[ip]; } } } 「Stack2.java:10:return 文が指定されていません」コンバイルエラーになります。 どのようにデバックすれば良いのか教えてください。

  • javaの同期について

    javaの同期について package rwlock; public class App1 extends Thread { static private final int REFER = 0; static private final int UPDATE = 1; static private final int[] conf1 = { REFER, REFER, REFER, UPDATE }; static private final int[] conf2 = { REFER, UPDATE, REFER, REFER }; static private MyObj0 mo = new MyObj0(); // App1 + MyObj0 int id; private int[] conf; public App1(int id, int[] conf) { this.id = id; this.conf = conf; } public void run() { long tStart = Time.current(); for (int i = 0; i < conf.length; i++) { switch(conf[i]) { case REFER: mo.refer(); break; case UPDATE: mo.update(); break; default: assert false : "internal error"; } } if (id == 1) { Time.printElapsed(tStart); } } public static void main(String[] args) { App1 th1 = new App1(1, conf1); App1 th2 = new App1(2, conf2); th1.start(); th2.start(); } } public class MyObj0 { private Object countLock = new Object(); private int count; private void enter() { synchronized(countLock) { count++; } } private void leave() { synchronized(countLock) { count--; } } public MyObj0() { count = 0; } public void refer() { enter(); Time.sleep(300); leave(); } public void update() { enter(); synchronized(countLock) { assert count == 1; } Time.sleep(500); leave(); } において、updateの実行中は、他のスレッドでもupdateもreferも実行されないが、二つのスレッドで同時にreferは実行されうるという条件を満たすにはどうしたらよいでしょうか?updateにsynchronizedをつけてみましたが、referが同時に実行されてしまいました

    • ベストアンサー
    • Java