只是希望对代码有一些指导。对于第一点(见下文),我试图创建一个实例方法,它几乎可以添加给定的值。我的问题是我不断收到表达式错误的非法开始。我想知道是否有人能够解释为什么,因为它对我来说看起来很好?请记住,我只发布了收到错误消息的代码部分。
编辑我实际上想重新打开这个问题一秒钟,因为无论出于何种原因我收到了一个奇怪的错误。我能够完成代码,但在测试时收到一条错误消息,指出 IllegalArgumentException: 选择的字母无效。必须是 T、D 或 E。给定的数据 84 将被忽略。我想知道为什么会出现错误消息,因为我选择了这些字母并且根本没有使用 84 作为数字。
public int addNewValue ( char amtType, int amount) {
Scanner keyboard= new Scanner (System.in);
System.out.println("Please enter the amount in dollars");
amount=keyboard.nextInt();
System.out.println("Please select the amount Type: T-Ticket Sales,"
+ " D-Donations, E-Expenses");
amtType=keyboard.next().charAt(0);
if (amount<=0) {
throw new IllegalArgumentException("Amount must be positive and "
+ "non-zero. The given data " +amount+ " will be ignored.");
//below is where I recieve the error message
}else if (amtType !=T&&!=D&&!=E ) {
throw new IllegalArgumentException ("The letter chosen is invalid."
+ "Must be T, D, or E. The given data " +amtType+
" will be ignored");
}else{
return amount + amtType;
}
}
最佳答案
尝试这样编写 if 条件:
}else if (amtType !='T' && amtType !='D' && amtType !='E' ) {
使用 &&
而不是 ||
并使用 '
单引号来检查字符。
关于java - Java 中收到非法表达式开始错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36726273/