创建一个简单的学费计划,我陷入困境(我认为是最后的接触)。
问题是,我不确定如何计算“小时”变量,以便在所有内容保持不变的情况下。
这是我使用Java的第一年,请原谅我的坏风格。
import java.util.*;
import javax.swing.JOptionPane;
/**
The application calculates the cost of tuition for one semester at OCC.
*/
public class TuitionOCC
{
public static void main(String[] args)
{
String input;
int residency;
byte credits;
int residentTuition;
int nonTuition;
int internationalTuition;
int hours;
double tuition;
residentTuition = 82;
nonTuition = 154;
internationalTuition = 216;
JOptionPane.showMessageDialog(null, "Calculate your tuition!", "Tuition",JOptionPane.INFORMATION_MESSAGE);
input = JOptionPane.showInputDialog("Are you a:\n" +
"1- College District Resident\n" +
"2- Non-Resident of College District\n" +
"3- Out-of-State or International Student");
residency = Integer.parseInt(input);
if (residency == 1)
{
input = JOptionPane.showInputDialog("How many credit hours are you taking?: ");
hours = Integer.parseInt(input);
if (hours <=0)
JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
}
else if (residency == 2)
{
residency = nonTuition;
input = JOptionPane.showInputDialog("How many credit hours are you taking?: ");
hours = Integer.parseInt(input);
if (hours <=0)
JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
}
else if (residency == 3)
{
residency = internationalTuition;
input = JOptionPane.showInputDialog("How many credit hours are you taking?: ");
hours = Integer.parseInt(input);
if (hours <=0)
JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "You must enter a 1, 2, or 3\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
tuition = hours * residency;
if
(residency == 1)
{
JOptionPane.showMessageDialog(null, hours + " hours at $ " + residentTuition + "per hour yields a tuition of " + tuition);
}
else if
(residency == 2)
{
JOptionPane.showMessageDialog(null, hours + " hours at $ " + nonTuition + "per hour yields a tuition of " + tuition);
}
else if
(residency == 3)
{
JOptionPane.showMessageDialog(null, hours + " hours at $ " + internationalTuition + "per hour yields a tuition of " + tuition);
}
}
}
答案 0 :(得分:0)
如果你想让时间不变,那么你可以这样做:
input = JOptionPane.showInputDialog("How many credit hours are you taking?: ");
final hours = Integer.parseInt(input);
您还可以将JOptionPane放在if语句之上,这样就不会在每个if语句块上重新分配值。 同样,您可以对代码执行此操作以缩短所有内容。
import java.util.*;
import javax.swing.JOptionPane;
/**
The application calculates the cost of tuition for one semester at OCC.
*/
public class TuitionOCC {
public static void main (String[] args) {
String input;
int residency;
byte credits;
int residentTuition;
int nonTuition;
int internationalTuition;
double tuition;
residentTuition = 82;
nonTuition = 154;
internationalTuition = 216;
JOptionPane.showMessageDialog(null, "Calculate your tuition!", "Tuition",JOptionPane.INFORMATION_MESSAGE);
input = JOptionPane.showInputDialog("Are you a:\n" +
"1- College District Resident\n" +
"2- Non-Resident of College District\n" +
"3- Out-of-State or International Student");
residency = Integer.parseInt(input);
input = JOptionPane.showInputDialog("How many credit hours are you taking?: ");
final int hours = Integer.parseInt(input);
if (hours <= 0) {
JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
tuition = hours * residency;
if (residency == 1) {
JOptionPane.showMessageDialog(null, hours + " hours at $ " + residentTuition + "per hour yields a tuition of " + tuition);
} else if (residency == 2) {
JOptionPane.showMessageDialog(null, hours + " hours at $ " + nonTuition + "per hour yields a tuition of " + tuition);
} else if (residency == 3) {
JOptionPane.showMessageDialog(null, hours + " hours at $ " + internationalTuition + "per hour yields a tuition of " + tuition);
} else {
JOptionPane.showMessageDialog(null, "You must enter a 1, 2, or 3\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
}