循环不起作用

时间:2016-12-19 21:51:55

标签: java

我正在编写一个读取用户输入的程序。如果用户输入了错误的选择,程序将进入循环模式,直到用户选择不做出其他选择。

这是我想要的输出:

    choose (1): To find if the student fulfil the words count criteria 
    choose (2): To find if all sentences start with a Capital letter 
    choose (3): To find how many of the keywords used 
    choose (4): To find a word in the essay
    choose (5): To replace a word in the essay
    choose (6): To score the essay and print a report
    choose (7): To Exit

    Please Enter your choice: 1
You chose 1
        Do you want to choose another option (yes/no): 
Yes


    choose (1): To find if the student fulfil the words count criteria 
    choose (2): To find if all sentences start with a Capital letter 
    choose (3): To find how many of the keywords used 
    choose (4): To find a word in the essay
    choose (5): To replace a word in the essay
    choose (6): To score the essay and print a report
    choose (7): To Exit

    Please Enter your choice:  

我的问题是:程序退出之前会询问用户是否要选择其他选项

这是我的输出:

    choose (1): To find if the student fulfil the words count criteria 
    choose (2): To find if all sentences start with a Capital letter 
    choose (3): To find how many of the keywords used 
    choose (4): To find a word in the essay
    choose (5): To replace a word in the essay
    choose (6): To score the essay and print a report
    choose (7): To Exit

    Please Enter your choice: 1
You chose 1
        Do you want to choose another option (yes/no): BUILD SUCCESSFUL (total time: 1 second)

这是我的代码:

    Scanner input = new Scanner (System.in);
      boolean checkflag = false;
      String answer;
      int choice;
   do {checkflag=false;

        System.out.println("\n\n\n");
                   System.out.print("       choose (1): To find if the student fulfil the words count criteria \n"
                + "     choose (2): To find if all sentences start with a Capital letter \n"
                + "     choose (3): To find how many of the keywords used \n"
                + "     choose (4): To find a word in the essay\n"
                + "     choose (5): To replace a word in the essay\n"
                + "     choose (6): To score the essay and print a report\n"
                + "     choose (7): To Exit\n"
                + "\n       Please Enter your choice: ");

        choice = input.nextInt();

        if ((choice == 1)||(choice == 2)||(choice == 3)||(choice == 4)||(choice == 5)||(choice == 6)) {
            System.out.println("You chose " + choice);
        }

        else  if (choice == 7) {
            System.exit(0);
        } 


        else {
            System.out.print("        Incorrect entry, Please try again with valid entry from the list."
                    + "\n                 Please Enter your choice: ");
            choice = input.nextInt();

        }

        System.out.print("      Do you want to choose another option (yes/no): ");

        answer = input.nextLine();
        System.out.print("\n\n\n");
    } while (answer.equalsIgnoreCase("yes"));

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

public static void sentencesChecker(String essay){

    int count = 1;


    int countcap=0;

    String sent = ".";

    String senten;

    for (int i = 0; i < essay.length() - 1; i++) {

        if (essay.charAt(i) == '.') {
            count++;
        }

        if ((essay.charAt(i)) >= 'A' || (essay.charAt(i)) <= 'Z') {
            countcap++;
        }
    }