java初心者です。三勝で終了するグリコゲームを作ってみました。最終結果としてポイントで勝敗が決まります。それでなのですがメソッドなどを別のクラスに分けてメインメソッドをすっきりさせたいのですが自分なりに試行錯誤してみたとこをうまくいきません。今後もっと複雑なコードを書くとなると必要であると感じ質問させていただきました。心優しいどなたかご教授いただけないでしょうか。
package mygame; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random; import java.util.Scanner; public class RspGame { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] rsp = {"Rock", "Scissors", "Paper"}; System.out.print("Please enter your Name:"); String playerName = reader.readLine(); String cpuName = "Computer"; int points1 = 0; int points2 = 0; int win = 0; int lose = 0; String result = ""; // save the result while (win < 3 && lose < 3) { int user = getUser(); int pc = getPc(); if(user==0 && pc==1) { System.out.println(playerName + ":" + rsp[user] + " || " + cpuName + ":"+ rsp[pc]); System.out.println("Win"); win++; points1 += 3; } else if(user==1 && pc==2) { System.out.println(playerName + ":" + rsp[user] + " || " + cpuName + ":"+ rsp[pc]); System.out.println("Win"); win++; points1 += 6; } else if(user==2 && pc==0) { System.out.println(playerName + ":" + rsp[user] + " || " + cpuName + ":"+ rsp[pc]); System.out.println("Win"); win++; points1 += 6; } else if(user==0 && pc==2) { System.out.println(playerName + ":" + rsp[user] + " || " + cpuName + ":"+ rsp[pc]); System.out.println("Lose"); lose++; points2 += 6; } else if(user==1 && pc==0) { System.out.println(playerName + ":" + rsp[user] + " || " + cpuName + ":"+ rsp[pc]); System.out.println("Lose"); lose++; points2 += 3; } else if(user==2 && pc==1) { System.out.println(playerName + ":" + rsp[user] + " || " + cpuName + ":"+ rsp[pc]); System.out.println("Lose"); lose++; points2 += 6; } else if( (user==0 && pc==0) || (user==1 && pc==1) || (user==2 && pc==2) ) { System.out.println(playerName + ":" + rsp[user] + " || " + cpuName + ":"+ rsp[pc]); System.out.println("Drew"); } System.out.println("<RESULT> " + "WIN:" + win + " LOSE:" + lose + " <SCORE> " + playerName + ":" + points1 + " || " + cpuName + ":" + points2 ); } System.out.println("Finish!!!"); System.out.println("Let's Judge.........."); if( points1 < points2 ) { System.out.println("You Lose..." + " <SCORE> " + playerName + ":" + points1 + " || " + cpuName + ":" + points2 ); } else if( points2 < points1) { System.out.println("Congratulations!! You Win!!" + " <SCORE> " + playerName + ":" + points1 + " || " + cpuName + ":" + points2 ); } else if( points1 == points2 ) { System.out.println("Wow... You Drew..." + " <SCORE> " + playerName + ":" + points1 + " || " + cpuName + ":" + points2 ); } } public static int getUser() { Scanner stdin = new Scanner(System.in); while(true) { //outpu massages System.out.println("\nPlease enter your choice"); System.out.print("(ROCK:0,SCISSORS:1、PAPER:2) --> "); if(stdin.hasNextInt()) { int number = stdin.nextInt(); if(number<=-1 || number >=3) { System.out.println("【ERROR】You can choose only 0, 1 or 2"); } else {return number;} } else {System.out.println("【ERROR】You can enter only the number 0, 1 or 2"); stdin.next(); } } } public static int getPc() { // ready for judge Random rand = new Random(); // get a value random return rand.nextInt(3); } } コード
回答4件
あなたの回答
tips
プレビュー