java - Java 中收到非法表达式开始错误

标签 java runtime-error performance-testing

只是希望对代码有一些指导。对于第一点(见下文),我试图创建一个实例方法,它几乎可以添加给定的值。我的问题是我不断收到表达式错误的非法开始。我想知道是否有人能够解释为什么,因为它对我来说看起来很好?请记住,我只发布了收到错误消息的代码部分。

编辑我实际上想重新打开这个问题一秒钟,因为无论出于何种原因我收到了一个奇怪的错误。我能够完成代码,但在测试时收到一条错误消息,指出 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/

相关文章:

java - 可以在 Room 数据库的构造函数中使用 @Ignore 字段吗?

Java KeyEvent.VK_???这是什么 key ?

java - 泛型将子类添加到数组

java - "Exception in thread "主要 "java.lang.ArrayIndexOutOfBoundsException: 178"错误,不知道为什么

python - 异步 : [ERROR] Task was destroyed but it is pending

scala - 试图理解 Scala 数组

java - Spring & Hibernate 的连接池

c++ - 使用自定义比较器排序时出现运行时错误

ruby-on-rails - 为什么我的 session 在使用 PerformanceTest 而不是 IntegrationTest 时会过期?

android - 使用 Android Development Studio 分析 Android 应用程序的性能