• ベストアンサー

例外処理

import java.io.*; class janken { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int k=0,m=0,h=0,z=0; while(true){ System.out.print("あなたの手を入力して下さい。(1:グー、2:チョキ、3:パー、0:終了)?\n"); String str = br.readLine(); int a = Integer.parseInt(str); if(a == 0){ break; } z++; 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("あなたの勝ちです。"); k++; } else if(c== -1){ System.out.println("あなたの勝ちです。"); k++; } else if(c==0){ System.out.println("あいこです。"); h++; } else if(c==1){ System.out.println("コンピュータの勝ちです。"); m++; } else { System.out.println("コンピュータの勝ちです。"); m++; } } } System.out.println("じゃんけん終了。"); System.out.println("あなたは"+z+"試合中、"+k+"勝"+m+"敗"+h+"分けです"); } } このようにじゃんけんのプログラムを作りました。 しかし0~3以外の数字が入力されても、じゃんけんの勝敗を勝手に判定してしまいます・・・ try文でやってみたのですがいまいちうまくいきませんでした。 0~3を入力したら入力エラーと表示して、ふたたびユーザーの手を聞くようにしたいです。 どうすればよいかアドバイスお願いします。

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

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

  • ベストアンサー
noname#47975
noname#47975
回答No.4

とりあえず、これで質問者さんの望むとおりの動作はすると思います。 ********************ソースファイル****************************** import java.io.*; class janken { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int k=0,m=0,h=0,z=0; /***********************修正部分*******************************/ String str; int ec; while(true){ ec = 0; do{ if(ec > 0 ){ System.out.println("入力値が不正です。再度入力し直して下さい"); } System.out.print("あなたの手を入力して下さい。(1:グー、2:チョキ、3:パー、0:終了)?\n"); str = br.readLine(); ec++; }while(!(str.equals("1") || str.equals("2") || str.equals("3") || str.equals("0"))); /*************************************************************/ int a = Integer.parseInt(str); if(a == 0){ break; } z++; 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("あなたの勝ちです。"); k++; } else if(c== -1){ System.out.println("あなたの勝ちです。"); k++; } else if(c==0){ System.out.println("あいこです。"); h++; } else if(c==1){ System.out.println("コンピュータの勝ちです。"); m++; } else { System.out.println("コンピュータの勝ちです。"); m++; } } } System.out.println("じゃんけん終了。"); System.out.println("あなたは"+z+"試合中、"+k+"勝"+m+"敗"+h+"分けです"); } }

noraneko66
質問者

お礼

ご回答ありがとうございます。 コンパイルしたところ私の思っていたとおりに動いてくれて、とても感動しました。 こんな方法があったなんて・・・まだまだ勉強不足ですね^^; 本当にありがとうございました。

その他の回答 (3)

回答No.3

横から失礼。 基本的に、1文字だけの変数が悪いんじゃなくて、使い方だと思われます。同じ変数でもループカウンターなどの場合でしたら、あくまでそのブロック内だけで処理が完結する事を条件として、iでもjでもいいと思います。 ただ、質問者さんの場合は、インクリメントなどのロジックの部分だけではなくて、標準出力の所でも同じように使用されていたので、ちょっと見づらいんじゃないかな~、というわけです。(嫌味ったらしく聞こえるようでしたら、ごめんなさい。)

noraneko66
質問者

お礼

いえいえ、おっしゃるとおりです。 プログラムを作る以上誰から見てもわかるような変数がいいにきまってますし、これからは意識して変数をつけたいとおもいます。

  • koko_u_
  • ベストアンサー率18% (459/2509)
回答No.2

私も所詮素人なのです。 メソッドの分け方も「長くなりそうなら分ける」という程度。 変数名も最初に「これは試合数を格納する変数にしよう → gameCount」という程度。 変数名については、巷の入門書でも 1文字のいい加減な変数名が使用されていたりするので、その悪影響か?

  • koko_u_
  • ベストアンサー率18% (459/2509)
回答No.1

