質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

7576閲覧

エラーで配列が要求されましたが、intが見つかりましたと出ました。どこが違うか教えてください。

takunii925

総合スコア11

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2016/10/26 18:01

class CountFills31b {
public static void main(String args[]) {

int c[][] = { {0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,1,0,0}, {0,0,1,1,1,1,0,1,0,0}, {0,0,1,0,0,1,0,1,0,0}, {0,0,1,1,1,1,0,1,0,0}, {0,0,0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,1,1,0,0}, {0,0,0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0,0,0} }; int xZeroCount[] = new int[10]; int yZeroCount[] = new int[10]; int xNextOneCount[] = new int[10]; int yNextOneCount[] = new int[10]; int xNextZeroCount[] = new int[10]; int yNextZeroCount[] = new int[10]; int xZeroOneCount[] = new int[10]; int yZeroOneCount[] = new int[10]; int ZeroOrOne; int i; int x,y;

for( y = 0; y < 10; y++ ) {
xZeroCount[y] = 0;
x = 0;
while( x < 10 && c[y][x] == 0 ) {
xZeroCount[y] = xZeroCount[y] + 1;
x++;
}

xNextOneCount[y] = 0; while( x < 10 && c[y][x] == 1 ) { xNextOneCount[y] = xNextOneCount[y] + 1; x++; } xNextZeroCount[y] = 0; while( x < 10 && c[y][x] == 0 ) { xNextZeroCount[y] = xNextZeroCount[y] + 1; x++; }

}
for( y = 0; y < 10; y++) {
x = 0;
i = 0;

ZeroOrOne = 1;
while( i <= 10 ) {
xZeroOneCount[y][i] = 0;
if( ZeroOrOne == 0 ) {
ZeroOrOne = 1;
} else {
ZeroOrOne = 0;
}
while( x < 10 && c[y][x] == ZeroOrOne ) {
xZeroOneCount[y][i] = xZeroOneCount[y][i] + 1;
x++;
}
i++;
}
}

for( x = 0; x < 10; x++ ) { yZeroCount[x] = 0; y = 0; while( y < 10 && c[y][x] == 0 ) { yZeroCount[x] = yZeroCount[x] + 1; y++; }

yNextOneCount[x] = 0;
while( y < 10 && c[y][x] == 1 ) {
yNextOneCount[x] = yNextOneCount[x] + 1;
y++;
}
yNextZeroCount[x] = 0;
while( y < 10 && c[y][x] == 0 ) {
yNextZeroCount[x] = yNextZeroCount[x] + 1;
y++;
}

yZeroOneCount[x] = 0;
while( y < 10 && c[y][x] == 1 ) {
yZeroOneCount[x] = yZeroOneCount[x] + 1;
y++;
}
}

for( y = 0; y < 10; y++ ) { for( x = 0; x < 10; x++ ) { System.out.print("" + c[y][x] + " "); } System.out.print(" || " + xZeroCount[y]); System.out.print("," + xNextOneCount[y]);

System.out.print("," + xNextZeroCount[y]);
System.out.print("," + xZeroOneCount[y]);
System.out.print("\n");
}
System.out.print("-------------------------\n");
for( x = 0; x < 10; x++ ) {
System.out.print("" + yZeroCount[x] + " ");
}
System.out.print("\n");
for( x = 0; x < 10; x++ ) {
System.out.print("" + yNextOneCount[x] + " ");
}
System.out.print("\n");
for( x = 0; x < 10; x++ ) {
System.out.print("" + yNextZeroCount[x] + " ");
}
System.out.print("\n");
for( x = 0; x < 10; x++ ) {
System.out.print("" + yZeroOneCount[x] + " ");
}
System.out.print("\n");
}
}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

MasahikoHirata

2016/10/26 19:20

実際のエラーメッセージを添付して頂けると幸いです、
guest

回答1

0

ベストアンサー

java

1xZeroOneCount[y][i] = 0;

「朝飯前」という感じの、簡単なエラーです。
50行あたりの上記が、二次元配列になってますよね?


java

1int xZeroOneCount[] = new int[10];

java

1int xZeroOneCount[][] = new int[10][10];

だから、冒頭での配列の宣言時にも、二次元にしておく必要があります。


java

1while( i <= 10 ) {

java

1while( i < 10 ) {

また、やはり50行あたりのループ(一番最初の引用コードのすぐ上)では、
配列宣言で確保する要素数を増やすか、上のようにループ回数を変更しないと、
ゼロから始まるため、インデックスが配列の上限を超え、エラーになります。

これは、非常~によくありがちなミスなんで、ループを書くときに気をつけましょう。
なお、エラーを取った後の挙動が、仕様通りかどうかまでは回答者側には不明です。

投稿2016/10/26 21:10

編集2016/10/26 21:15
LLman

総合スコア5592

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問