- 締切済み
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); } } } } これがプログラムです。大学の課題でついにつまずきました。エラーの原因と解決方法(プログラム)を教えていただけるとありがたいです。 初めの方の内容なので簡単なレベル(技術)で解決できると助かります。
- 1205tomo
- お礼率16% (3/18)
- その他(学問・教育)
- 回答数1
- ありがとう数1
- みんなの回答 (1)
- 専門家の回答
みんなの回答
- mikkarin
- ベストアンサー率50% (55/110)
質問者様のプログラムにインデント(関数や条件文などのまとまり毎にタブで段落付け)を付けると以下のようになります。 -------------------------- 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); } } } } -------------------------- カッコの開始と終了がmain関数の前でずれてしまっているのがわかるでしょうか?
関連する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の超初心者です。ご教授いただけたら幸いです。
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
- 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
- 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
- *を表示させるプログラムを
//putChars()メソッドを呼ばず(使わず) for文のネストで、できますか import java.util.Scanner; class IsoscelesTriangleRB { //--- 文字cをn個連続表示 ---// static void putChars(char c, int n) { while (n-- > 0) System.out.print(c); } public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.println("右下直角の三角形を表示します。"); System.out.print("段数は:"); int n = stdIn.nextInt(); for (int i = 1; i <= n; i++) { putChars(' ', n - i); // ' 'をn - i個表示 putChars('+', i); // '+'を i 個表示 System.out.println(); } } }
- 締切済み
- Java
- JAVAの演習問題
現在JAVAを独学中なのですが、以下の演習問題でつまずいております。 (解答がない演習問題なので、このサイトにて質問することにしました。) 実行例 1からnまでの和を求めます。 nの値:5 1から5までの和は15です。 (5はキーボードにて入力された値。15はその結果の値。) 自分の解答は以下になるのですが、実行例のようになりません。 ご解答頂けると大変助かります。 import java.util.Scanner; class Ensyuu413 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.println("1からnまでの和を求めます。"); System.out.print("nの値:"); int n = stdIn.nextInt(); for ( int i = 0, j = 0; i <= n; i++, j += i) System.out.println("1から"+n+"までの和は"+j+"です。"); } }
- ベストアンサー
- その他(ソフトウェア)
- 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]); } } }
- 締切済み
- Java
- フローチャートについて
import java.util.Scanner; class Test{ public static void main(String[] args){ Scanner stdIn = new Scanner(System.in); System.out.println("1からnまでの和を求めます。"); System.out.print("nの値:"); int n= stdIn.nextInt(); int sum=0; int i=1; while(i<=n){ sum +=i; i++; } System.out.println("1から"+n+"までの和は"+sum+"です。"); } } 1からnまでの和を求めるプログラムで、この画像はそのフローチャートなのですが、 参考書にはsum+i→iと書かれています。 これってsum+i→sumの間違いですか? また、初期化や代入のフローチャートは 代入したい値を左に書いて右矢印にして右に変数を書くのが一般的なのでしょうか?
- ベストアンサー
- Java
- javaのfor文
nの値の和を求めるのはこれ以外に書き方はありますか? import java.util.*; class SampleGoo{ public static void main(String args[]){ Scanner std = new Scanner(System.in); System.out.print("整数値:");int n = std.nextInt(); int ans = 0; for (int i = 1;i <= n;i++){ ans = ans + i; } System.out.println(Integer.toString(ans)); } }
- 締切済み
- Java
お礼
ありがとうございます。 正しく実行できました。
補足
プログラムは実行できました。このプログラムはx,y,zの乱数の和を当てるというモノです。 正解するとyesかnoを選択します。yesのとき新しい問題(新しい乱数)に答え、noのときwhile文から抜け出して終了。 としたのですが、実行の結果yesのとき同じ問題、noのとき新しい問題となってしまいました。 解決策をお願いします。