我正在为以下任务编写以下代码,但我最难重写为switch语句,请帮我正确写。
ABC社区医院需要一个程序来计算和打印每位患者的账单。下表显示了各种服务的费用:
ABC社区医院
每日房费: 私人550.0美元 半私人350.0美元 病房$ 105.00
电话$ 4.50
电视$ 7.50药物$ 275.00
写一个名为Hospital的类,它接受以下指标: •患者姓名(名字和姓氏) •一个整数,表示患者在医院的天数。 •表示患者选择的房间类型的单个字符。房间类型(P / p =私人,S / s表示半私人,W / W表示病房)
作为结算的一部分,请注意以下事项: •如果患者选择私人房间,则药物费用是计费表上显示的两倍;如果患者选择半私人,那么药物费用就是计费表上显示的;如果患者选择病房,则计费成本是计费表上显示的一半。 •选择私人房间的患者支付电视服务和电话服务的费用;选择半私人房间的病人支付电视服务费用,选择病房的病人可以免费获得电视和电话服务。
import java.text.NumberFormat;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Andrea
*/
public class hospital {
private double roomCharges;
private double telephone;
private double television;
private double medication;
private static final NumberFormat nf = NumberFormat.getCurrencyInstance();
private double totalCharges;
private String header;
private String s;
char roomType;
int noOfdays;
public hospital() {
totalCharges = 0.0;
s = "";
header = "\tThe ABC Community Hospital\n"
+ "\t Patient Billing Statement\n\n";
}
hospital (int i, char p )
{
noOfdays = i;
roomType = p;
}
public void getBillingStatement() {
JTextArea b = new JTextArea("\tThe ABC Community Hospital\n\n"
+ "Room Charges:\n\tPrivate\t\t$550.0\n\tSemi-Private\t\t$350.0\n\tWard\t\t"
+ "$105.0\n\nTelephone\t\t\t$4.50\n\nTelevision\t\t\t$7.50\n\n"
+ "Medication\t\t\t$275.00", 14, 35);
JScrollPane p1 = new JScrollPane(b);
JOptionPane.showMessageDialog(null, p1, "Charges",
JOptionPane.INFORMATION_MESSAGE);
int days = Integer.parseInt(JOptionPane.showInputDialog("Days patient has been in the Hospital:"));
if (days > 0) {
s = s + "Number of days in hospital:\t\t" + days + "\n";
} else {
s = s + "Number of days in hospital must be positive!\n";
days = 0;
}
String room = JOptionPane.showInputDialog("Type of room: (P,S,W)");
switch (room.toUpperCase()) {
case "P":
if (room.equalsIgnoreCase("P"))
roomCharges = 550.0;
s = s + "Type of room:\t\t\tPrivate\n\n"
+ "Room Charge:\t\t\t" + nf.format(days * roomCharges) + "\n";
totalCharges = totalCharges + (days * roomCharges);
break;
case "S":
if (room.equalsIgnoreCase("S"))
roomCharges = 350.0;
s = s + "Type of room:\t\t\tSemi-Private\n\n"
+ "Room Charge:\t\t\t" + nf.format(days * roomCharges) + "\n";
totalCharges = totalCharges + (days * roomCharges);
break;
case "W" :
if (room.equalsIgnoreCase("W"))
roomCharges = 105.0;
s = s + "Type of room:\t\t\tWard\n\n"
+ "Room Charge:\t\t\t" + nf.format(days * roomCharges) + "\n";
totalCharges = totalCharges + (days * roomCharges);
default:
roomCharges = 0.0;
s = s + "Please select a valid room type\n\n"
+ "Room Charge:\t\t\t" + nf.format(days * roomCharges) + "\n";
totalCharges = totalCharges + (days * roomCharges);
break;
}
String phone = JOptionPane.showInputDialog("Telephone: (Y/N)");
if (phone.equalsIgnoreCase("Y")) {
telephone = 4.50;
s = s + "Telephone:\t\t\t" + nf.format(telephone) + "\n";
totalCharges = totalCharges + telephone;
} else if (phone.equalsIgnoreCase("N")) {
telephone = 0.0;
s = s + "Telephone:\t\t\t" + nf.format(telephone) + "\n";
totalCharges = totalCharges + telephone;
} else {
telephone = 0.0;
s = s + "Invalid telephone option\n";
totalCharges = totalCharges + telephone;
}
String tv = JOptionPane.showInputDialog("Television: (Y/N)");
if (tv.equalsIgnoreCase("Y")) {
television = 7.50;
s = s + "Television:\t\t\t" + nf.format(television) + "\n";
totalCharges = totalCharges + television;
} else if (tv.equalsIgnoreCase("N")) {
television = 0.0;
s = s + "Television:\t\t\t" + nf.format(television) + "\n";
totalCharges = totalCharges + television;
} else {
television = 0.0;
s = s + "Invalid television option\n";
totalCharges = totalCharges + television;
}
JOptionPane.showMessageDialog(null, "Medication will be charged as well");
medication = 275.0;
s = s + "Medication:\t\t\t" + nf.format(medication) + "\n";
totalCharges = totalCharges + medication;
s = s + "Total Charges:\t\t\t" + nf.format(totalCharges) + "\n";
}
public static void main(String[] args) {
hospital i = new hospital(20, 's');
i.getBillingStatement();
}
}
答案 0 :(得分:2)
你用这样的东西切换它(看我在那里做了什么?):
switch(room.toUpperCase()) {
case "P":
//do stuff for P
break;
case "S":
//do stuff for S
break;
.....
default:
//do what's in your "else" block
break;
}
switch语句决定查看哪个项目,然后每个案例都针对每个结果。如果您的案例都不匹配,则代码将运行默认案例。休息时间也非常重要。如果我理解正确,没有中断,您的代码将执行下面的每个案例代码并包括匹配的案例。如果你想要这个实现,这可能很方便,但看起来你可能不会在这里。
有关switch-case的更多信息,请访问: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html