我应该在哪里开始和结束while循环

时间:2015-02-06 20:16:43

标签: java

我能够让代码工作,下一个挑战是将它放在循环中多次工作,如果用户输入-ve number或0,循环应该结束。 我是一个完全的初学者,请不要诅咒我愚蠢的代码。

package Exceptions;

import java.util.Scanner;

public class usrDefined {

    public static void main(String[] args) {

        try {
            String s;
            int i;
            Scanner a = new Scanner(System.in);
            System.out.print("Enter your Name: ");
            s = a.nextLine();
            System.out.print("Enter your Age: ");
            i = a.nextInt();
            while((i = a.nextInt())!= -(i = a.nextInt()) && (i = a.nextInt())!= 0 ) 
            if (i < 18) {
                throw new AgeException();
            } 
            System.out.print("You are eligible for the deal " + s);
        } catch (AgeException e) {
            e.specifyException();

        }
    }
}

class AgeException extends Exception {
    public void specifyException() {
        System.out.println("Sorry,you are not eligible");
    }

}

4 个答案:

答案 0 :(得分:2)

我不确定该异常是为了实现什么,或者为什么甚至尝试过这种异常。如果您只想查看某个值是否小于18,则可以使用简单的if语句执行此操作:

String s;
int i;
Scanner a = new Scanner(System.in);
System.out.println("Enter your Name: ");
s = a.nextLine();
System.out.println("Enter your Age: ");
i = a.nextInt();

if (i < 18) {
    System.out.println("Sorry,you are not eligible");
    // presumably exit the application here as well?
} else {
    System.out.println("You are eligible for the deal!!!");
    // presumably continue with other logic here as well?
}

作为一般规则,永远不会使用逻辑流的异常。为此目的存在条件结构(通常为if语句)。应该使用异常来退出处于故障状态的方法或操作,以便使用该方法或操作的代码可以响应该故障状态。检查用户的年龄不是故障状态,它只是业务逻辑。


至于为什么你拥有的代码&#34;没有工作&#34;,有几个问题......

此行将立即退出代码块:

throw new AgeException();

这是因为它抛出异常。那条线将执行后 nothing 。代码将立即转到catch块,因此此处预期的行为将是用户永远不会被提示输入,并且总是立即告知他们有资格进行交易。

此外,这里有三个错误:

if ((a.nextInt) > 18);

第一个错误是该上下文中没有a变量。该变量位于main方法中。因此需要将其传递给specifyException方法才能使用它。

第二个错误是分号。除非编译器在语法上抱怨它(我不熟悉Java以确定),否则基本上会使整个if块无效,因为它代表一个空语句。因此,整个块转换为&#34;如果值大于18,则不执行任何操作&#34;。

第三个错误是您忘记了nextInt()的括号。

最后,即使您立即抛出异常,也无法调用specifyException方法。所以永远不会调用逻辑。如果你在何处或如何调用该方法似乎不直观,那是因为它处于异常状态并且使用逻辑流异常本质上是不直观和错误的:)

答案 1 :(得分:2)

这不是异常的工作方式。当您throw异常时,异常情况已经发生;你在报道它。因此,使用此代码,只要您输入try块,就会自动生成异常条件。此外,在异常类定义中的特殊specifyException方法中未指定条件。另外,当您捕获异常时,即处理异常条件时,而不是异常条件未发生时。

要使用AgeException

  1. 删除顶部的throw声明。
  2. 从用户输入中捕获年龄后,请使用正常if条件测试年龄。在if块内,抛出AgeException
  3. 的实例
  4. if阻止后,但仍在try区块中,打印您的“您符合条件”消息。如果未抛出异常,则用户“符合条件”。
  5. 捕获异常时,请打印“不符合条件”的消息。
  6. 但是,在这里使用例外似乎并不正确。某人的年龄小于18岁对我来说似乎不是一个特例。一个简单的if / else是一个更好的设计。

答案 2 :(得分:0)

不需要抛出异常或设计好。我想这就是你想要的:

 public static void main(String[] args) {
     Scanner a = new Scanner(System.in);
     System.out.println("Enter your Name: ");
     String s = a.nextLine();
     System.out.println("Enter your Age: ");
     int i = a.nextInt();
     if (ageOk(a)) {
         System.out.println("You are eligible for the deal!!!");
     }
}

private static boolean ageOk(int age) {
     return age > 18;
}

答案 3 :(得分:0)

我不确定使用异常处理此问题是否正确但您可以尝试:

UsrDefined.java

package exceptions;

import java.util.Scanner;

public class UsrDefined {



    public static void main(String[] args) {

    try{
         System.out.print("Enter your age:"); 
         Scanner a = new Scanner(System.in);
         checkAge(a);
    }catch (AgeException e){
         System.out.println(e.getMessage());
    }
}
    private static void checkAge(Scanner a) throws AgeException {

        if(a.nextInt() < 18) {
            throw new AgeException("You are not eligible for the deal!");
             }

    }


}

AgeException.java

package exceptions;

public class AgeException extends Exception {

    private static final long serialVersionUID = 1L;

    public AgeException(String msg) {
        super(msg);
    }

}