Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将字符更改为布尔值?_Java_Char_Boolean_Setter_Getter - Fatal编程技术网

Java 如何将字符更改为布尔值?

Java 如何将字符更改为布尔值?,java,char,boolean,setter,getter,Java,Char,Boolean,Setter,Getter,我的问题是如何将字符更改为布尔值。例如,我有两个班 一个是只有BleedGhistory方法和信息的患者类: Public class Patient { private static String name; private static boolean bleedingHistory = true; private String answer; private double ureaLevel; private

我的问题是如何将字符更改为布尔值。例如,我有两个班

一个是只有BleedGhistory方法和信息的患者类:

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);
我知道我对历史出血的耐心检查部分是错误的,但我能做些什么来纠正它呢?谢谢

pat1 = new Patient (inName, Character.toString(inBleedingHistory).equalsIgnoreCase("y"));
如果inBleedingHistory与y或y匹配,则返回true。它还将在比较之前将字符转换为字符串

您还可以使用

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

boolean charIsY=(ch='y'| | ch='y')
还可以使用
equalsIgnoreCase
计算y/y差异。您必须在
char
上使用
==
,而不是
equals
修复它。强制转换为字符串或使用字符串。@EmanuelSeibold,我认为问题在于
inBleedingHistory
是一个字符,因此没有
equalsIgnoreCase
methodwhops,现在我得到了它。:-)对不起,我没看见。今天没喝咖啡;)哦,那么我能做到?我从没想过是的,你可以。如果答案符合你的要求,请接受。
pat1 = new Patient(inName, (inBleedingHistory=='y' || inBleedingHistory=='Y'));