【问题标题】:JOptionPane inputsJOptionPane 输入
【发布时间】:2015-02-19 04:28:14
【问题描述】:

我是 Java 新手,我必须编写一个 Java 应用程序,要求用户使用 JOptionPane 输入对话框输入一个句子,去吧 通过输入中的每个字符并计算大写字母、小写字母和 数字,然后使用JOptionPane 消息对话框打印计数。然后应用程序应重复此操作 处理,直到用户键入单词 STOP(或停止,或停止等)

我认为我到目前为止所做的大部分工作都是我需要做的,但由于某种原因,程序会忽略所有其他输入,除了我键入 stop 时,这并不意味着是字符串的一部分,但退出词。感谢您的帮助和解释。

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."); 
  }

}

【问题讨论】:

    标签: java joptionpane


    【解决方案1】:

    逻辑错误很少:

    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
    

    试试吧:

    String string1;
    do {
        string1 = JOptionPane.showInputDialog("Input words or type 'stop' to end.");
        charcount(string1);
    } while("stop".equalsIgnoreCase(string1));
    

    【讨论】:

      【解决方案2】:

      在编写代码之前:

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

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

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

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

      【讨论】:

        【解决方案3】:

        如果我的想法是正确的,这可能是因为除非你从一开始就输入“stop”,否则你的字符串永远不会等于“stop”。例如,它正在检查您的字符串是否等于“stop”(忽略大小写),而您应该检查它是否contains“stop”。您可以尝试以下方法:

        while(!string1.contains("stop")) {
            //Run your code here.
        }
        

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

        String checkStop = string1.toUpperCase();
        

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

        String checkStop = string1.toLowerCase();
        

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

        【讨论】:

          猜你喜欢
          • 2011-03-08
          • 2016-03-17
          • 1970-01-01
          • 2011-01-06
          • 2011-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多