首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JOptionPane输入

JOptionPane输入
EN

Stack Overflow用户
提问于 2015-02-19 12:28:15
回答 3查看 2.7K关注 0票数 0

我是Java新手,我必须编写一个Java应用程序,它将要求用户使用JOptionPane输入对话框键入一个句子,遍历输入中的每个字符,计算大写字母、小写字母和数字的数量,然后使用JOptionPane消息对话框打印计数。然后,应用程序应重复此过程,直到用户键入单词STOP (或Stop,或STop等)。

我认为到目前为止我已经完成了我需要它做的大部分事情,但是由于某种原因,程序忽略了所有其他输入,除了当我输入stop时,这并不意味着它是字符串的一部分,而是退出单词。我很感谢你的帮助和解释。

代码语言:javascript
复制
import javax.swing.JOptionPane; 
public class Project0 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String string1 = ""; 
    while(!string1.equalsIgnoreCase("Stop")){
        string1 = JOptionPane.showInputDialog("Input words or type 'stop' to end.");
        string1 += string1;
    }
    charcount(string1);



}
public static void charcount(String userin){
    int uppercount, lowercount, digitscount; variables
    uppercount = 0;
    lowercount = 0;
    digitscount = 0;

    for(int c = 0; c < userin.length(); c++ ){ 
        char ch = userin.charAt(c); 
        if(Character.isUpperCase(ch)){ uppercount += 1; } 
        else if(Character.isLowerCase(ch)){ lowercount += 1; } 
        else if(Character.isDigit(ch)){ digitscount += 1; }

    }
    JOptionPane.showMessageDialog(null, "There are " + uppercount + " uppercase characters, " + lowercount + " lowercase characters and " + digitscount + " digits."); 
  }

}
EN

回答 3

Stack Overflow用户

发布于 2015-02-19 12:35:44

逻辑错误很少:

代码语言:javascript
复制
while(!string1.equalsIgnoreCase("Stop")){
        string1 = JOptionPane.showInputDialog("Input words or type 'stop' to end.");
        string1 += string1;// gets doubled the input, resulting into infinite loop
    }
    charcount(string1);// possibly has to be inside loop

相反,尝试:

代码语言:javascript
复制
String string1;
do {
    string1 = JOptionPane.showInputDialog("Input words or type 'stop' to end.");
    charcount(string1);
} while("stop".equalsIgnoreCase(string1));
票数 0
EN

Stack Overflow用户

发布于 2015-02-19 12:40:12

在编写代码之前:

while(!string1.equalsIgnoreCase("Stop")){ string1 = JOptionPane.showInputDialog("Input words or type 'stop' to end."); string1 += string1; }

你应该检查string1是否等于"stop“。如果没有,很酷,像你已经在做的那样添加,但是如果没有,break循环。

由于已经创建了string1,因此我建议使用do-while类型的循环:

do { string1 = JOptionPane.showInputDialog(...); if (!string1.equalsIgnoreCase("Stop") { // Add to string1 } } while (!string1.equalsIgnoreCase("Stop"));

票数 0
EN

Stack Overflow用户

发布于 2015-02-19 13:20:59

如果我没记错的话,这可能是因为,除非你从一开始就输入“”,否则你的字符串永远不会停止等于“”。例如,它检查字符串是否等于contains”(忽略大小写),而你应该检查它是否等于"stop“。您可以尝试以下操作:

代码语言:javascript
复制
while(!string1.contains("stop")) {
    //Run your code here.
}

如果在检查字符串时忽略大小写,则可以创建一个名为"checkStop“的字符串,并将其设置为等于string1,然后将其全部转换为大写,如下所示:

代码语言:javascript
复制
String checkStop = string1.toUpperCase();

您还可以将其转换为小写:

代码语言:javascript
复制
String checkStop = string1.toLowerCase();

然后,您可以使用checkStop来确定字符串是否包含"stop“或"STOP”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28598602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档