2015-04-15 79 views
1

所以我正在使用try/catch块来捕获(这是作业)的猜数游戏。如果用户输入非整数值或低于或高于给定范围的数字(在本例中为1-10)。实现try/catch块

但我有理解/正确放置try catch块,我读了它是如何工作的,但是当我尝试将它实现到我的简单代码中时,它似乎被忽略。

下面是代码

//import statements 
import java.util.*;  //for scanner class 


// class beginning 
class Guess { 
    public static void main(String[] args) { 
     //Declare variables area 

     int secretNumber, guess; 

     secretNumber = (int) (Math.random() * 10 + 1);   

     Scanner keyboard = new Scanner(System.in); 


     // beginning message to user to explain the program 
     System.out.println("Welcome to my guess a number program!"); 
     System.out.println("Please enter in a number to begin guess what the secret number is(1-10): "); 

     //Collect inputs from user or read in data here 

     System.out.println("Enter a guess (1-10): "); 
     try { 
      guess = keyboard.nextInt(); 
      if (guess < 1 || guess > 10){ 
      } 
     } catch (InputMismatchException e) { 
      guess= keyboard.nextInt(); 
      System.out.println("Not a valid integer"); 
     } 
     //Echo input values back to user here 



     //main code and calculations to do 
     do { 

      if (guess < 1 || guess > 10) { 
       System.out.println("Your guess is not in the corre try again."); 
       guess = keyboard.nextInt(); 
      } 
      if (guess == secretNumber) { 
       System.out.println("Your guess is correct. Congratulations!"); 
       guess = keyboard.nextInt(); 
      } else if (guess < secretNumber) { 
       System.out 
         .println("Your guess is smaller than the secret number."); 
       guess = keyboard.nextInt(); 
      } else if (guess > secretNumber) { 
       System.out 
         .println("Your guess is greater than the secret number."); 
       guess = keyboard.nextInt(); 
      } 
     } while (guess != secretNumber); 


     //End program message 
     System.out.println(); 
     System.out.println("Hope you enjoyed using this program!"); 
    }// end main method 


}// end class 

它不显示"Your guess is correct. Congratulations!"当你猜对数。

我正在实现try catch块吗?如果不是,我怎么能解释它,所以我可以做第二个捕获浮点数,字符串和任何不是int的东西。

它所做

  • 它正确地从1-10
  • 随机化程序没有检查,看它是否在给定范围

编辑下面 是内更新的代码我现在唯一的问题是,我无法弄清楚如何应用另一个捕获,如果用户只需输入没有输入任何内容我假设我需要从用户的输入变成一串然后比较?

//import statements 
import java.util.*;  //for scanner class 


// class beginning 
public class Guess { 
public static void main(String[] args) { 
    //Declare variables area 
    int guess, secretNumber = (int) (Math.random() * 10 + 1), lowGuess,highGuess; 
    Scanner keyboard = new Scanner(System.in); 

    // beginning message to user to explain the program 
    System.out.println("Welcome to my guessing number program!"); 
    System.out.println("Please enter in a number to start guessing(enter in a number between 1-10): "); 

    //main code and calculations to do 
    guess = 0; 
    lowGuess = 0; 
    highGuess = 11; 
    do { 
     try { 
      guess = keyboard.nextInt(); 
      if (guess < 1 || guess >10){ 
       System.out.println("Your guess is not in the correct range try again."); 
      } 
      else if(guess == secretNumber){ 
       System.out.println("Your guess is correct. Congratulations!"); 
      } 
      else if(guess < secretNumber && guess <= lowGuess){ 
       System.out.println("The number you entered is either the same entered or lower please re-enter"); 
      } 
      else if (guess < secretNumber && guess > lowGuess){ 
       lowGuess = guess; 
       System.out.println("Your guess is smaller than the secret number."); 
      } 
      else if (guess > secretNumber && guess >= highGuess){ 
       System.out.println("The number you entered is either the same entered or higher please re-enter"); 
      } 
      else if (guess > secretNumber && guess < highGuess){ 
       highGuess = guess; 
       System.out.println("Your guess is greater than the secret number."); 
      } 
     } catch (InputMismatchException e) { 
      System.out.println("Not a valid input please re-enter"); 
      keyboard.next(); 
      guess = 0; 
     } 
    } while (guess != secretNumber); 

    //End program message 
    System.out.println(); 
    System.out.println("Hope you enjoyed using this program!"); 
}// end main method 


}// end class 
+0

