Step1 ゲームタイトルと終了メッセージを表示してみよう

[ソースフォルダ] myproj_super_intro/src

[パッケージ] jp.co.f1.superintro.ch08

[作成] HighAndLowS01.java

package jp.co.f1.superintro.ch08;

public class HighAndLowS01 {

    public static void main(String[] args) {

        System.out.println("**************");
        System.out.println("* High & Low *");
        System.out.println("**************");

        System.out.println("");

        System.out.println("--ゲーム終了--");


    }

}

Step2 ランダムな数値を取得して表示してみよう

[ソースフォルダ] myproj_super_intro/src

[パッケージ] jp.co.f1.superintro.ch08

[作成] HighAndLowS02.java

package jp.co.f1.superintro.ch08;

public class HighAndLowS02 {

    public static void main(String[] args) {

        System.out.println("**************");
        System.out.println("* High & Low *");
        System.out.println("**************");

        System.out.println("");

        int leftCard = (int)(Math.random()*9)+1;
        System.out.println("ランダム数値:"+leftCard);

        System.out.println("");

        System.out.println("--ゲーム終了--");


    }

}

Step3 ランダムな数値をトランプの枠に合わせて表示してみよう

[ソースフォルダ] myproj_super_intro/src

[パッケージ] jp.co.f1.superintro.ch08

[作成] HighAndLowS03.java

package jp.co.f1.superintro.ch08;

public class HighAndLowS03 {

    public static void main(String[] args) {

        int leftCard = (int)(Math.random()*9)+1;

        System.out.println("**************");
        System.out.println("* High & Low *");
        System.out.println("**************");

        System.out.println("");

        System.out.println("  [問題表示]");
        System.out.println("*****    *****");
        System.out.println("*   *    * * *");
        System.out.println("* "+leftCard+" *    * * *");
        System.out.println("*   *    * * *");
        System.out.println("*****    *****");

        System.out.println("");

        System.out.println("--ゲーム終了--");


    }

}

Step4 キーボード入力を利用してHigh or Lowを選択してみよう

[ソースフォルダ] myproj_super_intro/src

[パッケージ] jp.co.f1.superintro.ch08

[作成] HighAndLowS04.java

package jp.co.f1.superintro.ch08;
import java.util.Scanner;
public class HighAndLowS04 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int leftCard = (int)(Math.random()*9)+1;

        System.out.println("**************");
        System.out.println("* High & Low *");
        System.out.println("**************");

        System.out.println("");

        System.out.println("  [問題表示]");
        System.out.println("*****    *****");
        System.out.println("*   *    * * *");
        System.out.println("* "+leftCard+" *    * * *");
        System.out.println("*   *    * * *");
        System.out.println("*****    *****");
        System.out.print(" High  or  Low ?(h/l)>");
        String select = sc.nextLine();

        if(select.equals("h")){
            System.out.println(" →Highを選択しました。");
        }else{
            System.out.println(" →Lowを選択しました。");
        }

        System.out.println("");

        System.out.println("--ゲーム終了--");


    }

}

Step5 隠れた数値を画面に表示してみよう

[ソースフォルダ] myproj_super_intro/src

[パッケージ] jp.co.f1.superintro.ch08

[作成] HighAndLowS05.java

package jp.co.f1.superintro.ch08;
import java.util.Scanner;
public class HighAndLowS05 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int leftCard = (int)(Math.random()*9)+1;
        int rightCard = (int)(Math.random()*9)+1;

        System.out.println("**************");
        System.out.println("* High & Low *");
        System.out.println("**************");

        System.out.println("");

        System.out.println("  [問題表示]");
        System.out.println("*****    *****");
        System.out.println("*   *    * * *");
        System.out.println("* "+leftCard+" *    * * *");
        System.out.println("*   *    * * *");
        System.out.println("*****    *****");
        System.out.print(" High  or  Low ?(h/l)>");
        String select = sc.nextLine();

        if(select.equals("h")){
            System.out.println(" →Highを選択しました。");
        }else{
            System.out.println(" →Lowを選択しました。");
        }

        System.out.println("");

        System.out.println("  [結果表示]");
        System.out.println("*****    *****");
        System.out.println("*   *    *   *");
        System.out.println("* "+leftCard+" *    * "+rightCard+" *");
        System.out.println("*   *    *   *");
        System.out.println("*****    *****");

        System.out.println("");

        System.out.println("--ゲーム終了--");


    }

}

