如何反复要求用户输入一个句子,如果用户键入END,则应停止输入,还应计算单词、元音和非元音的数量 import java.util.Scanner; 公开课教师 { 公共静态void main(字符串参数[]) { 扫描仪扫描=新扫描仪(System.in); 整数计数=0; int count1=1; int count2=0; //布尔t=false; System.out.println(“请输入一个句子”); 字符串s1=scan.nextLine(); 而(!s1.equals(“END”)) { 系统输出打印LN(s1); s1=scan.nextLine(); 如果(s1等于(“结束”)) { 系统输出打印项次(“结束”); } } 对于(int i=0;i

如何反复要求用户输入一个句子,如果用户键入END,则应停止输入,还应计算单词、元音和非元音的数量 import java.util.Scanner; 公开课教师 { 公共静态void main(字符串参数[]) { 扫描仪扫描=新扫描仪(System.in); 整数计数=0; int count1=1; int count2=0; //布尔t=false; System.out.println(“请输入一个句子”); 字符串s1=scan.nextLine(); 而(!s1.equals(“END”)) { 系统输出打印LN(s1); s1=scan.nextLine(); 如果(s1等于(“结束”)) { 系统输出打印项次(“结束”); } } 对于(int i=0;i,java,string,Java,String,建议:您希望像以下伪代码一样构造代码: import java.util.Scanner; public class exampaper { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int count=0; int count1=1; int count2=0; // boole

建议:您希望像以下伪代码一样构造代码:

    import java.util.Scanner;

public class exampaper
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        int count=0;
        int count1=1;
        int count2=0;
    //  boolean t =false;

        System.out.println("Please enter a sentence");
        String s1=scan.nextLine();

        while(!s1.equals("END"))
        {

            System.out.println(s1);
            s1=scan.nextLine();

            if(s1.equals("END"))
            {
            System.out.println("END");
            }


        }

        for(int i=0;i<s1.length();i++)
        {

            if(s1.charAt(i)==' ')
            {
                count1++;
            }
            else if(s1.charAt(i)=='a'||s1.charAt(i)=='A')
            {
                count++;
            }
            else if(s1.charAt(i)=='e'||s1.charAt(i)=='E')
            {
                count++;
            }
            else if(s1.charAt(i)=='i'||s1.charAt(i)=='I')
            {
                count++;
            }
            else if(s1.charAt(i)=='o'||s1.charAt(i)=='O')
            {
                count++;
            }
            else if(s1.charAt(i)=='u'||s1.charAt(i)=='U')
            {
                count++;
            }
            else
            {
                count2++;
            }



        }
    System.out.println("Number of vowels: " +count);
    System.out.println("Number of words: " +count1 );
    System.out.println("Number of non-vowels: " +count2);
    }

}

每次通过循环时,“无限循环”都会提示用户输入一个字符串,但是当收到
“您的终止符在这里”
时,会将
从循环中断开。

什么是真的?这是每个字符的for循环吗?因为这显然是家庭作业(或自学),您将从中了解到更多信息,而不仅仅是将答案交给您,我使用Java和伪代码的组合进行了回答。您已经有了一个贯穿每个字符的for循环,但没有将它嵌套在用户输入的每一行的外部循环中。至于“while what is true”:
while(true){…}
是一个无限循环。它在括号中的表达式计算为
true
时循环。由于括号中的表达式是文本常量
true
,因此它将永远循环…或者更确切地说,直到出现
中断;
。可能的重复
public class ExamPaperAnswerFromStackOverflow {

    public static void main(String args[]) {

        initializations-here

        while( true ) {
            System.out.println("Your prompt here");
            s1 = scan.nextLine();
            if ( !s1.equals("your-terminator-here") ) {
                break;
            }

            for-each-character {
                // code-to-count-vowels-constants-words-here
            }
        }

        print-summary-here
    }
}