gpt4 book ai didi

java - 使用空字符作为循环条件,程序在应该循环时结束

转载 作者:太空宇宙 更新时间:2023-11-04 07:44:13 25 4
gpt4 key购买 nike

This program is supposed to loop for an input of a char if nothing is written in, the program will however not compile, it does not accept char tegn='';. How can i create a loop where the condition si that nothing has been written in for a char?

import static javax.swing.JOptionPane.*;
char tegn = '';
do {
tegn = showInputDialog("Input a char.").charAt(0);
} while (tegn == '');

更新:我用 Vishal K 代码中的一个想法解决了我的代码,使用 String 来确定用户是否没有写入或写入了什么:

    String tegn = ""; 
do { tegn = showInputDialog("Input a char.");
} while (tegn.equals(""));

char chartegn = tegn.charAt(0);

最佳答案

您可以尝试以下操作:

import static javax.swing.JOptionPane.*;
String str = "";
char tegn=' ';
do {
str = showInputDialog("Input a char.");
if (str != null && !"".equalsIgnoreCase(str.trim()))
{
tegn = str.charAt(0);
}
}while(str!=null && !"".equalsIgnoreCase(str.trim())

关于java - 使用空字符作为循环条件,程序在应该循环时结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15586693/

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