2016-08-03 24 views
-7

次の割り当てには次のコードを使っていますが、 switch文は、私が正しく書き直すのを助けてください。switch文にelsesが書かれている場合、私はこのコードをelsesと書いて、スイッチとして書き直す必要があります。

ABCコミュニティ病院は、患者の請求書を計算して印刷するプログラムが必要です。 1日あたりのABCコミュニティ病院

ルーム料金

: プライベート$ 550.0 セミプライベート$ 350.0 ウォード$ 105.00

電話$ 4.50

テレビ次の表は、さまざまなサービスのための手数料を示し$ 7.50

薬$ 275.00

以下のインジケータを受け入れるHospitalというクラスを作成します。 •患者の名前(名と姓) •患者が病院にいる​​日数を表す整数。 •患者が選択した部屋のタイプを表す1文字。部屋のタイプ(P/P =プライベート、S/Sはセミプライベート、W/Wは病棟を意味します)

請求の一部として、次の点に注意してください。 •患者がプライベートルームを選択すると、投薬については、請求チャートの2倍です。患者がセミプライベートを選択した場合、投薬コストは請求チャートに示されたものとなる。患者が病棟を選択した場合、請求費用は請求チャートの半分になります。 •プライベートルームを選択した患者は、テレビサービスおよび電話サービスに料金を支払う。セミプライベートルームを選択した患者はテレビサービスを払い、その病棟を選択した患者はテレビと電話サービスを無料で受ける。

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(); 



     } 



    } 

答えて

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文を見にどの項目を決定し、その後、それぞれの場合には、それぞれの結果のためです。一致するケースがなければ、コードはデフォルトのケースを実行します。休憩もかなり重要です。私が正しく理解している場合、休憩なしに、あなたのコードはすべてのケースのコードを実行します。この実装が必要な場合は便利ですが、おそらくここにはないようです。スイッチケースの

詳細情報はここで見つけることができます:イムつもりでも、それと仕事と私は何を得る見てみ https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

+0

おかげで私は本当に、答え、助けるために時間を割いてくださったことを感謝したいと思いました私は負のポイントを与えるために読むだけでpplを理解していない.... – hhhh

+2

問題はない、私は助けることができてうれしい!下降音は、この質問がほぼ確実に存在し、switch文がGoogleで見つけられないという事実から来る可能性が高い。しかし、私は同じことをやったにもかかわらず人々が私を助けてくれたという事実を知っています。あなたに魚を与える方法を教えるために、もしあなたがJavaで何かをする方法を見つけるのが難しいなら、私は "java <困ったこと> api"が始めるのに最適な検索であることを発見しました。少なくとも、それは通常、あなたがあなたの検索を家に持ち出すのに十分な理解を得るのを助けるはずの場所を指します。 –

+0

これは私が思いついた.... – hhhh

関連する問題