繁体   English   中英

While循环JOptionPane问题

[英]While loop JOptionPane issues

好的-我知道我已经很接近解决方案了,但是需要朝正确的方向轻推。 我需要用户单击“是”,程序需要再次开始询问问题。

执行后,出现以下错误

线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:java.lang.String.charAt(未知源)为1,Vowel3.main(Vowel3.java:49)

// java class for Panel I/O
import javax.swing.JOptionPane;

// declaration of the class
public class Vowel33
{

    // declaration of main program
    public static void main(String[] args)
    {

    // objects used to store data
    String input_string = null;
    int a_count = 0;
    int e_count = 0;
    int i_count = 0;
    int o_count = 0;
    int u_count = 0;
    int i = 0;
    int yes = 0;

    // 1. display a descriptive message
    String display_message = "This program asks the user for a sentence,\n" 
        + "searches the sentence for all vowels,\n"
        + "and displays the number of times each"
        + "vowel appears in the sentence";
    JOptionPane.showMessageDialog(null, display_message, "Lab 3 Description", JOptionPane.INFORMATION_MESSAGE);



    // 4. visit each String posotion

     do{

        // 3. input the character string

        input_string = JOptionPane.showInputDialog("Enter the sentence to search");

        // 5.  if position i of String is a vowel
        // 6.  increase the appropriate vowel counter
        if (input_string.charAt(i) == 'a' || input_string.charAt(i) == 'A')
        a_count++;

        else if (input_string.charAt(i) == 'e' || input_string.charAt(i) == 'E')
        e_count++;

        else if (input_string.charAt(i) == 'i' || input_string.charAt(i) == 'I')
        i_count++;

        else if (input_string.charAt(i) == 'o' || input_string.charAt(i) == 'O')
        o_count++;

        else if (input_string.charAt(i) == 'u' || input_string.charAt(i) == 'U')
        u_count++;

        i++;

        String display_message1 = input_string                                      // 7. display the String
        + "\n\n" + "has " + input_string.length() + " characters.\n\n"          // 8. display the number of characters
        + "There are \n" 
        + a_count + " a's,\n"                                                   // 9. disaply the number of each vowel
        + e_count + " e's,\n"
        + i_count + " i's,\n"
        + o_count + " o's, and\n"
        + u_count + " u's.\n\n";

        JOptionPane.showMessageDialog(null, display_message1, "Lab 3 Description", JOptionPane.INFORMATION_MESSAGE);

        yes = JOptionPane.showConfirmDialog(null, "Would you like to enter another string?\n\n", "Extra Credit", JOptionPane.YES_NO_OPTION);

    }   while (i < input_string.length());

            if (i == input_string.length())
            {
                yes = JOptionPane.showConfirmDialog(null, "Would you like to enter another string?\n\n", "Extra Credit", JOptionPane.YES_NO_OPTION);
                if (yes == 1)
                {
                    input_string = JOptionPane.showInputDialog("Enter the sentence to search");
                }
            }






    } // end of main
} // end of the class 

在您的代码中,您有一个条件,如果yes为0,则仍然可以执行循环。由于从我所见,从不重新定义yes ,因此您基本上有一个无限循环,并且当我退出字符串的边界时,您的代码将出错。长度。

同样不确定为什么在循环的底部有两个JOptionPanes(它将执行x次,其中x = input_string.length())

考虑类似:

if(i == input_string.length()) {
    //ask the user if they want to enter another string
    if(the user selected yes){
        yes = 1;
        //instantiate again with new input_string
    }
}

另一个注意事项:假设您要显示一条消息,询问用户是否要在完成对第一个条目的迭代之后再输入另一个字符串,所以似乎没有理由要包含yes变量和条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM