• ベストアンサー

選択肢

・ ・ ・ int dealer=0; String line = "Y"; while(line == "Y"){ for(int p = 1; p < 3; p++){ int c=(int)(Math.random()*11); System.out.println(p + "回目に引いたカードは"+ c +"です。"); }//自分のひいたカード表示 System.out.println("まだ引きますか?(Y/N)"); line = reader.readLine(); System.out.println(line); if(line == "y"){ System.out.println("イエス"); }else if(line == "n"){ System.out.println("ノー"); } } 上記のようにline という変数に納入した文字をifに使用したいのですが、yとうっても「ノー」の所へ行ってしまいます。どうしてなのでしょうか?

  • Java
  • 回答数2
  • ありがとう数2

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

  • ベストアンサー
  • liar_adan
  • ベストアンサー率48% (730/1515)
回答No.1

if(line == "y"){ System.out.println("イエス"); }else if(line == "n"){ System.out.println("ノー"); ---- の、==で比較しているところを、 equalsを使ってみたらどうでしょう。 ---- f(line.equals("y")){ System.out.println("イエス"); }else if(line.equals("n")){ System.out.println("ノー"); ---- 「文字列の比較には、==ではなくequals()を使う」 というのは一つの基本です。 それで動かなかったら補足に書き込んでください。

megumi0808
質問者

お礼

動きました! 文字のときは==ではなかったのですね、 ありがとうございました!

その他の回答 (1)

回答No.2

あと while(line == "Y") の部分もね。 それと while(line.equals("Y"))←大文字で判定 if(line.equals("y"))←小文字で判定 だと「イエス」と出力されても whileの条件でfalseになってwhileのループを抜けますよ。 っていうか、(Y/N)の入力を促してるけど、 結局NじゃなくてもY以外を入力しちゃうとループ抜けますね。 さぁ、どうする???

megumi0808
質問者

お礼

プログラムは無事に完成いたしました。 お答えありがとうございました!^^

関連するQ&A

  • javaで課題を出されています

    題名の通りなのですが、javaのプログラミングで課題を出されていて うまく書けません お題としては 1 if分と論理演算を使うこと。 2 forまたはwhileを使用すること 3 メソッドを使用すること。(main以外で) 4 配列を使用すること 以上なのですが、一応昔スロットプログラムを作っていたのでこれをベースに作ろうかなと考えています import java.io.*; public class Slot01{ public static void main(String[] args){ try{ //String line =reader.readLine(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("こんにちは!スロットゲームへようこそ!"); System.out.println("EnterKeyを押して当たりを出してくださいね!"); while(true){ //while文は処理を繰り返すためのものです。 // ▽これよりソースを打ち込んでください //try{ int x=(int)(Math.random()*9)+1; int y=(int)(Math.random()*9)+1; int z=(int)(Math.random()*9)+1; int[]kakuritu; int sum; System.out.print(x); System.out.print(y); System.out.print(z); System.out.println(""); String line =reader.readLine(); //System.out.println(""); if(x==7&&y==7&&z==7){ System.out.println("スーパー大当たり"); }else if(x==y&&y==z){ System.out.println("大当たり"); }else if(x==y||y==z||x==z){ System.out.println("小当たり"); }else{ System.out.println("外れ"); } }//while文の終わり }catch (IOException e){ System.out.println(e); }catch (NumberFormatException e){ System.out.println("正しい形式で表示してください。"); } } } このプログラムなのですが、1と2はこの中に入ってます。考えてみたのですが、このプログラムに当たり確率を表示させるプログラムなら3と4も満たせそうなのですが、いまいちソースが思いつきませんのでご教授お願いします。 文章おかしいところだらけで申し訳ありません。

    • ベストアンサー
    • Java
  • Java言語のプログラムをC言語にする場合

    次の2つのJava言語のプログラムをC言語にしたいのですが、C言語でプログラムを書いたことがありません。C言語にする場合はどう書けばいいのでしょうか? import java.io.*; public class Sort { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("整数値を入力してください"); String line = reader.readLine(); int a = Integer.parseInt(line); String line2 = reader.readLine(); int b = Integer.parseInt(line2); String line3 = reader.readLine(); int c = Integer.parseInt(line3); String line4 = reader.readLine(); int d = Integer.parseInt(line4); int[] data = {a, b, c, d}; for (int i = 0; i< data.length - 1 ; i++) { for (int j = i + 1; j< data.length; j++) { if(data[i] > data[j]) { int e = data[i]; data[i] = data[j]; data[j] = e; } } } System.out.println("昇順に並べ替えると、"); for (int i = 0; i< data.length; i++) { System.out.print(data[i] + " "); } System.out.println("です。"); } catch (IOException e){ System.out.println(e); } catch (NumberFormatException e) { System.out.println("数式の形式が正しくありません。"); } } } import java.io.*; public class Yakusu { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("2つの整数値を入力してください"); System.out.print("整数A : "); String line = reader.readLine(); int a = Integer.parseInt(line); System.out.print("整数B : "); String line2 = reader.readLine(); int b = Integer.parseInt(line2); if(a%b == 0){ System.out.println("BはAの約数です"); } else { System.out.println("BはAの約数ではありません"); } } catch (IOException e){ System.out.println(e); } catch (NumberFormatException e) { System.out.println("数式の形式が正しくありません。"); } } }

  • このプログラミング問題の解答で何が足りないのか教えてください。

    学校から出た問題なんですが、うまくいかなくて悩んでます!ヒントだけでもいいのでください! 問題 整数a,b,c,d(b≠0、d≠0)を読み込み、a/b+c/dを計算し、結果を帯分数として出力せよ。(分数の約分はしなくてよい。) という問題で、僕は (キーボード入力は略) int a,b,c,d,x,y; int i=0 ; x=0; y=0; System.out.println("文字aを入力してください"); line = br.readLine(); a = Integer.parseInt(line); System.out.println("文字bを入力してください"); line = br.readLine(); b = Integer.parseInt(line); while (b==0){ System.out.print("文字bを入力してください"); line = br.readLine(); b = Integer.parseInt(line); } System.out.print("文字cを入力してください"); line = br.readLine(); c = Integer.parseInt(line); System.out.print("文字dを入力してください"); line = br.readLine(); d = Integer.parseInt(line); while (d==0){ System.out.print("文字dを入力してください"); line = br.readLine(); d = Integer.parseInt(line); } x=(a*d+c*b)/(b*d); if(x<1){ System.out.print((a*d+c*b)+"/"+(b*d)); }else{ if( (a*d+c*b)%(b*d)==0 ){ System.out.print(x); }else{ if((a*d+c*b)/(b*d)>1){ for( i=(a*d+c*b); i>=(b*d); i-=(b*d)){ y=i-(b*d); } System.out.print(x+"+"+y+"/"+(b*d)); }}}}} と作ってコンパイルしたのですが、他はちゃんとできたのですが、2,3,4,5と打ったり、5,6,7,8と打つと何も表示されません! 何が原因でしょうか。わかる人はヒントだけでも教えてもらえるとうれしいです!

    • ベストアンサー
    • Java
  • Javaで九九の計算

    今、Javaの勉強をしてる者です。勉強本の中に九九の問題があり、正しく動作することを確認したのですが、 入力された値が数値以外のものだった場合に、再入力させるプログラムに機能アップしたいと欲が出てきました。 現状のプログラムでは、入力値が整数でない場合、NumberFormatException で判定をして次の回に進んでしまってます。 int result = Integer.parseInt(line); の戻り値を判定して、整数でない場合に再入力させれば良いのかと予想したのですが、どう記載すべきかわかりません。 そもそもこの考え方は間違っているのか、別の方法が良いのかご教示いただけませんでしょうか。 --- import java.io.*; public class KuKu4 { public static final int max_question = 10; public static void main(String[] args){ int goodAns = 0; for(int i=0; i < max_question; i++){ boolean check = showQuestion(i+1); if(check){ goodAns++; } } double rate = (goodAns * 100 / max_question) ; System.out.println(""); System.out.println("正解は" + goodAns +"問"); System.out.println("間違いは" + (max_question - goodAns) +"問"); System.out.println("正答率は" + rate +"%"); } public static boolean showQuestion(int question){ int x = (int)(Math.random() * 9) + 1; int y = (int)(Math.random() * 9) + 1; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("[第" + question + "問]" + x + " * " + y + "== ?"); String line = reader.readLine(); int result = Integer.parseInt(line); if(x * y == result){ System.out.println("正しい"); return true; }else{ System.out.println("正しくない"); return false; } }catch(IOException e){ System.out.println(e); }catch(NumberFormatException e){ System.out.println("入力された値が正しくない"); } return false; } }

  • 環境でeval()が利用できません

    3×3の簡易エクセルをjavaで作っているところです。 各セルにキーボードから入力された数値を表示するところまではできました。しかし、Data: と表示するところで、A1+A2などを入力すると、 エラーが出ます。 Row:A Line:1 Data:2 Row:A Line:2 Data:4 と入力しているとき Row:A Line:3 Data:A1+A2 と入力された際、 2 4 6 と表示させたいです。どう改良すればいいでしょうか? for(roop=0;roop<=8;roop++) { System.out.print("Row:"); try { Row_before = br.readLine(); } catch(IOException e) { System.err.println(e); } if(Row_before.equals("A")||Row_before.equals("B")||Row_before.equals("C")) { Row[roop] = Row_before;Row_before=""; } else {Row_before="";} System.out.print("Line:"); try { Line_before = br.readLine(); } catch(IOException e) { System.err.println(e); } if(Line_before.equals("1")||Line_before.equals("2")||Line_before.equals("3")) {Line[roop] = Line_before;Line_before="";} else {Line_before="";} System.out.println("Data:"); //A1に数値を入力 if(Row[roop].equals("A")&&Line[roop].equals("1")) { try { A1_string = br.readLine(); } catch(IOException e) { System.err.println(e); } A1 = Integer.parseInt(A1_string); System.out.println(""+A1_string); } //以下同様の動き if(Row[roop].equals("A")&&Line[roop].equals("2")) {try{A2_string = br.readLine();}catch(IOException e){System.err.println(e);}A2 = Integer.parseInt(A2_string);} if(Row[roop].equals("A")&&Line[roop].equals("3")) {try{A3_string = br.readLine();}catch(IOException e){System.err.println(e);}A3 = Integer.parseInt(A3_string);} if(Row[roop].equals("B")&&Line[roop].equals("1")) {try{B1_string = br.readLine();}catch(IOException e){System.err.println(e);}B1 = Integer.parseInt(B1_string);} if(Row[roop].equals("B")&&Line[roop].equals("2")) {try{B2_string = br.readLine();}catch(IOException e){System.err.println(e);}B2 = Integer.parseInt(B2_string);} if(Row[roop].equals("B")&&Line[roop].equals("3")) {try{B3_string = br.readLine();}catch(IOException e){System.err.println(e);}B3 = Integer.parseInt(B3_string);} if(Row[roop].equals("C")&&Line[roop].equals("1")) {try{C1_string = br.readLine();}catch(IOException e){System.err.println(e);}C1 = Integer.parseInt(C1_string);} if(Row[roop].equals("C")&&Line[roop].equals("2")) {try{C2_string = br.readLine();}catch(IOException e){System.err.println(e);}C2 = Integer.parseInt(C2_string);} if(Row[roop].equals("C")&&Line[roop].equals("3")) {try{C3_string = br.readLine();}catch(IOException e){System.err.println(e);}C3 = Integer.parseInt(C3_string);} System.out.println(""); }

  • Javaでクイズ作成途中もエラー

    あの、以前こちらで質問した者です。→http://okwave.jp/qa/q6482901.html 今、英単語が表示され日本語でそれを答えるクイズを作ろうとしています。 まだまだ作りかけですが、どうしてもコンパイル時にエラーになってしまいます。 どこから直せば良さそうですか?自己研究ということで周りに質問する人がいません。 すみませんがヒントを頂けたらと質問しました。よろしくお願いします。 import java.io.*; public class EnglishToJapaneseQuiz { public int MAX_QUESTION = 5; public int GOODANSWER = 0; public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("Welcome! This quiz helps you study Japanese in English"); System.out.println("'n' normal"); System.out.println("'h' hard"); System.out.println("'e' expert"); System.out.println("'z' exit"); System.out.println("Please select the level"); String line = reader.readLine(); char c = line.charAt(0); switch (c) { case 'n': System.out.println("You selected normal"); for (int i=0; i<MAX_QUESTION; i++) { int x = (int)(Math.random() * 10) + 1; switch (x) { case 1: System.out.println("School"); String Answer = "学校"; BufferedReader responce = new BufferedReader(new InputStreamReader(System.in)); String line = responce.readLine(); If (line.equals(Answer)) { System.out.println("correct"); GOODANSWER = GOODANSWER + 1; } else { System.out.println("incorrect"); } break; case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: } } break; case 'h': System.out.println("You selected hard"); break; case 'e': System.out.println("You selected expert"); break; case 'z': break; default: System.out.println("you must indicate valid character"); break; } } catch (IOException e) { System.out.println(e); System.out.println("invalid value"); } System.out.println("the program was successfully ended"); } }

    • ベストアンサー
    • Java
  • applet

    あの以前こちらで質問したものです。 →http://okwave.jp/qa/q6547731.html 前回、前々回とお世話になってます。あれから少し変化を加えて一応形にはなりました。来月提出ですが急にブラウザ上で表示させるようにしてくれと教授が学科長に言われたみたいなのでそうすることになりました。その予定はなかったのでアプレット参考書はないですが持ってる本に少し載っていました。(サンプルは表示させることはできました。wamp server使ってます)が自分のを表示できません。”エラー、クリックして詳細を確認”とでます。どうすれば良いでしょうか。(制限のため省略&見にくくてすみません) import java.io.*; import java.applet.Applet; public class EnglishToJapaneseQuiz2 { public static int MAX_QUESTION = 5; public static int GOODANSWER = 0; public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("Welcome! This quiz helps you study Japanese in English"); System.out.println("'n' normal"); System.out.println("Please select the level"); String line = reader.readLine(); char c = line.charAt(0); switch (c) { case 'n': System.out.println("You selected normal"); String normal[][]={{"School?","1.学校","2.生徒","3.先生","4.勉強","1"}, {"Human?","1.ゴミ","2.サル","3.ヒト","4.ブタ","3"}}; System.out.println("Choose the best one from multiples"); for (int i=0; i<MAX_QUESTION; i++) { int x = (int)(Math.random() * 10); System.out.println(normal[x][0]); System.out.println(normal[x][1]); System.out.println(normal[x][2]); System.out.println(normal[x][3]); System.out.println(normal[x][4]); BufferedReader responce = new BufferedReader(new InputStreamReader(System.in)); line = responce.readLine(); int input = Integer.parseInt(line); String answer = normal[x][5]; int inputanswer = Integer.parseInt(answer); if(input == inputanswer) { System.out.println("Correct"); GOODANSWER = GOODANSWER + 1; }else if(input <= 0 || input >=5) { System.out.println("Invalid Number"); }else { System.out.println("Incorrect"); } } System.out.println("You got "+GOODANSWER+" out of "+MAX_QUESTION); break; default: System.out.println("you must indicate valid character"); break; } } catch (IOException e) { System.out.println(e); System.out.println("invalid value"); } System.out.println("the program was successfully ended"); } }

    • ベストアンサー
    • Java
  • どこに・・・

    import java.io.*; class janken { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("あなたの手を入力して下さい。(1:グー、2:チョキ、3:パー)?\n"); String str = br.readLine(); int a = Integer.parseInt(str); switch(a){ case 1: System.out.println("あなたの手はグーです。"); break; case 2: System.out.println("あなたの手はチョキです。"); break; case 3: System.out.println("あなたの手はパーです。"); break; default: System.out.println("1~3を入力してください。"); break; } { int ran = ((int)(Math.random()*3)+1); int b=ran; switch(b){ case 1: System.out.println("コンピュータの手はグーです。"); break; case 2: System.out.println("コンピュータの手はチョキです。"); break; case 3: System.out.println("コンピュータの手はパーです。"); break; } int c; c=a-b; if(c == 2){ System.out.println("勝ち"); } else if(c== -1){ System.out.println("勝ち"); } else if(c==0){ System.out.println("あいこ"); } else if(c==1){ System.out.println("負け"); } else { System.out.println("負け"); } } } } いまjavaの勉強をしており、練習をかてね上のようにじゃんけんのプログラムを書きました。今の段階で、一様動くのですが、一回ごとにじゃんけんが終了されてしまいます。そこで繰り返し文のwhile(a != x)を使って数値を入力したら終了するように考えているんですが、whileをどこに入れたらいいのかがわかりません・・・ 自分でいろんなところに入れて試して見たんですが、無限ループになったりして、終了してくれません。どこにwhileを入れたらいいのでしょうか?それてもwhileじゃないほうがいいのでしょうか?アドバイスお願いします。

    • ベストアンサー
    • Java
  • エラーの訂正でアドバイスください。

    javaプログラミング超初心者です import java.io.*; public class ex22b { public static void main(String[] arg) { System.out.print("x: "); int x = (new Integer (in.readLine())). intValue(); System.out.print("y: "); int y = (new Integer (in.readLine())). intValue(); int a; while (y > 0) { a = a + x; System.out.println("kekka = " + a); System.out.println("y = " + y); y = y - 1; } System.out.println("乗算結果は " + a); } } というソースを書いたら、コンパイルの際に シンボルを見つけられません シンボル:変数 in 場所  :ex22bのクラス int x = (new Integer (in.readLine())). intValue();            ^ int y = (new Integer (in.readLine())). intValue();            ^ というエラーが出ました。 どこをどう直したらいいのでしょうか。

    • ベストアンサー
    • Java
  • 至急!java オブジェクト指向

    昨日、質問させていただいた者です。おかげさまで、試合の状況を出力するプログラムが完成しました。あとはこれをオブジェクト指向のプログラムにしたいので、BSO()、Runner()、Score()のメソッド等を別々のクラスに記述して動作させるようにしたのですが、エラーばかりで起動できませんでした。インターフェースや継承などを使っていただいて構わないので、できれば改良のほうをお願いします。 import java.io.*; public class test1 { public static void main(String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int sc=0,bc=0,oc=0,rc1=0,rc2=0,rc3=0,tenA=0,tenB=0,i=0; BSO(sc, bc, oc); Runner(rc1,rc2,rc3); Score(tenA,tenB,i); while(true){ i++; while(true){ System.out.println("ピッチャー、投げた!"); String str=br.readLine(); char hantei=str.charAt(0); if(hantei=='s'){ System.out.println("ストライク!"); sc++; }else if(hantei=='b'){ System.out.println("ボール!"); bc++; }else if(hantei=='o'){ System.out.println("アウト!"); oc++; bc=sc=0; }else if(hantei=='h'){ System.out.println("ヒット!"); bc=sc=0; }else if(hantei=='x'){ System.out.println("ホームラン!"); bc=sc=0; }else{ System.out.println("入力が違います。"); continue; } if(sc==3){ System.out.println("ストライク!バッターアウト!"); oc++; sc = bc =0; }else if(bc==4){ System.out.println("フォアボール!"); sc = bc =0; } if(oc >= 3){ System.out.println("スリーアウト!"); break; } System.out.println("走者は出たか、又は進塁したか?"); String str6=br.readLine(); char kakuninn=str6.charAt(0); if(kakuninn=='y'){ System.out.println("一塁は"); String str2=br.readLine(); char r1=str2.charAt(0); if(r1=='y'){ rc1=1; }else if(r1=='n'){ rc1=0; }else{ System.out.println("入力が違います。"); continue; } System.out.println("二塁は"); String str3=br.readLine(); char r2=str3.charAt(0); if(r2=='y'){ rc2=1; }else if(r2=='n'){ rc2=0; }else{ System.out.println("入力が違います。"); continue; } System.out.println("三塁は"); String str4=br.readLine(); char r3=str4.charAt(0); if(r3=='y'){ rc3=1; }else if(r3=='n'){ rc3=0; }else{ System.out.println("入力が違います。"); continue; } }else{ } System.out.print("得点は? :"); String str7=br.readLine(); int tokuten=Integer.parseInt(str7); if(i%2==1){ tenA=tenA+tokuten; }else{ tenB=tenB+tokuten; } BSO(sc, bc, oc); Runner(rc1,rc2,rc3); Score(tenA,tenB,i); } oc=0; rc1=rc2=rc3=0; BSO(sc, bc, oc); Runner(rc1,rc2,rc3); Score(tenA,tenB,i); if(i==6){ System.out.println("ゲームセット!"); break; }else{ } } Score(tenA,tenB,i); } public static void BSO(int sc, int bc, int oc){ System.out.println("---- BSOカウント ----"); System.out.print("B "); for(int i=1;i<=bc;i++){ System.out.print("〇"); } System.out.println(""); System.out.print("S "); for(int i=1;i<=sc;i++){ System.out.print("〇"); } System.out.println(""); System.out.print("O "); for(int i=1;i<=oc;i++){ System.out.print("〇"); } System.out.println("\n------------------"); } public static void Runner(int rc1,int rc2,int rc3){ System.out.println("***走者の有無***"); if(rc2==1){ System.out.println(" ▲ "); }else if(rc2==0){ System.out.println("  △ "); } if(rc3==1){ System.out.print("▲  "); }else if(rc3==0){ System.out.print("△  "); } if(rc1==1){ System.out.println("▲"); }else if(rc1==0){ System.out.println("△"); } System.out.println("***************"); } public static void Score(int tenA,int tenB,int i){ if(i==0||i==1||i==2){ System.out.print("1回"); }else if(i==3||i==4){ System.out.print("2回"); }else if(i==5||i==6){ System.out.print("3回"); } if(i==0||i%2==1){ System.out.println("表"); }else{ System.out.println("裏"); } System.out.println(tenA+" "+"-"+" "+tenB); } }

    • ベストアンサー
    • Java

専門家に質問してみよう