2012-10-03 33 views
0

我有这个问题,我创建了两个验证用户输入的方法。然后我试图在程序运行的其余部分之前让他们的输入验证。这将无法正常工作,我无法在网上找到任何有助于解决问题的事情。无法调用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; 
} 

}

+0

你面临什么问题? –

回答

4

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

boolean valid = false; 

while(valid == false); 
{ 

由于尾随分号:将其删除。拖尾半结肠使得while等效于:

while (valid == false) {} 

这意味着预期的循环体从不执行和的valid值没有改变。

+0

你有牛眼。 –

+0

@hmjd ..你真的注意到了吗?哇。 –

+0

哇哇谢谢一堆!我花了几个小时试图找出问题所在......它总是这样.. –

1

虽然你的问题已经解决了,我想指出你的代码的一些东西..

首先,你的代码有那么多duplicacy的..

你的方法: - getIntWithinRange只是委托您的要求的另一种方法getInt,我认为这是没用..
你有getInt方法都什么,你可以简单地将它移动到getIntWithinRange方法里,你会不会创造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) 

在main方法: -

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(); 

在你getInt方法: -

else { 
      System.out.println("Please enter a number... "); 
    } 
    sc.nextLine(); --> // This should be sc.next().. And should be inside else 
         // You are just getting a new user input.. not required here.. 
         // Just move the pointer to next input.. But don't read it.. 
+0

虽然这不是一个直接的答案,但我仍然为此+1。很好的答案。 – Austin

+0

@奥斯汀..谢谢..其实OP已经得到了他在以前的答案他的问题的答案..我只是指出他可能感兴趣的一些错误.. :) –

+0

感谢您的额外输入...我是一个java noob –