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

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

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

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

Q&A

5回答

1124閲覧

被らずに整数を表示させる方法知りたいです。

nununu1234

総合スコア0

Java

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

0グッド

0クリップ

投稿2023/03/11 08:42

〇言語
・Java

〇実現したいこと。
・ビンゴ表に表示される乱数が被らないように表示させ、ビンゴ表を完成させたいです。

〇前提
javaでビンゴ表の作成~ビンゴまでの流れを作成しています。

➀B行(1~15), I行(16~30), N行(31~45), G行(46~60), O行(61~75)の数字をランダムに表示させる。
②Enterキーを押すたびに1~75までの数字をランダムに出し、当たればHIT、外れはDeviate
を表示させ、現在のカードを表示。
③リーチ数の表示、ビンゴしたらcongratulationを表示。

〇不明点
・ビンゴ表の作成を、for文、Math.randomを使い、表示させているのですが、
ランダムで出てくる数字が被ってしまう問題点があります。
数字が被らずにビンゴ表に表示をさせる方法が分からないため、教えていただきたいです。

〇作成中のソースコード
import java.util.Scanner;
class Bingo{
public static void main(String[] args){
int[][] bigoCard = new int[5][5];
int a = 15;
int b = 1;
int random = 0;
System.out.println("--------------------------");
System.out.println("| B | I | N | G | O |");
System.out.println("--------------------------");
for (int score1 = 0 ; score1 <= 4 ; score1 ++){
for (int score2 = 0 ; score2 <= 4 ; score2 ++){
random = (int)(Math.random() * a) + b;
bigoCard[score1][score2] = random;
}
b = b + 15;
}
for (int score1 = 0 ; score1 <= 4 ; score1 ++){
for (int score2 = 0 ; score2 <= 4 ; score2 ++){
if(score1 == 2 && score2 == 2){
System.out.print("| ■ ");
}else{
String str = String.format("%2s", bigoCard[score2][score1]);
System.out.print("| " + str + " ");
}
}
System.out.println("|");
System.out.println("--------------------------");
}
Scanner scanner = new Scanner(System.in);
int[] list = new int[75];
int count = 0;
int congratulation = 0;
while(congratulation == 0){
for(int i = 0 ; i < 75 ; i ++){
int random1 = (int)((Math.random() * 75) + 1);
for(int j = 0 ; j < 75 ; j ++){
if(list[i] == random1){
count = count + 1;
}
}
if(count > 0){
i = i - 1;
count = 0;
continue;
}else{
list[i] = random1;
String enter = scanner.nextLine();
System.out.println("出た数字" + list[i]);
int count1 = 1;
for (int score1 = 0 ; score1 <= 4 ; score1 ++){
for (int score2 = 0 ; score2 <= 4 ; score2 ++) {
if(bigoCard[score1][score2] == list[i]){
bigoCard[score1][score2] = 0;
count1 = 0;
}
}
}
if(count1 == 0){
System.out.println("HIT!");
}else if(count1 == 1){
System.out.println("Deviate!");
}
System.out.println("--------------------------");
System.out.println("| B | I | N | G | O |");
System.out.println("--------------------------");
for (int score1 = 0 ; score1 <= 4 ; score1 ++){
for (int score2 = 0 ; score2 <= 4 ; score2 ++){
if(score1 == 2 && score2 == 2){
System.out.print("| ■ ");
}else if(bigoCard[score2][score1] == 0){
System.out.print("| ■ ");
}else{
String str = String.format("%2s", bigoCard[score2][score1]);
System.out.print("| " + str + " ");
}
}
System.out.println("|");
System.out.println("--------------------------");
}
int tate = 5;
int yoko = 5;
int naname1 = 5;
int naname2 = 5;
int reach = 0;
for (int score1 = 0 ; score1 <= 4 ; score1 ++){
for (int score2 = 0 ; score2 <= 4 ; score2 ++){
if(bigoCard[score1][score2] == 0){
tate = tate - 1;
}
}
if(tate == 1){
reach = reach + 1;
}else if(tate == 0){
congratulation = congratulation + 1;
}
tate = 5;
}
for (int score1 = 0 ; score1 <= 4 ; score1 ++){
for (int score2 = 0 ; score2 <= 4 ; score2 ++) {
if(bigoCard[score2][score1] == 0){
yoko = yoko - 1;
}
}
if(yoko == 1){
reach = reach + 1;
}else if(yoko == 0){
congratulation = congratulation + 1;
}
yoko = 5;
}
int score2 = 0;
for (int score1 = 0 ; score1 <= 4 ; score1 ++){
if(bigoCard[score2][score1] == 0){
naname1 = naname1 - 1;
}
if(naname1 == 1){
reach = reach + 1;
}else if(naname1 == 0){
congratulation = congratulation + 1;
}
score2 = score2 + 1;
}
int score1 = 4;
for (int score3 = 4 ; score3 >= 0 ; score3 --){
if(bigoCard[score3][score1] == 0){
naname2 = naname2 - 1;
}
if(naname2 == 1){
reach = reach + 1;
}else if(naname2 == 0){
congratulation = congratulation + 1;
}
score1 = score1 - 1;
}
if(reach >= 1 && congratulation == 0){
System.out.println("リーチ数:" + reach);
}
}
if(congratulation > 0){
System.out.println("congratulation!!");
}
}
}
}
}

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

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

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

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

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

