3

我正在检查用户输入的号码是否是 Zeckendorf,如果不是,我想显示一个异常,我该怎么做?另外,如何将 Zeckondorf 转换为其十进制等效值?

import java.util.Scanner;


public class IZeckendorfNumberConvertor {
static String number;
int zeckonderEquivalent;
static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

    convertToZeckonderNumber();
    isTrueZeckonderNumber();
}

private static boolean isTrueZeckonderNumber() {
    System.out.println("Enter a Zeckonder Number:");
    number = scanner.nextLine();
    if (number.equals("10100")) 
    {
        return true; } 
    else if (number.equals("010011") || number.equals("010100")) 
    { 
        return false; }
    return false;
}

private static void convertToZeckonderNumber() {

}}
4

7 回答 7

6

我建议您不要显示异常(即跟踪等),因为它对用户非常不友好。您可以使用 throw 语法来引发适当的异常:

 throw new Exception("Given number is not a Zeckendorf number");

但一定要抓住它并显示一个漂亮而干净的消息:

try {
   // Your input code goes here
} catch (Exception e) {
    System.out.println(e.getMessage());
}

另一个更简单的选择是只检查方法的返回值并相应地打印结果。

我建议使用后一种解决方案,因为当您的程序中发生不良和意外情况并且您希望优雅地处理它时,会使用异常。在您的情况下,这种行为是预期的(即用户给出了错误的数字),因此检查返回值将更加干净和容易。

于 2013-11-04T12:07:02.883 回答
4

使用 try catch 块捕获异常

            try {

            } catch (Exception e) {

                e.printStackTrace();
            }

也使用 throw 抛出一个新异常

在此处输入图像描述

于 2013-11-04T12:23:45.497 回答
2

假设确实想要显示异常,而不是更用户友好的消息,第一步可能是将异常作为字符串获取。然后你可以用那个字符串做你喜欢的事情(回显到控制台,放在 javax.swing.JTextArea 中,通过电子邮件发送等等)。

如果您想要来自异常的消息,那么 getMessage() 将执行以下操作:

try { ... }
catch(FooException e) {
    String msg = e.getMessage();
}

但是,如果你想要整个异常、堆栈跟踪和所有,你会想要这样的东西:

public static String stackTrace(Exception e) {
    StringWriter w = new StringWriter();
    e.printStackTrace(new PrintWriter(w));
    return w.toString();
}

// ...

try { ... }
catch(FooException e) {
    String st = stackTrace(e);
}

如果您只想将完整的堆栈跟踪回显到控制台,可以使用 printStackTrace(), no-arg 方法:

try { ... }
catch(FooException e) {
    e.printStackTrace();
}

如果您想更好地控制堆栈跟踪的显示,您可以通过以下方式获取详细信息:

try { ... }
catch(FooException e) {
    StackTraceElement[] stes = e.getStackTrace();
    // Do something pretty with 'stes' here...
}     
于 2013-11-04T13:05:58.667 回答
1

你是什​​么意思你想显示一个例外?我建议只给用户反馈,因为异常更常用于不应该发生的异常操作。

但是,如果您愿意,您可以打印一条消息来解释发生了什么。

try {

} catch (Exception e) {
    System.out.println(e.getMessage());
}
于 2013-11-04T12:06:19.623 回答
1

您可以使用简单的if.

if(yourCondition){
    // Happy scenario
    // Go shead
}else{
    // Error Scenario
    System.out.println("Error. Invalid Input.");
    // If you persist to throw an exception, then you can do something like this
    // throw new Exception("Exception Explanation"); // I've commented this, but you can uncomment it if needed
    // But the advice is to display an error message, than throw a exception.
}

关于转换,您可以像这样将二进制转换为十进制

int i = Integer.parseInt(binaryString, 2); // 2 indicates the radix here, since you want to convert from binary.
于 2013-11-04T12:06:51.863 回答
1

使用此代码段,您可以将字符串转换为整数:

int numberAsInt;
    try {
          numberAsInt = Integer.parseInt(number);
    } catch (NumberFormatException e) {
          //Will throw an Exception
    }

如果你想创建自己的异常类,你可以像这里展示的那样做,或者只是抛出一个 RuntimeException

throw new RuntimeException("Your Message");
于 2013-11-04T12:07:52.190 回答
1

我的意见,您可以尝试以下操作

 public static void main(String[] args) {    
  if(!isTrueZeckonderNumber()){
      // your message should goes here
       System.out.println("Your message");
    }
 }

如果您真的想引发异常,请执行以下操作

 private static boolean isTrueZeckonderNumber() throws Exception{
    System.out.println("Enter a Zeckonder Number:");
    number = scanner.nextLine();
    if (number.equals("10100")) {
        return true;
    } else{
        throw new Exception("your message");
    }        
}
于 2013-11-04T12:08:37.667 回答