繁体   English   中英

IllegalArgumentException在Java中无法正常运行

[英]IllegalArgumentException Not functioning Correctly in Java

因此,抛出非法异常对我来说是新的,我一直希望在其中提出一些建议。 我编写的代码将确定事件中产生或损失的金额,但是,测试时我在代码中遇到2个错误。 第一个就是无论我输入什么,我都会收到IllegalArgumentException错误消息“线程“ main”中的异常” java.lang.IllegalArgumentException :所选字母无效。必须为T,D或E。给定的数据T将被忽略。” 我不太确定为什么会这样,甚至消息说我使用的是正确的字母。

我遇到的第二个问题是我的程序实际上没有在我的显示方法中显示值(除0外)。 我觉得我有一个正确的主意,所以我想也许我在其他地方做错了。 我确实在需要时附加了整个代码,但是我在错误所在的地方留下了注释。

public class EdmondEventManager2 {

        /**Purpose of code is to determine the amount of money generated or  lost in an event
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            EdmondEventClass object= new EdmondEventClass();

            object.addNewValue(amountType(),validateAmount());
            object.displayResults();
        }

        /*static method that will read and validate amount type T-ticket sales, D-Donation, E-expenses */
        public static char amountType (){
            Scanner keyboard= new Scanner (System.in);
            System.out.println("Please select the amount Type: T-Ticket Sales,"
                         + " D-Donations, E-Expenses");
            char amtType=keyboard.next().charAt(0);
            amtType=Character.toUpperCase(amtType);

            switch(amtType)
            {
                case 'T' &'t':
                    break;
                case 'D'& 'd'  :
                    break;
                case 'E' &'e' :
                    break;
            }

            if (amtType !='T'&& amtType!='D'&& amtType!='E'&&amtType!='t'
                  &&amtType!='d'&&amtType!='e'){
              do {       
                  System.out.println("Choice invalid. Please select T, D, or E");
                  System.out.println("Enter amount type: T, D, or E.");
                  amtType=keyboard.next().charAt(0);   
              }while (amtType!='T'&&amtType!='D'&&amtType!='E');
              return amtType= Character.toUpperCase(amtType);
            }

            return amtType= Character.toUpperCase(amtType);
      }

      /*static method that will read and validate the amount. I believe te first  issue is here */
      public static double validateAmount (){
         Scanner keyboard= new Scanner (System.in);
         System.out.println("Please enter the amount in dollars");
         double amount=keyboard.nextInt();

         if (amount<=0){
             do {       
                 System.out.println("Amount must be a positive number!"
                     + " Try Again");
                 System.out.println("Please enter the amount in dollars");
                 amount=keyboard.nextInt();   
            }while (amount<=0);
                 return amount;
         }

         return amount;                
      }

      public static class EdmondEventClass{
        private int T;
        private int D;
        private int E;

        //constructors   
        public EdmondEventClass (){    
            T=0;
            D=0;
            E=0;    
        }

        //getters
        public int getTotalIncomeSale () {
            return this.T;
        }

        public int getTotalDonatins () {
            return this.D;
        }

        public int getTotalExpenses (){
            return this.E;
        }

        /*Instance method that will add a new value to one of the totals
         *@char amtType- One of the letters the user must chose. T-ticket sales,
          D-Donation, E-expenses
        *@ double amount- the amount for the chosen amtType.    
        */
        public double addNewValue ( char amtType, double amount) {

            if (amount<=0) {
                 throw new IllegalArgumentException("Amount must be positive and "
               + "non-zero. The given data " +amount+ " will be ignored.");
            }
            else if (amtType !=T&& amtType!=D&& amtType!=E ) {
                throw new IllegalArgumentException ("The letter chosen is invalid."
                   + "Must be T, D, or E. The given data " +amtType+ 
                   " will be ignored");
            }

            return amount + amtType;
        }

        //Will display the outcome of the event. I believe the second issue is here.
        public double  displayResults (){
            System.out.println ("Event Overall Outcome:");
            System.out.println ("Total ticket sales:     "+T);
            System.out.println ("Total donations:        "+D+ "  +");
            System.out.println ("                     -------");
            System.out.println ("Total income:    ");
            System.out.println ("Total expense:          "+E+ "  -");
            System.out.println ("                     -------");
            System.out.println ("Event profits:    ");
            String numberString=String.format ("%8.2f");
            return T+D;    
        }
    }
}

您的参数是一个char,但是您似乎正在根据int对其进行检查,因此方法addNewValue只是没有意义。 您应该只将chars与chars和ints与ints进行比较。

附带建议:请理解,代码格式非常重要,而不是可选的,或者应谨慎进行。 如果您在这里需要最好的帮助,请努力使您的代码尽可能易于阅读和理解。 另外,您将要学习并遵循Java命名约定,包括使用小写字母开头的变量名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM