如何围绕 for 循环创建 while 循环以允许用户退出循环? [英] How do I create a while loop around a for loop that will allow the user to quit the loop?

查看:36
本文介绍了如何围绕 for 循环创建 while 循环以允许用户退出循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是循环的新手,我已经编写了一个 for 循环来提示用户输入一个短语,for 循环会打印出有多少空格、a's、e's、s 和 t's.我需要做一个while循环,允许用户输入quit"来退出循环.我也是学习 switch 打印出字母的新手.一个多星期以来,我已经尝试用我的代码来修复我的错误(持续循环或没有结果).下面是我的代码,在此先感谢您.

I am new to loops and I coded a for loop already that prompts the user to input a phrase and the for loop prints out how many spaces, a's, e's ,s's, and t's there are. I need to make a while loop that will allow the user to type in "quit" to quit the loop. I am also new to learning switch to print out the letters. I have already tried experimenting with my code to fix my errors (either an ongoing loop or no results) for over a week. Here is my code below and thank you in advance.

import java.util.Scanner;
public  class Count
{

public static void main (String[] args)
{

    Scanner scan = new Scanner (System.in);
    String phrase;    // a string of characters
    int countBlank;   // the number of blanks (spaces) in the phrase 
    int i = 0;
    char ch;          // an individual character in the string
    String quit = "quit";
    System.out.println ();
    System.out.println ("Character Counter");
    System.out.println ();

    System.out.print ("Enter a sentence or phrase (quit to quit): ");
    phrase = scan.nextLine(); // read input Scanner  
    int length = phrase.length();       // the length of the phrase

    countBlank = 0;
    int countA = 0;
    int countE = 0;
    int countS = 0;
    int countT = 0;

    //I am not sure how to set up the start of the while loop
    //while (!phrase.equals("quit")){
    //while (phrase != "quit"){

    for (i = 0; i < length; i++){
        ch = phrase.charAt(i);
        if (ch == (' '))
            countBlank++;
        switch (ch)
        {
        case 'a': case 'A':  countA++; break;
        case 'e': case 'E':  countE++; break;
        case 's': case 'S':  countS++; break;
        case 't': case 'T':  countT++; break;       
        }
    }
    System.out.println ();
    System.out.println ("Number of blank spaces: " + countBlank);
    System.out.println ();
    System.out.println ("Number of a's: " +countA);
    System.out.println ("Number of e's: " +countE);
    System.out.println ("Number of s's: " +countS);
    System.out.println ("Number of t's: " +countT);
    }
}

输出:

Character Counter

Enter a sentence or phrase (quit to quit): AEST aest

Number of blank spaces: 1

Number of a's: 2
Number of e's: 2
Number of s's: 2
Number of t's: 2

推荐答案

您走在正确的轨道上,但您确实需要在该过程完成后再次阅读该短语以使 while 循环起作用.否则会死循环.以下是我所做的一些更改:

You are on the right track, but you do need to read the phrase again once the process is completed to make the while loop work. Otherwise, it will be endless loop. Here are few changes I made :

 while (!phrase.equals("quit")){

        for (i = 0; i < length; i++){
            ch = phrase.charAt(i);
            if (ch == (' '))
                countBlank++;
            switch (ch)
            {
            case 'a': case 'A':  countA++; break;
            case 'e': case 'E':  countE++; break;
            case 's': case 'S':  countS++; break;
            case 't': case 'T':  countT++; break;       
            }
        }
        System.out.println ();
        System.out.println ("Number of blank spaces: " + countBlank);
        System.out.println ();
        System.out.println ("Number of a's: " +countA);
        System.out.println ("Number of e's: " +countE);
        System.out.println ("Number of s's: " +countS);
        System.out.println ("Number of t's: " +countT);
        phrase =scan.nextLine(); //read next phrase from user to continue.
        }

这篇关于如何围绕 for 循环创建 while 循环以允许用户退出循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