If statement 需要使用JOptionPane和if/if-else提供多个答案
创建一个简单的教学计划,我就陷入了(我认为是)收尾阶段 问题是,我不知道如何计算“小时”变量,使其在声明后始终保持不变 这是我使用Java的第一年,请原谅我的糟糕风格If statement 需要使用JOptionPane和if/if-else提供多个答案,if-statement,joptionpane,If Statement,Joptionpane,创建一个简单的教学计划,我就陷入了(我认为是)收尾阶段 问题是,我不知道如何计算“小时”变量,使其在声明后始终保持不变 这是我使用Java的第一年,请原谅我的糟糕风格 import java.util.*; import javax.swing.JOptionPane; /** The application calculates the cost of tuition for one semester at OCC. */ public class TuitionOCC { publ
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);
}
}
}
import java.util.*;
导入javax.swing.JOptionPane;
/**
该应用程序计算OCC一学期的学费。
*/
公共课学费
{
公共静态void main(字符串[]args)
{
字符串输入;
国际居住;
字节信用;
国际学生公寓学费;
int非图腾;
国际学费;
整小时;
双倍学费;
剩余学费=82;
非图腾=154;
国际学费=216;
showMessageDialog(null,“计算学费!”,“学费”,JOptionPane.INFORMATION\u消息);
input=JOptionPane.showInputDialog(“您是:\n”+
“1-学院区居民\n”+
“2-大学区的非居民\n”+
“3-国外或国际学生”);
常驻=整数.parseInt(输入);
如果(居住时间==1)
{
input=JOptionPane.showInputDialog(“你修了多少学时?”:”;
小时=整数.parseInt(输入);
如果(hours如果要使小时数恒定,则可以执行以下操作:
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);
}
}
}
}
import java.util.*;
导入javax.swing.JOptionPane;
/**
该应用程序计算OCC一学期的学费。
*/
公共课学费{
公共静态void main(字符串[]args){
字符串输入;
国际居住;
字节信用;
国际学生公寓学费;
int非图腾;
国际学费;
双倍学费;
剩余学费=82;
非图腾=154;
国际学费=216;
showMessageDialog(null,“计算学费!”,“学费”,JOptionPane.INFORMATION\u消息);
input=JOptionPane.showInputDialog(“您是:\n”+
“1-学院区居民\n”+
“2-大学区的非居民\n”+
“3-国外或国际学生”);
常驻=整数.parseInt(输入);
input=JOptionPane.showInputDialog(“你修了多少学时?”:”;
最终整数小时数=整数.parseInt(输入);
如果(小时)