2014-10-22 198 views
0

我的問題是如何將char轉換爲布爾值。比如我有2類如何將char轉換爲布爾值?

一個是隻有bleedinghistory方法和病人的信息類:

Public class Patient { 

     private static String name; 
     private static boolean bleedingHistory = true; 
     private String answer; 
     private double ureaLevel; 
     private double proLevel; 


     public Patient() 
     { 
      String name = "unknown"; 
      double ureaLevel = 0.0; 
      double proLevel = 0.0; 
      boolean bleedingHistory = false; 

     } 

     public Patient (String inName, boolean inBleedingHistory) 
     { 
      if (name != "" || bleedingHistory == true) 
      { 
       name = inName; 
       bleedingHistory = inBleedingHistory; 

      } 

      else 
      { 
       name = "unknown"; 
       bleedingHistory = false;  
      } 

      proLevel = 0.0; 
      ureaLevel = 0.0; 
     } 
    public boolean getBleedingHistory() 
      { 
       return bleedingHistory; 

      } 

      public void setBleedingHistory(boolean inBleedingHistory) 
      { 

       bleedingHistory = inBleedingHistory; 
      } 

和另一級呼叫patientcheck是要求用戶輸入患者是否有出血史的一部分:

System.out.print("Does the patient has a history of bleeding(Yes or No)?: "); 
     char inBleedingHistory; 
     do 
     { 
      inBleedingHistory = sc.next().charAt(0); 
      if (inBleedingHistory != 'n' && inBleedingHistory != 'N' && inBleedingHistory != 'y' && inBleedingHistory != 'Y') 
      {System.out.println("Invalid answer, please enter YES or NO: "); 

      sc.nextLine(); 
      } 

     }while (inBleedingHistory != 'n' && inBleedingHistory != 'N' && inBleedingHistory != 'y' && inBleedingHistory != 'Y'); 

     pat1 = new Patient (inName, inBleedingHistory); 

我知道我的病歷檢查部分的歷史出血是錯誤的,但我能做些什麼來糾正它?感謝

+0

'布爾charIsY =(CH == 'Y' || CH == 'Y');' – khelwood 2014-10-22 09:14:19

回答

0
pat1 = new Patient (inName, Character.toString(inBleedingHistory).equalsIgnoreCase("y")); 

如果inBleedingHistory匹配Ÿ這將返回true或Y.它還將比較之前轉換字符爲String。

你也可以將它轉換爲使用

pat1 = new Patient (inName, String.valueOf(inBleedingHistory).equalsIgnoreCase("y")); 

這是同一個字符串。

您還可以比較可能是最快方式的字符。

Character yesLower, yesUpper; 

yesLower = new Character('y'); 
yesUpper = new Character('Y'); 

pat1 = new Patient (inName, (inBleedingHistory.compareTo(yesLower) == 0 || inBleedingHistory.compareTo(yesUpper) == 0)); 
+0

也可以使用'equalsIgnoreCase'計數y的/ Y的差異。 – Holloway 2014-10-22 09:14:49

+2

你必須在'char'上使用'==',而不是'equals' – khelwood 2014-10-22 09:14:53

+0

修復它。強制轉換爲字符串或使用字符串。 – 2014-10-22 09:17:05

1
pat1 = new Patient(inName, (inBleedingHistory=='y' || inBleedingHistory=='Y')); 
+0

哦,所以我可以做到這一點?我從來沒有想過這個 – 2014-10-22 12:22:51

+0

是的,你可以。請接受答案,如果它做到了你想要的。 – khelwood 2014-10-22 12:46:17