简体   繁体   English

打印if和else循环的布尔输出

[英]Print boolean output of if and else loops

I have a Java code that checks input from the keyboard. 我有一个Java代码,用于检查键盘输入。 There is a loop of if that for specific character returns True. 如果特定字符返回True,则存在一个循环。 The else returns false for any other character. else对于其他任何字符均返回false。

How to write a code that identifies which of the above two (True or False) was chosen by the program and print it? 如何编写代码以识别程序选择了以上两个(真或假)中的哪一个并将其打印出来? The boolean value of the instance connectionStatus 实例connectionStatus的布尔值

import java.util.Scanner;

public class BlueTooth3 {
    public boolean connectionStatus;

     boolean connectBlueTooth () {
        System.out.println("Enter connecting code Baby3");
        Scanner keyboard = new Scanner(System.in);
        String conCode = keyboard.next();
        System.out.println("You Entered " + conCode);
        keyboard.close();
        if (conCode.equals ("c")){
        System.out.println(conCode + " Is a true Code");
        System.out.println("This is Boolean " +Boolean.TRUE);

            return connectionStatus;
        } 

        if (conCode.equals ("C")){
            System.out.println(conCode + " Is a true Code");
            System.out.println("This is Boolean " +Boolean.TRUE);

                return connectionStatus;
            }
        else {
            System.out.println( conCode + " Is a false Code" );
            System.out.println("This is Boolean " + Boolean.FALSE);
            return connectionStatus;
        }
        }
     }

Unless my eyes are closed, you don't seem to be doing anything to the connectionStatus variable. 除非我闭上眼睛,否则您似乎对connectionStatus变量没有任何作用。 You can change the value of that variable so that the function that receives that variable can do things accordingly. 您可以更改该变量的值,以便接收该变量的函数可以相应地执行操作。 For example: 例如:

    if (conCode.equals ("C")){
        System.out.println(conCode + " Is a true Code");
        System.out.println("This is Boolean " +Boolean.TRUE);
        connectionStatus = true; // means that the code went in this if
            return connectionStatus;
    }
    else {
        System.out.println( conCode + " Is a false Code" );
        System.out.println("This is Boolean " + Boolean.FALSE);
        connectionStatus = false; // means that the code went in this else
        return connectionStatus; 
    }

And your code should be something like this: 而且您的代码应如下所示:

public void myFunc(){
    System.out.println(connectBlueTooth);
}

This just simply prints out the outcome of the connectBlueTooth function, which should be True if it went in the if , and False if it went in the else . 这只是简单的打印出的结果connectBlueTooth功能,这应该是真实的,如果它在去if ,假如果在去else

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM