Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
在do while循环结束时使用char而不是字符串。它';有可能吗?JAVA_Java_String_Char - Fatal编程技术网

在do while循环结束时使用char而不是字符串。它';有可能吗?JAVA

在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.

所以在这个do-while代码中,布尔值是用户的一个输入,但它只需要一个输入。如果用户按[c]键,则do将继续,但如果按任何其他键,则do将停止。我已经有了处理
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');