结果跳过单词请求扫描程序

时间:2017-10-08 19:30:52

标签: java

import java.util.Scanner;
public class Ariete25Programs {
public static void main(String[] tin) {

char again = 0;
int choose;
if(choose == 7) {
char again7 = 0;

do
{
    System.out.println("---Vowels and Consonants---");  

    String line;
    System.out.println("Enter word: ");
    line = sc.nextLine();

    String vowels = " ", consonants = " ", digits = " ", spaces = " ";

        for(int i = 0; i < line.length(); ++i)
        {
            char ch = line.charAt(i);
            if(ch == 'a' || ch == 'e' || ch == 'i'
                || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I'
                || ch == 'O' || ch == 'U') 
    {
                vowels+=line.charAt(i);
        }
            else if((ch >= 'a'&& ch <= 'z')) 
    {
                consonants+=line.charAt(i);
           }
    }
    System.out.println("Vowels: " + vowels);
    System.out.println("Consonants: " + consonants);

        System.out.println("Try Again? (Y/N)");
        again7 = sc.next().charAt(0);
        if(again7 == 'n') {
        System.out.println("Menu? [y/n]");
        again = sc.next().charAt(0);
        }
        else if(again7 == 'n') {
        System.out.println("Thank you for using the programs!");
        System.exit(0);
        }
        }
        while(again7 == 'y');   

然后当你选择7时的结果变成这样 -----元音和辅音----- 输入字词: 再试一次?(是/否)

有人能帮帮我吗?我似乎无法弄清楚我的代码中的问题。它一直在跳过。

1 个答案:

答案 0 :(得分:0)

我猜你做choose = sc.nextInt();之类的事情你应该尝试choose = Integer.parseInt(sc.nextLine());
请将sc.next().charAt(0)替换为sc.nextLine().charAt(0) 文档:

此外else if(again7 == 'n')错误,因为您之前已经检查了几行,所以您永远不会执行else if块中的代码

        do
        {
            System.out.println("---Vowels and Consonants---");  

            String line;
            System.out.println("Enter word: ");
            line = sc.nextLine();

            String vowels = " ", consonants = " ", digits = " ", spaces = " "; // digits and spaces are not used

            int end = line.length();
            for (int i = 0; i < end; ++i) {
                char ch = line.charAt(i);
                if(ch == 'a' || ch == 'e' || ch == 'i'
                        || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I'
                        || ch == 'O' || ch == 'U') {
                    vowels += line.charAt(i);
                }
                else if ((ch >= 'a'&& ch <= 'z')) {
                    consonants += line.charAt(i);
                }
            }

            System.out.println("Vowels: " + vowels);
            System.out.println("Consonants: " + consonants);

            System.out.println("Try Again? (Y/N)");
            again7 = sc.nextLine().charAt(0);
            if (again7 == 'n') {
                System.out.println("Menu? [y/n]");
                again = sc.nextLine().charAt(0);
                if (again == 'n') {
                    System.out.println("Thank you for using the programs!");
                    break;
                } else {
                    // display Menu
                }
            }
        } while(again7 == 'y');