[英]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.