无法在Java中调用验证方法

时间:2012-10-03 07:26:50

标签: java

我有这个问题,我创建了两个验证用户输入的方法。然后我试图在程序的其余部分运行之前让他们的输入进行验证。它不起作用,我找不到任何有用的东西。

我在另一个程序中以完全相同的方式完成了它,但它不适用于此程序。任何帮助将不胜感激。

(主要部分已被注释掉,因为我试图让它运行)...

    import java.util.*;




public class GuessingGame {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub



    int answer;
    int tries = 0;
    answer = (int) (Math.random() * 99 + 1);



    System.out.println("Welcome to the Guess the Number Game ");
    System.out.println("+++++++++++++++++++++++++++++++++++++ \n");
    System.out.println("I'm thinking of a number between 1-100 ");
    System.out.println("Try to guess it! ");

    Scanner sc = new Scanner(System.in);
    String choice = "y";

while (choice.equalsIgnoreCase("y"))    
{



        int guess = getIntWithinRange(sc, "Enter number: ", 0, 100);




        /**

        if (guess == answer) 
        {
            System.out.println("Your guess is correct! Congratulations!");
        }
        else if (guess > answer + 10)
            { System.out.println("Your guess was way too high");
            tries++;
            }

        else if (guess < answer)
            { System.out.println("Your guess was too low. Try again. ");
            tries++;
            }

        else if (guess > answer)
            {System.out.println("Your guess was too high. Try again.");
            tries++;
            }



                System.out.println("The number was " + answer + " !");
                System.out.println("You guessed it in " + tries + " tries");

                    if (tries < 2)
                        {System.out.println("That was lucky!");
                        }

                    if (tries >=2 && tries <=4)
                        {System.out.println("That was amazing!");
                        }

                    if (tries > 4 && tries <= 6)
                        {System.out.println("That was good.");
                        }

                    if (tries >= 7 && tries <=7)
                        {System.out.println("That was OK. ");
                        }

                    if (tries > 7 && tries < 10)
                        { System.out.println("That was not very good. ");
                        }

                    if (tries >= 10)
                        {System.out.println("This just isn't your game. ");
                        }
                **/ 

                      //ask if they want to continue
                    System.out.println("\n Continue (y/n)? ");
                    choice = sc.next();
                    sc.nextLine();
                    System.out.println();

    }

    //print out thank you message
    System.out.println("Thanks for finding the common divisor ");
    sc.close();
}

public static int getInt(Scanner sc, String prompt)
{
    int i = 0;
    boolean valid = false;

    while(valid == false);
    {   
        System.out.println(prompt);
        if (sc.hasNextInt())
        {
            i = sc.nextInt();
            valid = true;
        }
        else
        {
            System.out.println("Please enter a number... ");
        }
        sc.nextLine();
    }
    return i;

}








public static int getIntWithinRange(Scanner sc, String prompt, int min, int max)
{
    int i = 0;
    boolean valid = false;

    while (valid == false)
    {
        i = getInt(sc, prompt);
        if (i <= min || i >= max)
            System.out.println("Number must be between 1-100 ");
        else
            valid = true;
    }
    return i;
}

}

2 个答案:

答案 0 :(得分:4)

在方法getInt()中,这将导致无限循环:

boolean valid = false;

while(valid == false);
{

由于尾​​部分号:删除它。尾部半冒号使while等效于:

while (valid == false) {}

这意味着永远不会执行预期的循环体,并且永远不会更改valid的值。

答案 1 :(得分:1)

虽然您的问题已经解决,但我想指出一些关于您的代码的事情......

首先,你的代码有很多重复..

您的方法: - getIntWithinRange 只是将您的请求委托给另一种方法 getInt ,我认为这种方法毫无用处。
您在 getInt 方法中所拥有的一切,只需将其移至 getIntWithinRange 方法即可。这样您就不会创建{{ 1}}变量,2 boolean以及许多重复的代码..

此外,您不需要检查布尔值,如: -

2 while loops

此外,您可以将while (valid == false) // Not needed while (!valid) // is enough 作为您的实例变量..您不需要在阅读用户输入的所有方法中定义它。实际上您不是..您只是复制它..

在评论的代码中: -

Scanner sc = new Scanner(System.in);

相当于: -

if (tries >= 7 && tries <=7)

在您的主要方法中: -

if (tries == 7)

在您的getInt方法中: -

System.out.println("\n Continue (y/n)? ");
choice = sc.next();
sc.nextLine(); --> // You don't need this line at all.. 
                   // It is just used to read user input.. 
                   // That you are doing in your `getIntWithinRange` method..
System.out.println();