在do while循环结束时使用char而不是字符串。它';有可能吗?JAVA
所以在这个do-while代码中,布尔值是用户的一个输入,但它只需要一个输入。如果用户按[c]键,则do将继续,但如果按任何其他键,则do将停止。我已经有了处理在do while循环结束时使用char而不是字符串。它';有可能吗?JAVA,java,string,char,Java,String,Char,所以在这个do-while代码中,布尔值是用户的一个输入,但它只需要一个输入。如果用户按[c]键,则do将继续,但如果按任何其他键,则do将停止。我已经有了处理String c=“”)的代码但是我想用char完成。 当我使用charc[]时,也会出现另一个问题不可能使用c=kb.next().toUpperCase() 下面是我编写的示例代码 Scanner kb = new Scanner(System.in); String c = ""; do { //some code.
String c=“”)的代码代码>但是我想用char
完成。
当我使用charc[]时,也会出现另一个问题代码>不可能使用c=kb.next().toUpperCase()代码>
下面是我编写的示例代码
Scanner kb = new Scanner(System.in);
String c = "";
do {
//some code.
//here asks to the user to continue or not.
System.out.println("Press [c] to continue, any key to exit.");
c = kb.next().toUpperCase();
} while ( c.equals == ("C") );
也许它已经被问到了我试图找到答案。。。也许我是小朋友。但我不想知道我该怎么做。(如果已经重复,请告诉我不要取消注释)是的,这是可能的,非常接近。您需要使用String.equals或String.equalsIgnoreCase来比较字符串,而不是使用您尝试的方法。指
带炭
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
char chr;
do {
//some code.
//here asks to the user to continue or not.
System.out.println("Press [c] to continue, any key to exit.");
chr = kb.next().toCharArray()[0];
} while (chr != 'c');
}
如果要使用char c
而不是String c
,此代码:
char c;
do {
System.out.println("Press [c] to continue, any key to exit.");
c = kb.next(".").toUpperCase().charAt(0);
} while (c == 'C');
这个想法是将“
传递到下一个(…)
方法,以便只使用一个字符。我想使用字符
而不是字符串
答案中,很抱歉我错过了这一部分。好的,但是现在的想法是,如果用户输入C?它会重新开始的,因为chr条件,您只需要将其更改为chr=='c'
char c;
do {
System.out.println("Press [c] to continue, any key to exit.");
c = kb.next(".").toUpperCase().charAt(0);
} while (c == 'C');