Javaの数当てゲームを作成する

このQ&Aのポイント
  • javaの超初心者が、2桁の整数値を当てる数当てゲームを作成したいという質問です。
  • 質問者はJavaの勉強をしているが、演習問題に答えがなく困っている状況です。
  • 質問者が試しに作成したコードがあり、そこでRandomクラスやScannerクラスを使用していることが分かります。
回答を見る
  • ベストアンサー

javaの超初心者です。ご教授いただけたら幸いです。

javaの超初心者です。ご教授いただけたら幸いです。 ある書籍を元にやっているのですが、演習に答えがなく、わからないためその問題をお願い致します。 2桁の整数値(10~99)を当てさせる数当てゲームを作成せよ。という問題です。 下記は少しやってみました。 import java.util.Random; import java.util.Scanner; class Kazuate99 { public static void main(String[] args) { Random rand = new Random(); Scanner stdIn = new Scanner(System.in);    int no = rand.nextInt(); ← ここがたぶん違う System.out.println("数当てゲーム開始!!"); System.out.println("10~99の数を当てて下さい。"); int x; // プレーヤが入力した数 do { System.out.print("いくつかな : "); x = stdIn.nextInt(); if (x > no) System.out.println("もっと小さな数だよ。"); else if (x < no) System.out.println("もっと大きな数だよ。"); } while (x != no ); System.out.println("正解です。"); } } 宜しくお願い致します。

  • Java
  • 回答数1
  • ありがとう数5

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

大体あってますね 参考URLを見て下さい nextIntは二つあります。 あなたはどちらを使っていますか?

参考URL:
http://java.sun.com/j2se/1.4/ja/docs/ja/api/java/util/Random.html

関連するQ&A

  • java eclipse do-while文

    計算問題のプログラムです。No…0を選択したときにwhile文から抜け出したいです。 抜け出せない原因と解決方法(プログラム)を教えていただけるとありがたいです。 package lesson5; import java.util.Random; import java.util.Scanner; public class MentalArithmetic { static Scanner stdIn = new Scanner(System.in); static boolean confirmRetry(){ int cont; do{ System.out.print("もう一度?<Yes・・・1/No・・・0> : "); cont = stdIn.nextInt(); }while (cont != 0 && cont != 1); return cont ==1; } static void mondai1(){ Random rand = new Random(); do{ int p = rand.nextInt(900) + 100; int q = rand.nextInt(900) + 100; int r = rand.nextInt(900) + 100; while(true){ System.out.println(p + "+" + q + "+" + r + " = " ); int k = stdIn.nextInt(); // if(k == p + q + r) // break; System.out.println("違いますよ!!"); } }while (confirmRetry()); } public static void main(String[] args) { System.out.println("暗算力トレーニング!!"); while (true) { mondai1(); } } }

  • java Eclipse 初心者 メソッド

     次のようなプログラムを書きました。”変数 mainにvoidは無効な型です。”とエラーが発生しました。 package lesson5; import java.util.Random; import java.util.Scanner; public class MentalArithmetic { static Scanner stdIn = new Scanner(System.in); static boolean confirmRetry(){ int cont; do{ System.out.print("もう一度?<Yes・・・1/No・・・0> : "); cont = stdIn.nextInt(); }while (cont != 0 && cont != 1); return cont ==1; } static int mondaix(){ Random rand = new Random(); int a = rand.nextInt(900) + 100; // return a; } static void mondai1( int p, int q, int r){ do{ while(true){ System.out.println(p + "+" + q + "+" + r + " = " ); int k = stdIn.nextInt(); // if(k == p + q + r) // break; System.out.println("違いますよ!!"); } }while (confirmRetry()); public static void main(String[] args) { System.out.println("暗算力トレーニング!!"); while (true) { int x = mondaix(); int y = mondaix(); int z = mondaix(); mondai1( x, y, z); } } } }  これがプログラムです。大学の課題でついにつまずきました。エラーの原因と解決方法(プログラム)を教えていただけるとありがたいです。  初めの方の内容なので簡単なレベル(技術)で解決できると助かります。

  • javaのプログラム

    int型の配列の各要素に1~10の乱数を代入し、各要素の値を縦向きの*のグラフで表示するプログラムを作っているのですが、結果がランダムででるので、自分の書いたプログラムが正しいのかわかりません。ソースを載せますので合っているのか間違っているか教えて下さい。もし間違っているならどこが間違いなのか教えていただけると嬉しいです。よろしくお願いします。 ●ソース import java.util.Random; import java.util.Scanner; class Graph { public static void main(String[] args){ Random rand = new Random(); Scanner stdIn = new Scanner(System.in); System.out.print("要素数:"); int n = stdIn.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = 1 + rand.nextInt(10); for (int i = 1; i <= 10; i++){ for (int j = 0; j < n; j++) if (a[j] <= i) System.out.print("* "); else System.out.print(" "); System.out.println(); } } } ●実行例 要素数:12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    • ベストアンサー
    • Java
  • JAVAで配列を使って * を縦向きのグラフで表示したいです。

    JAVAの勉強をしています。 このプログラムは配列に乱数を生成して * を 横向きにするプログラムです。 練習問題で、以下のプログラムを書き換えて、* を縦向きのグラフで表示する問題なのですが、解く方法がわかりません。 どなたか答えもしくはヒントを下さい。 よろしくお願いします。 import java.util.Random; import java.util.Scanner; public class Test06_04 { public static void main (String[] args) { Random rand = new Random(); Scanner stdIn = new Scanner(System.in); System.out.print("要素数 : "); int n = stdIn.nextInt(); //要素数を読み込む int[] a = new int[n]; //配列を生成 for (int i = 0; i < n; i++) { a[i] = 1+ rand.nextInt(10); } for (int i = 0; i < n; i++) { System.out.print("a[" + i + "] : "); for (int j = 0; j < a[i]; j++) System.out.print('*'); System.out.println(); } } }

    • ベストアンサー
    • Java
  • 要素数が10の配列で、乱数0~9の値が重複しないようにする方法がわからなくて困っています。

    JAVAの練習問題でわからなくて困っています 以下は線形探索のプログラムで、このプログラムを改良して、 要素数が10の配列で、乱数0~9の値が重複しないようにする方法がわからなくて困っています。 以下のような簡単なプログラムでできる方法で行いたいです。 どなたか答えまたはヒントを下さい、お願いします。 ------------------------------------------------------------ import java.util.Random; import java.util.Scanner; public static void main (String[] args) { Random rand = new Random(); Scanner stdIn = new Scanner(System.in); final int n = 10; //要素数 int[] a = new int[n]; //配列を宣言 for (int j = 0; j < n;) a[j] = rand.nextInt(10); System.out.print("配列aの全要素の値\n{ "); for (int j = 0; j < n; j++) System.out.print(a[j] + " "); System.out.println("}"); System.out.print("探す数値 : "); int key = stdIn.nextInt(); int i; for (i = 0; i < n; i++) if (a[i] == key) break; if (i < n) //探索成功 System.out.println("それはa[" + i + "]にあります。"); else //探索失敗 System.out.println("それはありません。"); } }

  • ランダムで月を生成し、その月を英語で入力する

    import java.util.Scanner; import java.util.Random; class test8{ public static void main(String[] args){ Scanner stdIn=new Scanner(System.in); Random rand =new Random(); String[] s1={"January","February","March","April","May","June","July","August","September","October","November","December"}; System.out.println("英語の月名を入力してください。\nなお、先頭は大文字で、2文字目以降は小文字とします。"); int n,t; do{ n=rand.nextInt(12); //nは0~11の乱数 boolean y=false; while(y==false){ System.out.print(n+1+"月:"); String s2=stdIn.next(); if(s1[n].equals(s2)){ y=true; }else{ System.out.println("違います。"); } } System.out.print("正解です。もう一度? 1…Yes, 2…No:"); t=stdIn.nextInt(); }while(t==1); System.out.println("終了です。"); } } これは正しいプログラムなのですが、 String s2=stdIn.next(); の next() の部分を nextLine() に変えると、 [実行例] 英語の月日を入力してください。 なお、先頭は大文字で、2文字目以降は小文字とします。 11月:November 正解です。もう一度? 1…Yes, 2…No: 1 2月:違います 2月: という風に、なぜか 2月:違います という一文が無駄に表示されてしまいます。 なぜなんでしょうか。。

    • ベストアンサー
    • Java
  • Randomクラスを使い同じ値をもつことがないように

    するには? サンプルコードをどう改良したらいいですか 例えば{1,3,5,5,3,2}の連続した5とならないようにするには? import java.util.*; class Sample6_9{ public static void main(String args[]){ Scanner std = new Scanner(System.in); Random rand = new Random(); System.out.print("要素数:"); int n = std.nextInt(); int[]a = new int[n]; for(int i=0;i<n;i++){ a[i] = 1+rand.nextInt(10); System.out.println("a["+i+"]="+a[i]); } } }

  • 乱数を発生させて、それを配列して小さい順に並べる

    乱数の発生のさせ方は、質問集で見て import java.util.Random; class ransuu { public static void main(String args[]) { Random rand = new Random(); for ( int i = 0; i <10; i++){ int x = rand.nextInt(9); System.out.println(x); } } } を用いて表示できたのですが、そのあとの配列がよく分かりませんのでぜひ教えてください。ちなみに私の持っているテキストには任意の数(22、80、57、60、50など)を並べ替えるというものはあるのですが、今回のような乱数+配列の例が載っていないのでよく分かりませんでした。

    • ベストアンサー
    • Java
  • java 入力命令

    以下うまくどうさしません、教えてください。 public class Main { public static void main(String[] args){ System.out.println("名前"); String name = new java.util.Scanner(System.in) .nextLine(); System.out.println("年齢"); int age = new java.util.Scanner(System.in) .nextInt(); System.out.println (age + name); } }

  • Javaの初心者です

    Javaの勉強をしています。 以下のコードを作成しました。 import java.util.*; public class Main { public static void main(String[] args) { int flg = 0; Scanner sc = new Scanner(System.in); String str = sc.next(); String data = sc.next(); if (data == str) { flg = 1; } if (flg == 1) { System.out.println("YES"); } else { System.out.println("NO"); } } } 標準入力に A A を入力して実行しました。 YESと表示されるのを期待していたのですが、NOと表示されます。 何が悪いのでしょうか?

    • ベストアンサー
    • Java

専門家に質問してみよう