簡體   English   中英

Java - 在字符輸入上使用三元運算符來獲取布爾值

[英]Java - Use ternary operator on char input to get boolean

希望在 C# 中做類似的事情:

bool walkable = t.Type == TileType.Green ? true : false;

但在 Java 中

Boolean international_F = (in.next() == 'Y') ? true : false;

以上是我到目前為止所嘗試的。 想知道這是否甚至可能。

編輯:我剛剛注意到.nextChar()不存在。 編輯片段以反映這一點。

"nextChar":假設in是一個Scanner ,你的問題是 Scanner 沒有nextChar()方法。 您可以閱讀整個單詞,然后將其作為第一個字符:

char theChar = in.next().charAt(0)

boolean vs ternery:如果你的輸出是真/假,那么你就不需要 if。 你可以只寫:

bool walkable = t.Type == TileType.Green; // C#
boolean international_F = in.next().charAt(0) == 'Y'` // Java

boolean vs Boolean:還請注意, boolean是 Java 中的原始布爾類型。 使用Boolean將強制將其包裝為 Boolean 類。

區分大小寫:如果要允許 'y' 或 'Y',請先強制輸入為已知大小寫。 由於charAt()返回原始字符,因此您需要使用靜態Character.toUpperCase()

解決方案:

boolean isY = Character.toUpperCase(in.next().charAt(0)) == 'Y'
// - OR - 
boolean isY = in.next().startsWith("Y") // not case-insensitive
Boolean international_F = "Y".equals(in.next()); // next  returns a string
Boolean international_F =in.next().charAt(0) == 'Y';

您不需要三元運算符來簡單地分配條件評估的結果 ( true / false )。 如果你想根據條件的評估結果做一些事情,你需要一個三元運算符,例如

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(System.in);
        System.out.print("Do you want to continue? [Y/N]: ");
        boolean yes = in.nextLine().toUpperCase().charAt(0) == 'Y';
        if (yes) {
            System.out.println("You have chosen to continue");
        } else {
            System.out.println("You have chosen to stop");
        }

        // Or simply
        System.out.print("Do you want to continue? [Y/N]: ");
        if (in.nextLine().toUpperCase().charAt(0) == 'Y') {
            System.out.println("You have chosen to continue");
        } else {
            System.out.println("You have chosen to stop");
        }

        // You can use ternary operator if you want to do something based on the result
        // of evaluation of the condition e.g.
        System.out.print("Do you want to continue? [Y/N]: ");
        String response = in.nextLine().toUpperCase().charAt(0) == 'Y' ? "Yes" : "No";
        System.out.println(response);

        // Without a ternary operator, you would write it as:
        System.out.print("Do you want to continue? [Y/N]: ");
        String res;
        char ch = in.nextLine().toUpperCase().charAt(0);
        if (ch == 'Y') {
            res = "Yes";
        } else {
            res = "No";
        }
        System.out.println(res);
    }
}

示例運行:

Do you want to continue? [Y/N]: y
You have chosen to continue
Do you want to continue? [Y/N]: n
You have chosen to stop
Do you want to continue? [Y/N]: y
Yes
Do you want to continue? [Y/N]: n
No

這是一個演示您想要執行的操作的示例:

char a = 'a';
char b = 'b';

Boolean b1 = (a == 'a') ? true : false;
Boolean b2 = (a == b) ? true : false;

System.out.println(b1);
System.out.println(b2);

輸出將是:

true
false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM