我应该从哪里开始和结束while循环这里
我能得到的代码工作,下一个挑战是把它放在一个循环工作多次,循环,如果用户输入-ve号或0应该结束。 我是一个完整的初学者,请不要骂我愚蠢的代码。我应该从哪里开始和结束while循环这里
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");
}
}
我不确定那个例外意味着什么或者甚至有人尝试过。如果你只是希望检查一个值小于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
声明)就是为此目的而存在的。应该使用异常退出处于故障状态的方法或操作,以便使用该方法或操作的代码可以响应该故障状态。检查用户的年龄不是故障状态,它只是业务逻辑。
至于为什么你有“不工作”的代码,有几个问题...
这条线将立即退出的代码块:
throw new AgeException();
这是因为它引发异常。所以没有什么后,该行将执行。该代码将立即转到catch
区块,因此期望的行为将是从未提示用户输入,并且始终立即告知用户符合交易条件。
此外,有三个错误的位置:
if ((a.nextInt) > 18);
第一个错误是,有在这方面没有a
变量。该变量位于main
方法中。所以它需要传递给那个specifyException
方法来使用它。
第二错误是分号。除非编译器会在语法上抱怨它(我对Java的了解还不够熟悉),它基本上会使整个if
块无效,因为它代表一个空语句。所以整个模块转换为“如果数值大于18,则什么也不做”。
第三个错误是您忘记了nextInt()
的括号。
最后,即使你不要立即抛出异常,你无处可以拨打specifyException
方法。所以这个逻辑永远不会被调用。如果对于你在哪里或如何调用这种方法似乎很不直观,那是因为它处于例外状态,并且使用逻辑流的异常本质上是不直观和错误的:)
这不是异常情况。当你throw
出现异常时,异常情况已经发生;你正在报告它。因此,使用此代码,只要输入try
块,就会自动生成异常条件。此外,该条件未在异常类定义中的特殊specifyException
方法中指定。另外,当你捕捉到异常时,就是当异常情况被处理时,而不是在没有发生异常情况时。
为了让您的AgeException
这方面的工作:
- 顶部取下
throw
声明。 - 一旦您从用户输入中捕获了年龄,然后使用正常的
if
条件测试年龄。在if
区块内,抛出一个AgeException
的实例。 - 在
if
块之后,但仍在try
块中,打印出您的“您有资格”消息。如果没有抛出异常,则用户是“合格的”。 - 捕捉异常时,打印“不符合条件”的消息。
但是,在这里使用例外似乎并不正确。年龄小于18岁的人对我来说似乎不是特别的条件。一个简单的if/else在这里是一个更好的设计。
谢谢你协助rgettman。我知道异常不是很好的匹配,但这是我的教授给出的一个小小的任务 – 2015-02-06 20:43:29
我猜想,所以我提供了关于如何使用AgeException工作的技巧。 – rgettman 2015-02-06 20:46:25
抛出一个异常是不需要或不错的设计。我认为这是你想要什么:
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;
}
我不知道如果这handleing使用的例外是正确的,但你可以试试这个:
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);
}
}
抛出新的AgeException(“你没有资格达成交易!”);没有可以访问UsrDefined类型的封闭实例。必须使用封闭的UsrDefined类型的实例来限定分配(例如,x.new A(),其中x是UsrDefined的实例)。 – 2015-02-06 21:21:17
'throw new AgeException();' - 为什么? – Maroun 2015-02-06 20:17:58
它甚至编译?你是什么意思?if((a.nextInt)> 18);'? – Maroun 2015-02-06 20:18:57
为什么不先告诉我们什么是错的?你声称有什么事情出错了,但没有提及它是什么。另外,当你只是使用if语句时,看起来好像你正在抛出一个异常。例外是*特殊行为*。 – tnw 2015-02-06 20:18:59