-
-
Save sunset0916/fdfacb6e3a2a5f78b0d2760c41e785ca to your computer and use it in GitHub Desktop.
計算をして遊ぶゲームのJava版 負数は式の先頭のみ対応、計算順序は加減乗除に関係なく先頭から、除算の結果は小数点以下切り捨ての手抜き仕様
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Random; | |
| import java.util.Scanner; | |
| public class NumberGame { | |
| //定数と使用可能な数値リストの宣言 | |
| final static int NUMBER_OF_START = 5; | |
| final static String TARGET_NUMBER = "10"; | |
| static ArrayList<String> availableNumbers = new ArrayList<>(); | |
| public static void main(String[] args) { | |
| //変数とScannerの宣言 | |
| boolean numberMatch = false; | |
| Scanner scanner = new Scanner(System.in); | |
| System.out.println("四則演算をして最終的に" + TARGET_NUMBER + "を作ろう"); | |
| //使用可能な数値リストの初期化 | |
| for(int i = 0; i < NUMBER_OF_START; i++) { | |
| Random random = new Random(); | |
| availableNumbers.add(String.valueOf(random.nextInt(10))); | |
| } | |
| //ゲーム本編 | |
| while(availableNumbers.size() > 1) { | |
| System.out.println("今回使える数字は以下のとおりです"); | |
| //使用可能な数値を出力 | |
| for(int i = 0; i < availableNumbers.size(); i++) { | |
| System.out.println("・" + availableNumbers.get(i)); | |
| } | |
| //入力の受付 | |
| System.out.print("式を入力してください: "); | |
| String inputText = scanner.nextLine(); | |
| //正常な式かどうかの判定 | |
| if(inputText.matches("^\\-?(\\d+[-+*/])+\\d+$")){ | |
| //式の文字を要素ごとに分割して配列に格納 | |
| String[] split = inputText.split("(?<=[-+*/])|(?=[-+*/])"); | |
| //先頭が負の数だった場合の処理 | |
| if(split[0].equals("-")) { | |
| split[0] = split[0] + split[1]; | |
| for(int i = 1; i < split.length - 1; i++) { | |
| split[i] = split[i+1]; | |
| } | |
| split[split.length-1] = ""; | |
| } | |
| //使用された数値がリストにあるかどうか検索 | |
| for(int i = 0; i < split.length; i+=2) { | |
| if(!split[i].equals("")) { | |
| numberMatch = false; | |
| for(int j = 0; j < availableNumbers.size(); j++) { | |
| if(split[i].equals(availableNumbers.get(j))) { | |
| numberMatch = true; | |
| } | |
| } | |
| if(!numberMatch) { | |
| System.out.println(split[i] + "は使えません!"); | |
| break; | |
| } | |
| } | |
| } | |
| //式・数値が正常な場合計算し、使用可能な数字リストに結果を格納 | |
| if(numberMatch) { | |
| availableNumbers.add(String.valueOf(calc(split))); | |
| } | |
| }else { | |
| System.out.println("不正な式です"); | |
| } | |
| } | |
| //使用可能な数値が残り1つになったとき | |
| System.out.println("最終的に残ったのは" + availableNumbers.get(0)); | |
| //TARGET_NUMBERと計算結果が一致するかどうかの判定 | |
| if(availableNumbers.get(0).equals(TARGET_NUMBER)){ | |
| System.out.println("成功!"); | |
| }else { | |
| System.out.println("失敗!"); | |
| } | |
| scanner.close(); | |
| } | |
| //与えられた式の計算 | |
| static int calc(String[] splitText) { | |
| //式の最初の数値をanswerに格納 | |
| int answer = Integer.parseInt(splitText[0]); | |
| //格納した数値を使用可能な数値リストから削除 | |
| availableNumbers.remove(splitText[0]); | |
| //加減乗除関係なく左から順番に計算し、使用した数値をリストから削除 | |
| for(int i = 1; i < splitText.length; i+=2) { | |
| if(!splitText[i].equals("")) { | |
| switch(splitText[i]) { | |
| case "+": | |
| answer += Integer.parseInt(splitText[i+1]); | |
| break; | |
| case "-": | |
| answer -= Integer.parseInt(splitText[i+1]); | |
| break; | |
| case "*": | |
| answer *= Integer.parseInt(splitText[i+1]); | |
| break; | |
| case "/": | |
| answer /= Integer.parseInt(splitText[i+1]); | |
| break; | |
| } | |
| availableNumbers.remove(splitText[i+1]); | |
| } | |
| } | |
| //計算結果を返却 | |
| return answer; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
元ネタ?
https://gist.github.com/Hayao0819/caad8ef3952bdfef7287ef8c5d71e03c