I want to repeat the status display in the switch statement.

Asked 1 years ago, Updated 1 years ago, 40 views

I am currently creating a character development game on java.Compile and

Enter the option to branch the switch so that points are added to each option.
When I enter the selection number, I want to display the status of each additional point (the getYutoStatus() function), but now

$java part01
Hello, Yuto!!
Yuto is 0 years old now.
Let's do our best to raise them.
1.Give Milk
2. Sleep
3. Comforting
2
1.Give Milk
2. Sleep
3. Comforting
3
1.Give Milk
2. Sleep
3. Comforting
4
Yuto, you have 17 points for training and 0 points for minus points.
Yuto, you have 17 points for training and 0 points for minus points.

As shown in , the next selection is processed, and the results are displayed at the end.
"I would like you to tell me how to describe it so that it can be set to ""input + result display"" each time."
Thank you for your cooperation.

public class part01 {
    static String name = "Yuto"; // Character name
    static age;// character age
    static int good=5;// Character Development Points
    static int wagama=0;// character minus points

    public static void main(String[]args)throws java.io.IOException{
        putOpening();
        putCommand();
        putStageClear();
    }

    public static void putCommand()throws java.io.IOException{

        put("1. Give Milk";
        put("2. Sleep";
        put("3. soothe");

        switch(inputCommand()) {
            case '1': {
                good+=2;
                putCommand();
                getYutoStatus();
                break;
            }
            case '2': {
                good+=5;
                putCommand();
                getYutoStatus();
                break;
            }
            case '3': {
                good+=7;
                putCommand();
                getYutoStatus();
                break;
            }

        }

    }

    /**
    * View Opening
    */
    public static void putOpening() {
        System.out.println(name+"Hello, my friend!!");
        System.out.println(name+"You are now"+age+" years old");
        System.out.println("Let's work hard");
    }

    public static void getYutoStatus(){
        put(name+"you are"+"development point"+good+"+"minus point"+wagama);
    }

    /**
    * Display stage clear screen
    */
    public static void putStageClear() {

        if(good>=20){
            age = 1;
            put(name+"you are"+age+" years old now!!");
        } else if(good>=30){
            age = 2;
            put(name+"you are"+age+" years old now!!");
        } else if(good>=40){
            age = 3;
            put(name+"you are"+age+" years old now!!");
        }
    }

    /**
    * take care of one's child
    */
    /*
    static void ikuji() {
    // Rising parenting points
    // Selfish and negative points
        int d = r.nextInt(8);
        wagama-=d;

    }
    */

    public static int inputCommand()throws java.io.IOException{

            intc=System.in.read();
            if(c==10||c==13){
                return(inputCommand());
            }
            return(c);
    }

    public static void put (String str) {
        System.out.println(str);
    }

}

java

2022-09-30 21:29

1 Answers

In conclusion, I think you should put getYutoStatus() in the switch statement before putCommand().

Supplemental:
1. Put the default action in the switch statement
2. Let name be private static final
3. The if statement of the inputCommand method looks like a magic number, but what are 10 and 13?If it's a meaningful number, why don't you define a constant and use it?


2022-09-30 21:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.