jimbe

2023/03/11 10:37

コードはコードのマークダウンを利用してご提示ください。
jimbe

2023/03/19 04:45

もし解決したのであれば、回答からベストアンサーを選択もしくはご自身で回答を書いてベストアンサーとして、解決済みとしてください。
guest

回答5

0

List に必要な分の数値を用意して、 shuffle メソッドで並び替え、先頭から使います。
→ 配列化しました。

java

1import java.util.Random; 2import java.util.Scanner; 3 4public class Bingo { 5 public static void main(String[] args) { 6 Card card = new Card(); 7 card.print(); 8 9 Scanner scanner = new Scanner(System.in); 10 11 int[] list = IntArrayUtils.generate(75, 1, true); 12 13 for(int number : list) { //先頭から順に取り出す 14 String enter = scanner.nextLine(); 15 System.out.println("出た数字" + number); 16 17 if(card.mark(number)) { 18 System.out.println("HIT!"); 19 } else { 20 System.out.println("Deviate!"); 21 } 22 card.print(); 23 24 if(card.getBingo() > 0) { 25 System.out.println("congratulation!!"); 26 break; 27 } 28 if(card.getReach() > 0) { 29 System.out.println("リーチ数:" + card.getReach()); 30 } 31 } 32 } 33 34 private static class Card { 35 private int[][] numbers = new int[5][5]; 36 private int reach = 0; 37 private int bingo = 0; 38 39 Card() { 40 for(int i=0; i<5; i++) { 41 int[] list = IntArrayUtils.generate(15, i*15+1, true); 42 for(int j=0; j<5; j++) numbers[j][i] = list[j]; //5個だけ使う 43 } 44 numbers[2][2] = 0; 45 } 46 47 int getReach() { 48 return reach; 49 } 50 int getBingo() { 51 return bingo; 52 } 53 54 boolean mark(int number) { 55 for(int i=0; i<5; i++) { 56 for(int j=0; j<5; j++) { 57 if(numbers[i][j] == number) { 58 numbers[i][j] = 0; 59 check(); 60 return true; //HIT 61 } 62 } 63 } 64 return false; //Deviate 65 } 66 67 private void check() { 68 int[] counts = new int[5 + 5 + 1 + 1]; //縦・横・斜めx2 69 for(int i=0; i<5; i++) { 70 for(int j=0; j<5; j++) { 71 if(numbers[i][j] != 0) continue; 72 counts[i] ++; 73 counts[5+j] ++; 74 if(i == 5-j-1) counts[5+5] ++; 75 if(i == j) counts[5+5+1] ++; 76 } 77 } 78 79 reach = 0; 80 bingo = 0; 81 for(int c : counts) { 82 if(c == 4) reach ++; 83 if(c == 5) bingo ++; 84 } 85 } 86 87 void print() { 88 System.out.println("--------------------------"); 89 System.out.println("| B | I | N | G | O |"); 90 System.out.println("--------------------------"); 91 for(int i=0; i<5; i++) { 92 for(int j=0; j<5; j++) { 93 if(numbers[i][j] == 0) { 94 System.out.print("| ■ "); 95 } else { 96 System.out.printf("| %2s ", numbers[i][j]); 97 } 98 } 99 System.out.println("|"); 100 System.out.println("--------------------------"); 101 } 102 } 103 } 104} 105 106class IntArrayUtils { 107 /** origin ~ origin+size-1 の値の入った int 配列を生成する 108 * @param size 配列の大きさ 109 * @param origin 格納する開始値 110 * @param shuffle ランダムに並び替える場合に true 111 * @return 生成した配列 */ 112 static int[] generate(int size, int origin, boolean shuffle) { 113 int[] a = new int[size]; 114 for(int i=0; i<size; i++) a[i] = origin + i; 115 if(shuffle) shuffle(a); 116 return a; 117 } 118 /** array をランダムに並び替える 119 * @param array 対象配列 */ 120 static void shuffle(int[] array) { 121 Random r = new Random(); 122 for(int i=array.length; i>0; i--) { 123 int j = r.nextInt(i); 124 { int t = array[i-1]; array[i-1] = array[j]; array[j] = t; } //[i-1] と [j] を交換 125 } 126 } 127}

投稿2023/03/11 12:05

編集2023/03/12 06:26
jimbe

総合スコア12646

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

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

episteme

2023/03/11 12:08

Collections.shuffle ってあったんだ...
nununu1234

2023/03/12 05:00

ありがとうございます。 とても分かりやすく納得できました。 とても助かります。
nununu1234

2023/03/12 05:13

またArrayListクラスを使われているかと思うのですが、 使わない方法でも被らずに乱数を取り出すことは可能でしょうか?
jimbe

2023/03/12 05:32 編集

配列でやりたいのであれば、 epistene さんのコードをメソッドやクラスにする形でしょうね。
nununu1234

2023/03/12 08:55

配列を使う以上、ArrayListクラスは必要ということですね。 教えていただきありがとうございます。
jimbe

2023/03/13 03:48

>配列を使う以上、ArrayListクラスは必要 いえ、そんな事はありませんが。 List(ArrayList) を使うのは長さを管理しなくて良かったり shuffle が用意されていたりと便利だからで、修正した回答では使っていません。
nununu1234

2023/03/13 13:48

そういうことですn。 教えていただきありがとうございます。 ArrayListも使えるように学習していきます。
guest

0

Java

1public class foo { 2 public static void main(String[] args) { 3 // 75個のハコを用意し 4 int[] values = new int[75]; 5 // それぞれに1~75を入れる 6 for ( int i = 0; i < values.length; ++i ) { 7 values[i] = i+1; 8 } 9 // n = 75, 74, 73 ,.. 1 について 10 for ( int n = values.length; n > 0; --n ) { 11 // n未満の乱数 t を生成し 12 int t = (int)(Math.random() * n) ; 13 // n-1番目と t番目の ハコのナカミを入れ替える 14 int tmp = values[n-1]; 15 values[n-1] = values[t]; 16 values[t] = tmp; 17 } 18 // すると、1~75をかき混ぜた列のできあがり 19 for ( int i = 0; i < values.length; ++i ) { 20 System.out.print(values[i]+" "); 21 } 22 } 23}

投稿2023/03/11 12:00

episteme

総合スコア16614

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

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

nununu1234

2023/03/12 05:15

教えていただきありがとうございます。 1列目(1~15)、2列目(16~30)、3列目(31~45)、4列目(46~60)、5列目(61~75) と列によって変えていきたいのですが、教えていただきました方法で列によって15ずつ変えていくことは可能でしょうか?
episteme

2023/03/12 06:02 編集

ごめん何を問われているのかわかんない。 1~15 のデタラメな並び 16~30 のデタラメな並び ... を作りたいということ? それとも1~75のデタラメな並びを15個ずつに区切りたい?
nununu1234

2023/03/12 06:32

いえ、分かりづらく申し訳ございません。 下記のように、列によって乱数の範囲が決まっている条件があるため、範囲内で乱数を表示させたいです。 ・B列=1~15の範囲の乱数を5列並べる。 ・I列=16~30の範囲の乱数を5列並べる。 ・N列=31~45の範囲の乱数を5列並べる。 ・G列=46~60の範囲の乱数を5列並べる。 ・O列=61~75の範囲の乱数を5列並べる。 ※真ん中は■にする。
episteme

2023/03/12 06:36

与えられた数nに対し (n-1)/15 が 0 なら B列 1 なら I列 ... ではダメなんですか?
episteme

2023/03/12 06:54

public class foo { public static void main(String[] args) { // 75個のハコを用意し int[] values = new int[75]; // それぞれに1~75を入れる for ( int i = 0; i < values.length; ++i ) { values[i] = i+1; } // n = 75, 74, 73 ,.. 1 について for ( int n = values.length; n > 0; --n ) { // n未満の乱数 t を生成し int t = (int)(Math.random() * n) ; // n-1番目と t番目の ハコのナカミを入れ替える int tmp = values[n-1]; values[n-1] = values[t]; values[t] = tmp; } // すると、1~75をかき混ぜた列のできあがり String bingo = "BINGO"; for ( int row = 0; row < 5; ++row ) { int count = 0; for ( int i = 0; i < values.length; ++i ) { if ( values[i] / 15 == row && count < 5 ) { if ( row == 2 && count == 2 ) System.out.print(" ■ "); else System.out.printf("%s%02d ", bingo.charAt(count), values[i]); ++count; } } System.out.println(); } } }
episteme

2023/03/12 08:01

↑ちょっと間違ってますね^^;
nununu1234

2023/03/12 08:57

いえ、教えていただきとても助かります。 教えていただいたことを参考に解決してみます。
guest

0

1から75まで入れた配列をシャッフルして、それを順に取っていく、というのがわかりやすいかと思います。

投稿2023/03/11 08:44

maisumakun

総合スコア145184

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

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

0

すでに「重複を考慮しないプログラム」が出来ていてということであれば、
「重複したら、乱数を再度発生させる」
というのが一番少ない修正でしょうか。

投稿2023/03/11 08:51

otn

総合スコア84555

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

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

nununu1234

2023/03/12 05:20

コメントありがとうございます。 重複を考慮しないプログラムはできていますので、重複したら、乱数を再度発生させるのは、 現時点で一番修正を少なく正せる方法かと思います。 しかし、重複した場合、乱数を再度発生させ、被らなければ乱数を表示させるという、コードの書き方がわからず、 躓いてしまっている状況です...。
episteme

2023/03/12 06:14 編集

1 生成された乱数がメモ(これまでに使った数の集合)に書かれてるかを調べる。 2.1 メモに書かれていないとき、その数をメモに書き足して終了。 2.2 メモに書かれていたら1からやりなおし。 あるいは 1~75 の書かれた紙を用意し、生成された乱数にバッテンつけて終了/すでにバッテンついてたらもう一度。 要は「すでに使った数か否か」が判定できればいいのだから。
guest

0

[ご参考] ビンゴ・カードを生成する(コメントは敢えて付さない)

Java

1public class foo { 2 3 static void swap(int[] array, int i, int j) { 4 int tmp = array[i]; 5 array[i] = array[j]; 6 array[j] = tmp; 7 } 8 9 public static void main(String[] args) { 10 int[][] board = new int[15][15]; 11 12 int[] values = new int[15]; 13 14 for ( int col = 0; col < 5; ++col ) { 15 for ( int i = 0; i < 15; ++i ) { values[i] = col*15 + i + 1; } 16 for ( int row = 0; row < 5; ++row ) { 17 swap(values, row, (int)(Math.random() * (15-row)) + row); 18 board[row][col] = values[row]; 19 } 20 } 21 board[2][2] = -1; 22 for ( int row = 0; row < 5; ++row ) { 23 for ( int col = 0; col < 5; ++col ) { 24 if ( board[row][col] > 0 ) System.out.printf("%02d ", board[row][col]); 25 else System.out.print("** "); 26 } 27 System.out.println(); 28 } 29 } 30}

投稿2023/03/13 03:54

episteme

総合スコア16614

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問