gpt4 book ai didi

java - char1 = f 和 char2 = t 怎么说?

转载 作者:行者123 更新时间:2023-12-02 01:49:55 26 4
gpt4 key购买 nike

import java.io.IOException;

public class DatentypChar {

public static void main(String [] args) throws IOException {

char c1 = (char) System.in.read();
char c2 = (char) System.in.read();

c1 = 'f' || 't'; // I would like to say c1 is f or t and c2 is t or f.
c2 = 't' || 'f';

if(c1 == 'f' && c2 == 'f');{
System.out.println(0);

}

if(c1 == 'f' && c2 == 't');{
System.out.println(1);
}

if(c1 == 't' && c2 == 'f');{
System.out.println(2);
}

if(c1 == 't' && c2 == 't');{
System.out.println(3);
}
}
}

大家好这就是目前的样子。我该如何解决这个问题?我想说 c1ftc2ft

任务:

编写一个程序将两个字符(数据类型char)覆盖标准输入。这两个字符代表 boolean 值值(value)观。字符“f”表示 false,字符“t”表示 true。根据此输入,带有提示文本的 int 值将显示在标准输出上。这依赖关系如下表所示:

1st character 2nd character Output value
false false 0
false true 1
true false 2
true true 3

如果没有输入有效的组合,则有意义的组合生成错误消息。

最佳答案

我认为您想问,如何验证用户输入的是“t”还是“f”。

在这种情况下,您可以使用 while 循环,一旦用户输入正确的值就退出,如下所示:

char c1;
char c2;
boolean correctInput = false;
while (!correctInput) {
System.out.println("Enter t or f");
c1 = (char) System.in.read();
c2 = (char) System.in.read();
if ((c1 == 't' || c1 == 'f') && (c2 == 't' || c2 == 'f')) {
correctInput = true;
}
}
//Rest of your code...

关于java - char1 = f 和 char2 = t 怎么说?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53131625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com