我能得到的代码工作,下一个挑战是把它放在一个循环工作多次,循环,如果用户输入-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");
}
}
'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