我正在尝试为if (student == 1,2,3)
添加另一个错误
如果他们为他们正在服用的学分数输入0,则显示无效的输入消息。
对我做错的任何帮助?
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 :(得分:4)
这是编写if-else if-else语句的正确方法。你错过了“别的”。
if (student == 1)
{
}
else if (student == 2)
{
}
else if (student == 3)
{
}
else
{
}
答案 1 :(得分: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
答案 2 :(得分: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);
}
答案 3 :(得分:1)
您可能希望简化处理并将代码分解为多种方法。考虑以下组织:
然后,您可以将代码调整为更简单 - 例如。
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;
}
}
答案 4 :(得分:0)
你错过了所有ifs之间的“else”。所以你得到了这个流程:
if (1) { ... }
and if (2) { ... }
and if (3) { ... } else { ... }
你到处想要“或”逻辑。
答案 5 :(得分:0)
那是因为else会捕获一个与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
}