0 ~ 3 以外の場合には continue で while ループの先頭に戻ればよいだけ? そして、コンピュータの出し手を決める箇所や、勝敗を判定する箇所をメソッドにして切り出すのが正常なプログラマの作法だと思う。 また、k l m c などの「意味の取れない」変数名も止めましょう。

noraneko66
質問者

補足

やっぱり、自分の手、コンピュータの手、勝敗の判定をメソッドで分けたほうがいいのですか・・・ まだ習いたてでメソッドをどのようにしたらわからないんですが、調べてみます。 変数なんですがまだ試作段階だったので適当にいれてました。 ややこしくてもうしわけないです。

関連するQ&A

  • どこに・・・

    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じゃんけんゲーの質問

    このjavaじゃんけんゲームで 0を押すまでじゃんけんが続いて0押したら終了して、終了と表示して、じゃんけんの勝敗が表示されるように作りたいんですけどどうしたらいいですか?教えてください import java.io.*; class kadai6 { public static void main(String args[]) throws IOException { System.out.println("これは、じゃんけんゲームです。"); System.out.println("あなたの手を入力して下さい。(1:グー、2:チョキ、3:パー、0:終了)さぁどれにしますか?"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); int res =Integer.parseInt(str); switch(res){ case 1: System.out.println("あなたの手はグーです。"); break; case 2: System.out.println("あなたの手はチョキです。"); break; case 3: System.out.println("あなたの手はパーです。"); break; default: System.out.println("あなたの入力した値はエラーです。1~3の値を入力してください。"); System.exit(1); break; } int comp = (int)(Math.random()*3) + 1; switch (comp){ case 1: System.out.println("コンピュータの手はグーです。"); break; case 2: System.out.println("コンピュータの手はチョキです。"); break; case 3: System.out.println("コンピュータの手はパーです。"); break; default: System.out.println ("エラーです。"); break; } switch (res -comp) { case -2: System.out.println("コンピュータの勝ちです。"); break; case -1: System.out.println ("あなたの勝ちです。"); break; case 0: System.out.println ("あいこです。"); break; case 1: System.out.println ("コンピュータの勝ちです。"); break; case 2: System.out.println("あなたの勝ちです。"); break; default: System.out.println ("エラーです。"); break; } } }

  • プログラミング(JAVA)について

    2つほど質問があります。どなたか回答していただける方がいたらお願いします。 1、以下のじゃんけんゲームのプログラムを作成したのですがfor文を用いてコードを短くするにはどうすればいいですか?      class jyanken { public static void main(String[] args) { int x = 0, y = 0; if (args[0].equals("グー")) { x = 0; } else if (args[0].equals("チョキ")) { x = 1; } else if (args[0].equals("パー")) { x = 2; } else { System.out.println("エラー"); } y = (int)(Math.random() * 10.0) % 3; if (x == 0) { if (y == 0) { System.out.println("あいこ"); } else if (y == 1) { System.out.println("勝ち"); } else if (y == 2) { System.out.println("負け"); } } else if (x == 1) { if (y == 1) { System.out.println("あいこ"); } else if (y == 2) { System.out.println("勝ち"); } else if (y == 0) { System.out.println("負け"); } } else if (x == 2) { if (y == 2) { System.out.println("あいこ"); } else if (y == 0) { System.out.println("勝ち"); } else if (y == 1) { System.out.println("負け"); } } } } 2、もう1パターンでじゃんけんプログラムを作成しようと考えているのですが。上手くできないので参考のファイルを掲示してもらえると嬉しいです。 機能としては、for文とif文を使用して。プログラムの起動と同時に、コンピュータがグー、チョキ、パーをランダムに出す(プレーヤに見せない) 次に人がグー、チョキ、パー何を出すかをキーボードで入力して最後にプログラムが出したものとコンピュータがランダムに出したものと比較し、勝負を表示する。といった感じです。

  • グーとグーで「あいこ」と表示されない。

    現在javaを勉強中でして、じゃんけんゲームを作成しているのですが、 出力のところで本来グーとグーで「あいこ」と表示されるはずなんですが、表示されなかったり、「相手側の勝ち」と表示されたりして、本来の表示がうまくできません。ソースコードを見ても間違っているようには見えないのですがどこか間違っていますか? ちなみにNetBeansを使用しております。よろしくお願いします。 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package jyanken1; */ import java.io.*; import java.lang.invoke.MethodHandles; import java.util.Random; import jdk.nashorn.internal.runtime.regexp.joni.EncodingHelper; class Jyanken1 { /** * @param args the command line arguments */ public static void main (String[] args) throws IOException{ // TODO code application logic here String nyuuryoku; int nyuuryokuyou1; int sentouhyou; System.out.println ("じゃんけんゲームを始めます"); System.out.println ("0.グー 1.チョキ 2.パー"); BufferedReader nyuryokuyou = new BufferedReader (new InputStreamReader (System.in)); Random rnd = new Random(); nyuuryoku=nyuryokuyou.readLine (); nyuuryokuyou1=Integer.parseInt (nyuuryoku); while (nyuuryokuyou1 >=3){ System.out.println ("エラーです。もう一度入力しなおしてください"); System.out.println ("0.グー 1.チョキ 2.パー"); nyuuryoku=nyuryokuyou.readLine (); nyuuryokuyou1=Integer.parseInt (nyuuryoku); } switch(nyuuryokuyou1){ case 0: System.out.println ("自分側:グー"); break; case 1: System.out.println ("自分側:チョキ"); break; case 2: System.out.println ("自分側:パー"); break; } int ran = rnd.nextInt(2); switch(ran){ case 0: System.out.println ("cp:グー"); break; case 1: System.out.println ("cp:チョキ"); break; case 2: System.out.println ("cp:パー"); break; } if((nyuuryokuyou1==0&&ran==1)&&(nyuuryokuyou1==1&&ran==2)&&(nyuuryokuyou1==2&&ran==0)){ System.out.println ("自分側の勝ち"); }else if((nyuuryokuyou1==0&&ran==2)&&(nyuuryokuyou1==1&&ran==0)&&(nyuuryokuyou1==2&&ran==1)){ System.out.println ("相手側の勝ち"); }else if ((nyuuryokuyou1==0&&ran==0)&&(nyuuryokuyou1==1&&ran==1)&&(nyuuryokuyou1==2&&ran==2)) { System.out.println ("あいこです。"); } } }

    • ベストアンサー
    • Java
  • C++ scanfで止まらない

    いつもお世話になっております。 最近C++を勉強し始めたばかりで、初心者丸出しの質問になってしまうと思うのですがひとつよろしくお願いします。 本を見ながらじゃんけんゲームを作っていて、サンプルとは別に自分で考えて作ってみたのですが、scanfの挙動がどうも思い通りにいきません。 以下がソースです。 int testJanken() {     int player=0;     int computer;     printf("【じゃんけんゲーム】\n");     printf("じゃんけん・・・(グー:1 チョキ:2 パー:3 終了:9)>");     scanf("%d",&player);    //入力受付     while(player!=9)     {         if(0<player&&player<=3)         {             printf("\nあなたは:");             showTe(player);                          srand(time(NULL));             computer=rand()%3+1;             printf("コンピューターは:");             showTe(computer);             showShouhai(player,computer);         }         else         {             printf("\n----------1~3,または9の値を入れてください!---------\n");         }         printf("\nじゃんけん・・・(グー:1 チョキ:2 パー:3 終了:0)>");         player=0;         scanf("%d",&player);    //入力受付     }     printf("\nバイバイー!!");     return 0; } //勝敗決定表示 int showShouhai(int player, int computer) {     if(player==computer)     {         printf("あいこ!\n");         return 0;     }     else if((player+1==computer)||(player-2==computer))     {         printf("あなたの勝ち!\n");         return 1;     }     else     {         printf("コンピューターの勝ち・・・\n");         return 2;     } } //入力値からグーチョキパーを文字列で表示させる int showTe(int su) {     switch(su)     {         case 1:             printf("グー!\n");             break;         case 2:             printf("チョキ!\n");             break;         case 3:             printf("パー!\n");             break;         default:             printf("???\n");             break;     }     return 0; } 【実現したい仕様はこちら】 1)じゃんけんの手(1,2,3のどれか)を入れれば通常通りじゃんけんが行われ、結果が表示される。 2)9が入力されたら終了 3)1,2,3,9以外が入力されたらメッセージの表示 この3がうまくいきません。 制御できないのは文字を入力した場合なのですが、例えばaと入力するとそれ以降永久ループとなり抜け出せなくなってしまいます・・・ ネットであちこち原因を調べてみたのですが、探し方が悪いのか解決策がわかりません・・・ 今後の勉強のためにも原因と解決策を詳しく教えて頂けると嬉しいです。 よろしくお願いします。

  • C言語について

    C言語のじゃんけんゲームを作成したいのですが、 仕様は 1.利用者とコンピュータによる対戦形式とします。 2.利用者がキーボードから入力した手(グー・チョキ・パー)と、擬似乱数を用いて生成したコンピュータの手を比較し、利用者の勝ち・あいこ・負けの結果を表示しなさい。 3.利用者の入力が不正の場合には再度入力を促すなど、適切な処理をしなさい。 4.これまでの累積勝利数・引き分け数・敗北数をそれぞれ、user_win・user_draw・user_loseの3つの変数(int型)に格納しなさい。 5.連勝中の場合は「5連勝中!」などと表示させるようにしなさい。 6.あいこである限りは自動的にじゃんけんを反復しなさい。 7.勝敗がついた場合、利用者にまだ継続するか質問した上で、じゃんけんを反復させなさい。 8.じゃんけんを終了した場合、これまでの通算成績として、累積勝利数・引き分け数・敗北数のほか、勝利=累積勝利数÷(累積勝利数+累積敗北数)×100、および、最大勝利数を計算して表示しなさい。 という仕様のじゃんけんゲームを作成したいのですが、下記に書いているまでしかできません。誰か教えていただけないでしょうか。分からなくて困っています。 #include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int a,c; srand(time(NULL)); c = rand()%3+1; printf("手を入力してください [1:グー 2:チョキ 3:パー] "); scanf("%d",&a); if(a==1 && c==1) printf("あなたはグーで、私もグーでした。アイコです。\n"); else if(a==1 && c==2) printf("あなたはグーで、私はチョキでした。あなたの勝ちです。\n"); else if(a==1 && c==3) printf("あなたはグーで、私はパーでした。あなたの負けです。\n"); else if(a==2 && c==1) printf("あなたはチョキで、私はグーでした。あなたの負けです。\n"); else if(a==2 && c==2) printf("あなたはチョキで、私もチョキでした。アイコです。\n"); else if(a==2 && c==3) printf("あなたはチョキで、私はパーでした。あなたの勝ちです。\n"); else if(a==3 && c==1) printf("あなたはパーで、私はグーでした。あなたの勝ちです。\n"); else if(a==3 && c==2) printf("あなたはパーで、私はチョキでした。あなたの負けです。\n"); else if(a==3 && c==3) printf("あなたはパーで、私もパーでした。アイコです。\n"); else printf("正しい手を入れてください。\n"); return 0; }

  • 何かおかしい?

    ソース何かおかしいですか? #include <stdio.h> #include <time.h> #include <stblib.h> //ジャンケンゲーム実行 (繰り返しなし) main () { int player = 0, computer; //乱数の種をまく srand(time(NULL)); printf("【ジャンケンゲーム】\n"); //プレイヤーの入力 printf("ジャンケン・・・(グー:1チョキ:2パー:3を入力) > "); scanf("%d", &player); //コンピュウターの手の入力 computer = rand()%3 + 1; printf("コンピュータは"); if(computer == 1) {printf("グー"); } else if(computer == 2) { printf("チョキ");} else if(computer == 3) { printf("パー");} printf("! "); //勝敗の判断と結果表示 if(computer == player){ printf("あいこ\n"); } else if(player ==1 && computer ==2){ printf("プレイヤーの勝ち\n"); } else if(player ==2 && computer ==3){ printf("プレイヤーの勝ち\n"); } else if(player ==3 && computer ==1){ printf("プレイヤーの勝ち\n"); } else { printf("コンピュウターの勝ち\n"); } return 0; }

  • プログラミング(じゃんけんゲーム)

    C言語をしています。 そこでわからない所がありのですが、解説お願いします。 作りたいのは、乱数を使ったジャンケン5回戦で、 一番最後に、何勝何敗何引き分けかを表示させ、 2人のうちどちらが勝ったかです。 最後の何勝何敗何引き分けかを どう書いたらいいかがわかりません。 関数のひきわたしみたいな感じでするというのはわかります。 できたところまでのプログラムが、以下のようなコードです。 #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int a,c,i; printf("じゃんけんをしましょう。 (グー=1 チョキ=2 パー=3)\n"); for(i=i; i<=5; i++) { srand(time(NULL)); c = rand()%3+1; scanf("%d",&a); if(a==1 && c==1) printf("私もグー=1なので、あいこです。\n"); else if(a==1 && c==2) printf("私はチョキ=2なので、あなたの勝ちです。\n"); else if(a==1 && c==3) printf("私はパー=3なので、あなたの負けです。\n"); else if(a==2 && c==1) printf("私はグー=1なので、あなたの負けです。\n"); else if(a==2 && c==2) printf("私もチョキ=2なので、あいこです。\n"); else if(a==2 && c==3) printf("私はパー=3なので、あなたの勝ちです。\n"); else if(a==3 && c==1) printf("私はグー=1なので、あなたの勝ちです。\n"); else if(a==3 && c==2) printf("私はチョキ=2なので、あなたの負けです。\n"); else if(a==3 && c==3) printf("私もパー=3なので、あいこです。\n"); else printf("正しい手を入れてください。\n"); } return 0; }

  • ジャンケンプログラム作ったんですが動作しません。

    Borland C++とVisualC++の両方でEXEまで出来るのですが、起動させてグーチョキパーの手の選択をしてリターンキーを押すと、エラーウィンドが出てしまいます。 いろいろ考えて見たのですが理由が分かりません。 どうか、バグの原因を教えてください。 #include <stdio.h> #include <stdlib.h> #include <time.h> void show_title(void); void match(void); void comp_match(void); void judge(void); void p_memory(void); int playerhand=1; //プレイヤーの手 int computerhand=0;//コンピューターの手 int win=0;//勝利数 int lost=0;//負け数 int draw=0;//引き分け数 int main(void) { while(playerhand!=0) { show_title(); match(); comp_match(); judge(); p_memory(); } return 0; } void show_title(void) { printf("ジャンケンゲームver0.1\n"); printf("製作 ForceFeed 2009/4.13\n"); } void judge(void) { if(playerhand==computerhand){ printf("引き分けですね"); draw++;//引き分けのカウント }else if(playerhand==1 && computerhand==2){ printf("あなたの勝ちです\n"); win++;//勝ちのカウント }else if(playerhand==2 && computerhand==3){ printf("あなたの勝ちです\n"); win++;//勝ちのカウント }else if(playerhand==3 && computerhand==1){ printf("あなたの勝ちです\n"); win++;//勝ちのカウント }else{ printf("あなたの負けです\n"); lost++;//負けのカウント } } void match(void) { printf("1:グー 2:チョキ  3:パー 0:終了 半角数字で入力してください>"); scanf("%d",playerhand); switch(playerhand) { case 1: printf("あなたの手 :グー\n"); break; case 2: printf("あなたの手 :チョキ\n"); break; case 3: printf("あなたの手 :パー\n"); break; case 0: printf("終了します。\n"); exit(0); default: printf("0、1,2,3以外の入力がありました"); break; } } void comp_match(void)//コンピューターの手 { srand((unsigned)time(NULL));//乱数の種 computerhand=rand()%3+1; switch(computerhand) { case 1: printf("コンピューターの手 :グー\n"); break; case 2: printf("コンピューターの手 :チョキ\n"); break; case 3: printf("コンピューターの手 :パー\n"); break; default: printf("1,2,3以外の入力がありました\n"); break; } } void p_memory(void) //記録表示 { printf("勝ち数 %d\n",win); printf("勝ち数 %d\n",lost); printf("勝ち数 %d\n",draw); }

  • JSP/Servletのパラメータの受け渡しが文字化けしてうまくいきません。

    JSP・Servlet・Beanがうまくいかなくて困っています。開発環境はWindowsXP JDK5とEclipse3.2とTomcat5.5を使って作っています。 じゃんけんをするゲームを作ったのですが、コンピュータの手は文字列も画像もきちんと出るのですが、自分の手が文字列は文字化けして画像も正しく表示されません。 JSPのcharsetやServletのrequest.setCharacterEncoding()メソッドなど文字化け等で考えられることはいろいろ調べてやってみたつもりなのですがどうもうまくいきません。コンソールに値を出力するようにしてデバックもやってみてはいるのですが、同様に文字化けした値が入ってしまいます。どなたかご教授下さい。以下にソースを書きます。 <!--JankenStart.jsp--> <%@page language="java" import="java.lang.*, java.util.*" contentType="text/html; charset=Shift_JIS"%> <html> <head><title>じゃんけんゲーム</title></head> <body> <h2>このゲームはコンピュータとじゃんけんをします!<br> コンピュータに勝てるかな?</h2> <form action="../JankenAction"> あなたの手: <input type="radio" name="te1" value="グー">グー<img src="../img/hand4-1-3.gif"> <input type="radio" name="te1" value="チョキ">チョキ<img src="../img/hand4-1-1.gif"> <input type="radio" name="te1" value="パー">パー<img src="../img/hand4-1-2.gif"><p> <center><input type="submit" value="勝負"></center> </body> //JankenAction.java package servlets; import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import beans.JankenBean; public class JankenAction extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{  request.setCharacterEncoding("Windows-31J");  response.setCharacterEncoding("Windows-31J");   String te=request.getParameter("te1");   JankenBean jankenBean=new JankenBean();   jankenBean.setJibun(te);   jankenBean.setComputer();   jankenBean.setHantei();   request.setAttribute("hoge", jankenBean);   RequestDispatcher rDispatcher=request.getRequestDispatcher("JSP/JankenKekka.jsp");   rDispatcher.forward(request, response); } } //JankenBean.java package beans; public class JankenBean { private String computer=null;private String jibun=null; private String hantei=null; private String jgazou=null; private String cgazou=null; public String getComputer(){ return computer; } public String getCgazou(){ return cgazou; } public void setComputer(){ int random =(int)(Math.random()*3); if(random==0){ System.out.println("グー"); computer="グー"; cgazou="img/hand4-1-3.gif"; } else if(random==1){ System.out.println("チョキ"); computer="チョキ"; cgazou="img/hand4-1-1.gif"; }else{ System.out.println("パー"); computer="パー"; cgazou="img/hand4-1-2.gif"; } System.out.println("computer"+computer); } public String getJibun(){ return jibun; } public String getJgazou(){ return jgazou; } public void setJibun(String te){ jibun=te; if(jibun.equals("グー")){ jgazou="img/hand4-1-3.gif"; } else if(jibun.equals("チョキ")){ jgazou="img/hand4-1-1.gif"; }else{ jgazou="img/hand4-1-2.gif"; } System.out.println("jibun"+jibun); } public String getHantei(){ return hantei; } public void setHantei(){ String hantei2=""; if( (computer.equals("グー"))&&(jibun.equals("チョキ"))||(computer.equals("チョキ"))&&(jibun.equals("パー"))||(computer.equals("パー"))&&(jibun.equals("グー")) ){ System.out.println("コンピュータの勝ち"); hantei2="コンピュータの勝ち"; }   else if( (computer.equals("グー"))&&(jibun.equals("パー"))     ||(computer.equals("チョキ"))&&(jibun.equals("グー"))     ||(computer.equals("パー"))&&(jibun.equals("チョキ")) ){ System.out.println("あなたの勝ち"); hantei2="あなたの勝ち"; }else{ System.out.println("あいこ"); hantei2="あいこ"; } hantei=hantei2; } }

専門家に質問してみよう