Step6 隠れた数値と比較してゲーム結果を画面に表示してみよう

[ソースフォルダ] myproj_super_intro/src

[パッケージ] jp.co.f1.superintro.ch08

[作成] HighAndLowS06.java

package jp.co.f1.superintro.ch08;
import java.util.Scanner;
public class HighAndLowS06 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int leftCard = (int)(Math.random()*9)+1;
        int rightCard = (int)(Math.random()*9)+1;

        System.out.println("**************");
        System.out.println("* High & Low *");
        System.out.println("**************");

        System.out.println("");

        System.out.println("  [問題表示]");
        System.out.println("*****    *****");
        System.out.println("*   *    * * *");
        System.out.println("* "+leftCard+" *    * * *");
        System.out.println("*   *    * * *");
        System.out.println("*****    *****");
        System.out.print(" High  or  Low ?(h/l)>");
        String select = sc.nextLine();

        if(select.equals("h")){
            System.out.println(" →Highを選択しました。");
        }else{
            System.out.println(" →Lowを選択しました。");
        }

        System.out.println("");

        System.out.println("  [結果表示]");
        System.out.println("*****    *****");
        System.out.println("*   *    *   *");
        System.out.println("* "+leftCard+" *    * "+rightCard+" *");
        System.out.println("*   *    *   *");
        System.out.println("*****    *****");
        String result = null;

        if(leftCard < rightCard){
            result = "h";
        }else if(leftCard > rightCard){
            result = "l";
        }else{
            result = select;
        }

        if(select.equals(result)){
            System.out.println(" →You Win!");
        }else{
            System.out.println(" →You Lose...");
        }

        System.out.println("");

        System.out.println("--ゲーム終了--");


    }

}

Step7 連続挑戦できるように繰り返してみよう

[ソースフォルダ] myproj_super_intro/src

[パッケージ] jp.co.f1.superintro.ch08

[作成] HighAndLowS07.java

package jp.co.f1.superintro.ch08;
import java.util.Scanner;
public class HighAndLowS07 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(true){
            int leftCard = (int)(Math.random()*9)+1;
            int rightCard = (int)(Math.random()*9)+1;

            System.out.println("**************");
            System.out.println("* High & Low *");
            System.out.println("**************");

            System.out.println("");

            System.out.println("  [問題表示]");
            System.out.println("*****    *****");
            System.out.println("*   *    * * *");
            System.out.println("* "+leftCard+" *    * * *");
            System.out.println("*   *    * * *");
            System.out.println("*****    *****");
            System.out.print(" High  or  Low ?(h/l)>");
            String select = sc.nextLine();

            if(select.equals("h")){
                System.out.println(" →Highを選択しました。");
            }else{
                System.out.println(" →Lowを選択しました。");
            }

            System.out.println("");

            System.out.println("  [結果表示]");
            System.out.println("*****    *****");
            System.out.println("*   *    *   *");
            System.out.println("* "+leftCard+" *    * "+rightCard+" *");
            System.out.println("*   *    *   *");
            System.out.println("*****    *****");
            String result = null;

            if(leftCard < rightCard){
                result = "h";
            }else if(leftCard > rightCard){
                result = "l";
            }else{
                result = select;
            }

            if(select.equals(result)){
                System.out.println(" →You Win!");
            }else{
                System.out.println(" →You Lose...");
                break;
            }
            System.out.println("");

        }

        System.out.println("--ゲーム終了--");


    }

}