简体   繁体   中英

Why is my program printing 3 times after user input?

I want to make a game kind of like those Oregon Trail games, and everything goes fine, until the User actually has to input something, like if they want to hunt, or shop, or move on. The console prints the messages 3 times, but no error is shown. The message is supposed to print once, and I cannot figure out why this is happening.

Console: [1]: https://i.stack.imgur.com/NMC4C.png Code:

class Started{
    static int DaysPassed = 1;
    static double Food = 100;
    static boolean city = true;
    public void start()
    throws java.io.IOException{
      
       
       System.out.println("You have passed " + DaysPassed + " days");
       System.out.println("You have " + Food + " food left");
       if(city = true){
           System.out.println("There is a city here");
       }
       System.out.println("Press M, H, or T to shop, hunt or travel respectively");
       char decision = (char) System.in.read();
       switch(decision){
         case 'M':
          if (city = true){
              //open market interface
              DaysPassed = DaysPassed + 1;
              Food = Food - 10;

          } else {
              System.out.println("There is no city here, try next route");
          }
              break;
         case 'H':
         //open hunt interface
          Food = Food - 10;
          DaysPassed = DaysPassed + 1;
          double hunter = Math.floor(Math.random());
          System.out.println("You got" + hunter * 10 + " food");
          Food = Food + hunter * 10;
          System.out.println("Your food is now " + Food);
          start();
          break;
          case 'T':
          DaysPassed = DaysPassed + 1;
          Food = Food - 10;
          start();
          break;
          default: 
          System.out.println("Not a valid Character, remember that it has to be caps or it won't work");
          start();
       }



    }
}
public class CalifornianTrail {
    public static void main(String[] args)
     throws java.io.IOException{
         //some filler start code
        System.out.println("Welcome to Californian Trail, a Console based video game based on Oregon Trail");
        System.out.println("Starting Simulation");
        System.out.println("Game does not have a save function");
        System.out.println("This is game version 1.0 Alpha");
        System.out.println("Today is the day you start your new journey as a pioneer.");
       Started Menu = new Started();
       Menu.start();
         




    }
}

It was a long time since I worked with console programs, so I had to use the debugger this time.

This line is the reason:

char decision = (char) System.in.read();

and should be replaced with these two:

Scanner sc = new Scanner(System.in);
char decision =  sc.next().charAt(0);

Why was your code executed three times? Because when you type a char and press enter, a string like this is created M\n, so you have two chars inside the stream System.in. Each System.in.read() reads the first char after the last char that was read. You had two chars each time, so your code was executed three times.

Additionally, your code has a recurrence (the start method calls the start method). After a long time, you will run out of memory. The simplest way to avoid recurrence is to use a loop instead. A break command or return command can break it.

Please learn asap:

  1. How to use a debugger
  2. Naming conventions in java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM