【发布时间】:2015-02-06 20:16:42
【问题描述】:
我能够让代码工作,下一个挑战是把它放在一个循环中多次工作,如果用户输入 -ve 数字或 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");
}
}
【问题讨论】:
-
throw new AgeException();- 为什么? -
它甚至可以编译吗?
if ((a.nextInt) > 18);是什么意思? -
你为什么不先告诉我们出了什么问题?你声称出了什么问题,但懒得提它是什么。此外,当您应该只使用 if 语句时,您似乎正在抛出异常。 异常行为例外。
-
正如@MarounMaroun 所说,
a无法在AgeException类中访问。
标签: java