你只会第一次捕捉到异常,并且会丢失所有其他'nextInt()'。我建议你在do/while里只做一次nextInt()。 – m0skit0

+2

您的缩进不会让您的代码更容易阅读。也许让你的IDE为你的代码格式化(在Eclipse中你可以使用'Source'菜单以及'Format'或'Correct Indentation')。 – Pshemo

+0

你告诉我们它不*做什么。它会做什么? –

回答

0

你想在这里做什么?

if (guess < 1 || guess > 10) { 
} 

你应该尝试这样的事:

public class Guess { 
    static int guess, secretNumber = (int) (Math.random() * 10 + 1); 
    static Scanner keyboard = new Scanner(System.in); 

    public static void main(String[] args) { 
     do { 
      try {  
       guess = keyboard.nextInt(); 
       if (guess < 1 || guess >10){ 
        System.out.println("Your guess is not in the corre try again."); 
       } 
       if(guess == secretNumber){ 
        System.out.println("Your guess is correct. Congratulations!"); 
       } 
       else if (guess < secretNumber){ 
        System.out.println("Your guess is smaller than the secret number."); 
       } 
       else if (guess > secretNumber){ 
        System.out.println("Your guess is greater than the secret number.");          
       } 
      } catch (InputMismatchException e) { 
       System.out.println("Not a valid integer"); 
      } 
     } while (guess != secretNumber); 
    } 
} 

编辑:你必须把一段代码,这也许可以抛出一个异常,在try块的范围。知道将哪部分代码放在try块中是最先进的(不是太多也不是太少)。

在中间的尝试,赶上你有你的主代码/计算, 做什么,然后你关闭其关闭,并结束与捕捉,看看是否 所有,如果输入的内容是有效的后?

您可以随时在顶层捕捉异常,就像您在这里所说的那样,但在实践中,这不是实现它的方式。因为当发生异常并且不捕获它时,将会有一个名为堆栈展开的进程。

理解堆栈展开如何工作以理解发生的事情非常重要。

你可以在这里找到关于这个过程如何工作的信息:Explanation of stack unwinding。它解释了C++中的堆栈展开,但我期望它在Java中(完全)相同。

+0

原始类型没有equals方法 – Sanj

+0

只需在while内部移动try-catch,就不需要递归调用。 – m0skit0

+0

如果我知道这是如何工作的,你需要从try块和你正在评估的代码开始,在这种情况下它的if(guess <1 || guess> 10)'和输出的消息一起在try和抓住你的主要代码/计算工具,然后关闭它并结束捕获,以查看是否毕竟如果输入的内容是有效的? –

0

这是另一种方法,但使用例外。请阅读this page了解try/catch块如何工作。

//import statements 
import java.util.*;  //for scanner class 


// class beginning 
class Guess { 
public static void main(String[] args) { 
//Declare variables area 
int secretNumber, guess; 

secretNumber = (int) (Math.random() * 10 + 1); 

Scanner keyboard = new Scanner(System.in); 


// beginning message to user to explain the program 
System.out.println("Welcome to my guess a number program!"); 
System.out.println("Please enter in a number to begin guess what the secret number is(1-10): "); 

//Collect inputs from user or read in data here 


//main code and calculations to do 
do { 


    System.out.println("Enter a guess (1-10): "); 
    try { 
    guess = keyboard.nextInt(); 
    if (guess < 1 || guess > 10){ 
     throw new Exception("Number must be between 1 and 10"); 
    }else if(guess == secretNumber){ 
    throw new Exception("YOU WIN"); 
    } 
    else if (guess < secretNumber){ 
    throw new Exception("NUMBER IS +"); 
    } 
    else if (guess > secretNumber){ 
    throw new Exception("NUMBER IS -"); 
    } 
} catch (InputMismatchException e) 
{ 
    System.println("mustbeanumber"); 
} 
catch(Exception e) 
{ 
System.out.println(e.getMessage()); 
} 

} while (guess != secretNumber); 


//End program message 
System.out.println(); 
System.out.println("Hope you enjoyed using this program!"); 
}// end main method 


}// end class` 
+0

'IT's'?那是什么? –

+0

这不是一个很好的解决方案AFAICT。它只会再试一次。 –

+0

我纠正了...... –