如何在Java中使用try catch获取异常?

时间:2016-04-29 01:01:14

标签: java

所以我正在制作一个程序,我想要一个例外,所以程序不会崩溃 如果用户放置的字符串不在else if statements中,那么它就不会崩溃。

另外,我尝试为integer执行此操作,因此如果有人试图写一些不是integer的内容,它就不会崩溃。该计划将抓住它,并说它不是integer

如何在try catch的{​​{1}}中获取例外情况。

感谢您的帮助

以下是代码:

java

4 个答案:

答案 0 :(得分:1)

我相信scan.nextInt();只会扫描整数,所以不应该需要捕获非整数

答案 1 :(得分:0)

您的计划有几个问题:

  • String planetName需要在try块之外声明。

  • 你不应该在try / catch中拥有planetName = scan.nextLine();。你应该找到一种方法来继续询问用户行星名称,直到他们得到一个正确的名称。

  • 此外,int weight需要在try块之外声明。

  • 在这里,你需要找出一种方法,如果他们没有给你一个整数(例如,你得到一个例外),就不断询问用户。

编辑:根据MasterBlaster的建议,您还应该使用scan.close()

关闭扫描仪

答案 2 :(得分:0)

您实际上不需要使用例外。你可以为星球输入任何东西,它不会崩溃,因为你正在检查nextLine()。对于重量,只需在设置重量之前检查是否scan.hasNextInt()

import java.util.Scanner; 


public class WeightOnADifferentPlanet { 
    public static void main ( String[] args ) {   
        Scanner scan = new Scanner(System.in); 

        System.out.print("Where do you want to travel? ");
        String planetName = scan.nextLine(); 

        System.out.print("Please enter your weight: ");
        int weight = 0;
        if (scan.hasNextInt()) {
            weight = scan.nextInt();

            double earthCalculation = weight * 1.0; 
            double jupiterCalculation = weight * (21.0 / 8.0); //check
            double marsCalculation = weight * (3.0 / 8.0); 
            double mercuryCalculation = weight * (3.0 / 10.0); 
            double neptuneCalculation = weight * (11.0 / 10.0); //check 
            double plutoCalculation = weight * (7.0 / 10.0); 
            double saturnCalculation = weight * (6.0 / 5.0); //check
            double uranusCalculation = weight * (9.0 / 10.0); 
            double venusCalculation = weight * (7.0 / 8.0);

            if (planetName.equalsIgnoreCase("Earth")) {
                System.out.println("Your weight on " + planetName + " is: " + earthCalculation + " pounds."); 
            } else if (planetName.equalsIgnoreCase("Jupiter")) {
                System.out.println("Your weight on " + planetName + " is: " + jupiterCalculation + " pounds."); 
            } else if (planetName.equalsIgnoreCase("Mars")) {
                System.out.println("Your weight on " + planetName + " is: " + marsCalculation + " pounds."); 
            } else if (planetName.equalsIgnoreCase("Mercury")) {
                System.out.println("Your weight on " + planetName + " is: " + mercuryCalculation + " pounds."); 
            } else if (planetName.equalsIgnoreCase("Neptune")) {
                System.out.println("Your weight on " + planetName + " is: " + neptuneCalculation + " pounds."); 
            } else if (planetName.equalsIgnoreCase("Pluto")) {
                System.out.println("Your weight on " + planetName + " is: " + plutoCalculation + " pounds."); 
            } else if (planetName.equalsIgnoreCase("Saturn")) {
                System.out.println("Your weight on " + planetName + " is: " + saturnCalculation + " pounds."); 
            } else if (planetName.equalsIgnoreCase("Uranus")) {
                System.out.println("Your weight on " + planetName + " is: " + uranusCalculation + " pounds."); 
            } else if (planetName.equalsIgnoreCase("Venus")) {
                System.out.println("Your weight on " + planetName + " is: " + venusCalculation + " pounds."); 
            } else {
                System.out.println("Planet not recognized");
            }
        } else {
            System.out.println("Invalid weight");
        }
        scan.close();
    }
}

答案 3 :(得分:0)

所以我最终修复它,结果就是这样。     import java.util.InputMismatchException;     import java.util.Scanner;

public class WeightOnADifferentPlanet { 
static Scanner scan = new Scanner ( System.in ); 

public static void main ( String[] args ){   



System.out.println("What planet do you want to travela:?");

     String planetName = scan.nextLine(); 



 System.out.println("Please enter your weight:"); 
 int weight = Integer();


 //int weight = scan.nextInt();


 double earthCalculation = weight * 1.0; 
 double jupiterCalculation = weight * (21.0 / 8.0); //check
 double marsCalculation = weight * (3.0 / 8.0); 
 double mercuryCalculation = weight * (3.0 / 10.0); 
 double neptuneCalculation = weight * (11.0 / 10.0); //check 
 double plutoCalculation = weight * (7.0 / 10.0); 
 double saturnCalculation = weight * (6.0 / 5.0); //check
 double uranusCalculation = weight * (9.0 / 10.0); 
 double venusCalculation = weight * (7.0 / 8.0);


 if (planetName.equalsIgnoreCase("Earth")) 
 {
   System.out.println("Your weight on "+planetName+" is: "+earthCalculation+" pounds."); 
 }
 else if (planetName.equalsIgnoreCase("Jupiter"))
 {
   System.out.println("Your weight on "+planetName+" is: "+jupiterCalculation+" pounds."); 
 }
 else if (planetName.equalsIgnoreCase("Mars")) 
 {
   System.out.println("Your weight on "+planetName+" is: "+marsCalculation+" pounds."); 
 }
 else if (planetName.equalsIgnoreCase("Mercury")) 
 {
   System.out.println("Your weight on "+planetName+" is: "+mercuryCalculation+" pounds."); 
 } 
 else if (planetName.equalsIgnoreCase("Neptune")) 
 {
   System.out.println("Your weight on "+planetName+" is: "+neptuneCalculation+" pounds."); 
 }
 else if (planetName.equalsIgnoreCase("Pluto")) 
 {
    System.out.println("Your weight on "+planetName+" is: "+plutoCalculation+" pounds."); 
 }
 else if (planetName.equalsIgnoreCase("Saturn")) 
 {
   System.out.println("Your weight on "+planetName+" is: "+saturnCalculation+" pounds."); 
 }
  else if (planetName.equalsIgnoreCase("Uranus")) 
 {
   System.out.println("Your weight on "+planetName+" is: "+uranusCalculation+" pounds."); 
 } 
   else if (planetName.equalsIgnoreCase("Venus")) 
 {
   System.out.println("Your weight on "+planetName+" is: "+venusCalculation+" pounds."); 

 }
   else {
       System.out.println("Planet not recognized");
   }
 }



 public static int Integer(){
      while (true)
      {
       try
       {
        return scan.nextInt();
       }
       catch (InputMismatchException e)
       {
           scan.next();
           System.out.print("That’s not an integer. Try again: ");
       }

}
}
}