为什么if语句不会在每个循环中进行评估

时间:2016-02-24 18:27:51

标签: java if-statement infinite-loop

好的我正在制作一个简单的基于文本的游戏,我不确定为什么会创建无限循环。它不是真的无限但我不确定为什么if语句不会在每个循环中被评估。这是整个计划。我需要修复的if语句位于代码末尾的roomEight方法中。

//********************************
// A simple game that moves the
// player though the map
//********************************
import java.util.*;
import java.text.*;

public class mazegame
{
    private static Scanner scan = new Scanner (System.in); // starts scanner for the program
    public static Scanner scanS;
    // ScanS is a scanner for strings.
    // To call this variable type mazegame.scanP;
        public static int lifeCount = 15;
        public static int damage = 1;
        // imp stats
            public static int impAmount = 0;
            public static int impDamage = 1;
            public static int impLife = 1;
                // Low level monster stats
                // m followed by a number stands for monster then the level of monster
                public static int m1health = 5;
                public static int m1damage = 2;
                    // High level monster
                    public static int m2health = 10;
                    public static int m2damage = 2;
                        // Boss stats       
                        public static int bosshealth = 30;
                        public static int bossdamage = 10;
                            // Placement of player
                            public static int placement = 3;
                            public static String movement;
                            public static int scanVal; // holder a scanner value generic.
    public static void main(String[] args)
    {
        System.out.println("You wake up on a cold hard floor");
        time();
        System.out.println("you are unsure how you got there.");
        time();
        System.out.println("There is an opening a head");
        time();
        System.out.println("you walk forward into the opening the ground begins to tremble");
        time();
        System.out.println("the wall behind you closes you are trapped.");
        time();
        time();
        clear(); // clears screen for user.
        roomThree();

    }
    public static void timeHalfSec()
    {
        try
            {
                Thread.sleep(500);              //1000 milliseconds is one second.
            }catch(InterruptedException ex)
                {
                    Thread.currentThread().interrupt();
                }
    }
    public static void time()
    {
        try
            {
                Thread.sleep(1500);             //1000 milliseconds is one second.
            }catch(InterruptedException ex)
                {
                    Thread.currentThread().interrupt();
                }
    }
    public static void clear()
    {
        final String ANSI_CLS = "\u001b[2J";
        final String ANSI_HOME = "\u001b[H";
        System.out.print(ANSI_CLS + ANSI_HOME);
        System.out.flush();
    }
    public static void position(int placement)
    {
        switch( placement )
        {
            //********************************
            // For each room create a method and 
            // call it in this switch statement.
            //********************************
            case 1: 
                break;
            case 2: 
                break;
            case 3:
                break;
            case 4: 
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            case 8: roomEight();
                break;
            case 9:
                break;
            case 10:
                break;
            case 11:
                break;
            case 12:
                break;
            case 13:
                break;
            case 14:
                break;
            case 15:
                break;
            case 16:
                break;
            case 17:
                break;
            case 18:
                break;
            case 19:
                break;
            case 20:
                break;
            case 21:
                break;
            case 22:
                break;
            case 23:
                break;
            case 24:
                break;
            case 25:
                break;
        }
    }
    public static void askMove()
    {
        System.out.println("You can walk forward, left , or right. Beware the imps.");
        System.out.println("Enter L for left, R for right, and F for forward.");
        time();
        System.out.print("Move:");
        movement = scan.nextLine();
    }
    public static void roomThree()
    {
        askMove();
        //--------------------------------
        // This switch stament is only for this room
        //--------------------------------
        switch ( movement )
        {
            case "l":
            case "L": 
                placement = 2;
                System.out.println("Changing rooms Please wait");
                time();
                clear();
                break;
            case "r":
            case "R": 
                placement = 4;
                System.out.println("Changing rooms Please wait");
                time();
                clear();
                break;
            case "f":
            case "F": 
                placement = 8;
                System.out.println("Changing rooms Please wait");
                time();
                clear();
                break;
        }
        // The switch statement changes position and position calls the next room method.
        position(placement);    
    }
    public static void roomEight()
    {
            System.out.print ("You have just entered a new room.");
            System.out.print ("There is an imp ahead would you like to see its stats? 1 for yes and 0 ");
            impAmount = 1;
            scanVal = scan.nextInt();
            if(scanVal == 1 )
            {
                impStats();
            }
            System.out.println("Would you like to hit the imp? 1 for yes and 0 for no.");
            scanVal = scan.nextInt();
            while (impAmount != 0)
            {
                if (scanVal == 1)
                {
                    impAmount = 0;
                    damage++;
                    lifeCount = 15;
                    System.out.println("You killed an imp! You found brass knuckles your damage increased by one. Here are your stats");
                    playerStats();

                }else{
                        lifeCount--;
                        System.out.println("The imp hit you. You took one damage");
                        playerStats();
                        timeHalfSec();
                        dead();
                    }
            }

    }
    public static void playerStats()
    {
        System.out.println("*----------------*");
        System.out.println("Your Hearts: " + lifeCount);
        System.out.println("Your Damage: " + damage);
        System.out.println("*----------------*");
    }
    public static void impStats()
    {
        System.out.println ("*----------------*");
        System.out.println("Amount of Imps: " + impAmount);
        System.out.println("Imp Health: 1");
        System.out.println("impDamage: 1");
        System.out.println("*----------------*");
    }
    public static void dead()
    {
        if(lifeCount < 1)
        {
            System.exit(0);
        }
    }



}



//********************************************************************************************************************************
// Places to look for code and things to look up.
// Lookup: .equalsIgnoreCase, global scanner.
// Places to look for code: 
// http://stackoverflow.com/questions/23586732/how-to-make-a-one-static-scanner-global-variable-without-closing-scan-constantly
// https://www.youtube.com/watch?v=zijvKOjnmwY
// http://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement
// http://www.csci.csusb.edu/dick/samples/java.classes.html#System
// http://stackoverflow.com/questions/22452930/terminating-a-java-program
// 
// 
// 
//********************************************************************************************************************************

2 个答案:

答案 0 :(得分:0)

您可能希望将以下两行移动到循环中:

System.out.println("Would you like to hit the imp? 1 for yes and 0 for no.");
scanVal = scan.nextInt();

答案 1 :(得分:0)

正如我所看到的,你扫描用户输入的nextInt,你用它做的东西,然后你重新扫描nextInt。问题是当你使用扫描仪并要求一个int时,扫描仪中仍然有新行字符('\ n')。因此,当您第二次询问int时,这将返回新的行char。我对这一切的理解不是重点,但你要做的就是解决方案之一:

  1. 使用nextLine代替nextInt并将字符串值解析为Int。这将清除缓冲区,您将能够验证用户是否输入了有效的Int。你这样做:

    String scanVal = scan.nextLine();
    //You can add some try catch in order to validate the int being parsed
    int choice = Integer.parseInt(scanVal); 
    
  2. 或者您可以在scan.nextLine()

  3. 之后致电scan.nextInt()扫描您的整数后清除缓冲区

    希望这有帮助!