在Java中接收到表达式非法启动错误



只是希望获得一些代码方面的指导。对于第一部分(见下文),我正在尝试创建一个实例方法,该方法几乎可以添加给定的值。我的问题是,我一直得到一个非法的表达式开头错误。我想知道是否有人能解释为什么,因为我觉得这很好?请记住,我只发布了代码中收到错误消息的部分。

编辑我实际上想再打开这个问题一秒钟,因为无论出于什么原因,我都收到了一个奇怪的错误。我能够完成代码,但当我测试它时,我收到了一条错误消息,上面写着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' ) {

使用&&代替||,并使用'单引号检查字符。

最新更新