2014-02-22 94 views
0

我想为if (student == 1,2,3)添加另一个错误。
如果他们输入的信用额度为0,则显示无效的输入消息。Java:如果语句

任何帮助我做错了什么?

import javax.swing.*; 
import java.text.*; 

public class TuitionCost { 

    public static void main(String[] args) { 
    int costHours; 
    int student; 
    String input; 
    double a; 
    double b; 
    double c; 
    DecimalFormat dollar = new DecimalFormat("#,##0.00"); 

    JOptionPane.showMessageDialog(null, "OCC Tuition Cost Calculation Program", "Tuition Costs at OCC", JOptionPane.INFORMATION_MESSAGE); 
    input = JOptionPane.showInputDialog(null, "Are you a:\n1 - College District Residents\n2 - Non-Residents of College District\n3 - Out-of-State and International Students\n\nPlease enter 1, 2 or 3:", "Input", JOptionPane.QUESTION_MESSAGE); 
    student = Integer.parseInt(input); 

    if (student == 1) { 
     input = JOptionPane.showInputDialog(null, "How many credit hours are you taking?", JOptionPane.QUESTION_MESSAGE); 
     costHours = Integer.parseInt(input); 
     a = costHours * 76.40; 
     JOptionPane.showMessageDialog(null, dollar.format(costHours) + " hours at $76.40 per hour yields a tuition of $" + dollar.format(a)); 
     System.exit(0); 
    } 

    if (student == 2) { 
     input = JOptionPane.showInputDialog(null, "How many credit hours are you taking?", JOptionPane.QUESTION_MESSAGE); 
     costHours = Integer.parseInt(input); 
     b = costHours * 139.10; 
     JOptionPane.showMessageDialog(null, dollar.format(costHours) + " hours at $139.10 per hour yields a tuition of $" + dollar.format(b)); 
     System.exit(0); 
    } 

    if (student == 3) { 
     input = JOptionPane.showInputDialog(null, "How many credit hours are you taking?", JOptionPane.QUESTION_MESSAGE); 
     costHours = Integer.parseInt(input); 
     c = costHours * 195.15; 
     JOptionPane.showMessageDialog(null, dollar.format(costHours) + " hours at $195.15 per hour yields a tuition of $" + dollar.format(c)); 
     System.exit(0); 
    } else { 
     JOptionPane.showMessageDialog(null, "You must enter a 1, 2, or 3.\nPlease Run the program again.", "Invalid Input", JOptionPane.ERROR_MESSAGE); 
    } 
    System.exit(0); 
    } 
} 
+0

'如果(学生== 0){}'怎么样。 – BitNinja

+0

'else'是最后一个'if'的一部分。 – devnull

+0

请修复代码格式和缩进。 –

回答

0

你错过了所有ifs之间的“else”。所以,你得到这个流程:到处

if (1) { ... } 
and if (2) { ... } 
and if (3) { ... } else { ... } 

你想“或”逻辑。

+0

你想简化和避免所有这些if/else结构。看看这个问题,你会发现学生类型只控制每小时的信用成本,并且它必须是1,2或3.所以:使用一个以student为数组的数组作为索引;和一个输入法,您可以给出最小值和最大值来检查错误。这样,您可以重复使用其他项目的输入法(例如0到25之间的信用小时数),例如 – ErstwhileIII

0

这是因为人会赶上的值是1,2或3种不同:

else    
    JOptionPane.showMessageDialog(null, "You must enter a 1, 2, or 3.\nPlease Run the program again.", "Invalid Input", JOptionPane.ERROR_MESSAGE); 
    System.exit(0); 

您需要添加一个条件,以检查0:

if (student == 0){} 

,并使用else-if语法:

if(student==0){ 
}else if(student==1){ 
}else if(student==2){ 
}else if(student==0){ 
}else{ 
//anything else 
} 
+0

如果可以通过其他方式减少问题,则不要使用复杂项。另外,考虑循环时要求有效的输入,而不是中止并让用户从头开始。 – ErstwhileIII

4

这是编写if-else if-else语句的正确方法。你错过了“其他”。

if (student == 1) 
{ 

} 
else if (student == 2) 
{ 

} 
else if (student == 3) 
{ 

} 
else 
{ 

} 
+0

你真的想要识别所有学生“类型”使用的模式,并做一次工作..不是多次输入全部(我的键盘不拼写,所以我可以在第二或第三个副本上创建错误东东)。 – ErstwhileIII

2

这是更好地使用switch如果你有一个可能的值:

switch (student) { 
    case 0: 
     //do stuff 
     break; 
    case 1: 
     //do stuff 
     break; 
    ... 
    default: 
     //it's analog of else 
     //do stuff 
     break; 
} 

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

+0

再次..认识到学生“类型”之间有什么不同,在这种情况下,只有小时成本是不同的。因此,最佳做法是创建一个小时成本数组并确定该数组的索引......然后您只需编写一次其余的语句。 – ErstwhileIII

1

我建议以下这个代码DRY原则。将税率声明为double[]although Double is not recommended for money),然后使用该数组获取每种特定类型学生的值。每个学生的代码非常相似,以至于一个简单的数组将允许主逻辑被写入一次。

double[] rates = {76.40,139.10,195.15}; 
student = Integer.parseInt(input); 

if(student > 0 && student < 4){ 
    input = JOptionPane.showInputDialog(null, "How many credit hours are you taking?", JOptionPane.QUESTION_MESSAGE); 
    costHours = Integer.parseInt(input); 
    if(costHours != 0){ 
     a = costHours * rates[student]; 
     JOptionPane.showMessageDialog(null, dollar.format(costHours) + " hours at $" + rates[student] + " per hour yields a tuition of $" + dollar.format(a)); 
    }else{ 
     JOptionPane.showMessageDialog(null, "Credit hours cannot be zero.", "Invalid Input", JOptionPane.ERROR_MESSAGE); 
    } 
    System.exit(0); 
}else{ 
    JOptionPane.showMessageDialog(null, "You must enter a 1, 2, or 3.\nPlease Run the program again.", "Invalid Input", JOptionPane.ERROR_MESSAGE); 
} 

Gist of Full File

1

你可能希望简化处理和要素你的代码的几种方法。请看下面的组织:

  1. 将根据学生居住的每小时成本到一个数组
  2. 创建方法来显示一个消息
  3. 创建一个方法来问一个问题,得到一个int响应,并检查响应在最小和最大之间

然后,您可以调整您的代码以使其更简单 - 例如。

package com.snippet; 

import java.text.DecimalFormat; 
import javax.swing.JOptionPane; 

public class TuitionCostNew { 
private static TuitionCostNew me; 

/** cost per hour for: 
* college district resident, 
* in-state but not college district, 
* out of state (including international) 
* 
*/ 
private static double[] hourCost = { 76.40, 139.10, 195.15 }; 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    me = new TuitionCostNew(); 
    me.start(hourCost); 
} 

/** 
* @param hourCost 
*/ 
private void start(double[] hourCost) { 
    int studentType; 
    int creditHours; 
    double tuitionCost; 
    DecimalFormat dollar = new DecimalFormat("#,##0.00"); 

    showInformationMessage("OCC Tuition Cost Calculation Program","Tuition Costs at OCC"); 

    studentType = inputIntegerDialog(
      "Are you a:\n1 - College District Residents\n2 - Non-Residents of College District\n3 - Out-of-State and International Students\n\nPlease enter 1, 2 or 3:", 
      1, 3); 

    creditHours = inputIntegerDialog("How many credit hours are you taking?", 1, 25); 
    tuitionCost = hourCost[studentType-1] * creditHours; 
    showMessage(dollar.format(creditHours) + " hours at " 
      + hourCost[studentType-1] + " per hour yields a tuition of $" 
      + dollar.format(tuitionCost)); 
} 

/** Show user an informational message pane, including a title for the pane and a message. 
* @param title 
* @param message 
*/ 
private void showInformationMessage(String title, String message) { 
    // TODO Auto-generated method stub 
    JOptionPane.showMessageDialog(null, title, message, 
      JOptionPane.INFORMATION_MESSAGE); 
} 

/** Shoe user a simple message 
* @param message 
*/ 
private void showMessage(String message) { 
    JOptionPane.showMessageDialog(null, message); 
} 

/** Ask the user to enter an integer value using the message, where the value must be between min and max 
* @param message 
* @param min 
* @param max 
* @return 
*/ 
private int inputIntegerDialog(String message, int min, int max) { 
    String input; 
    int value = -1; 
    boolean validAnswer = false; 

    while (!validAnswer) { 
     input = JOptionPane.showInputDialog(null, message, "Input", 
       JOptionPane.QUESTION_MESSAGE); 
     value = Integer.parseInt(input); 
     validAnswer = value >= min && value <= max; 
     if (!validAnswer) { 
      String errMessage = "Invalid entry. Enter number between " 
        + min + " and " + max + ". Try again."; 
      showMessage(errMessage); 
     } 
    } 
    return value; 
} 

}

+1

你可能想告诉OP为什么他们的代码不起作用(IE回答这个问题)。这不是[代码评论](http://codereview.stackexchange.com/)。你在做什么,张贴批评他人的答案,因为他们没有提出一个完全不同的选择,这是一种讨厌。 – Radiodef