交货不起作用

时间:2015-12-25 09:27:42

标签: java

在我的java代码中,代码运行时行String st = sc.nextLine();没有输入,而我使用sc.next();代码正常工作时,请告诉我为什么sc.nextLine();不是工作

 import java.lang.*;
 import java.util.*;

/**?Chef wrote some text on a piece of paper and now he wants to know how 
many holes are in the text. What is a hole? If you think of the paper as 
the plane and a letter as a curve on the plane, then each letter divides
the plane into regions.For example letters "A", "D", "O", "P","R" divide
the plane into two regions so we say these letters each have one hole. 
Similarly, letter "B" has 2 holes and letters such as "C", "E", "F","K" 
have no holes. We say that the number of holes in the text is equal to 
the total number of holes in the letters of the text. 
Help Chef to determine how many holes are in the text.

Input

First line contains a single integer T <= 40, the number of test cases. 
T test cases follow. The only line of each test case contains a non-empty
text composed only of uppercase letters of English alphabet. The length 
of the text is less then 100. There are no any spaces in the input.

 Output

For each test case,output a single line containing number of holes in the corresponding txt
Example

Input:
2
CODECHEF
DRINKEATCODE

Output:
2
5
*/

class Holes {
    public static void main(String[] args) {

        int i, Testcase;
        int holes, space = 0, j;

        Scanner sc = new Scanner(System.in);
        Testcase = sc.nextInt();
        for (i = 0; i < Testcase; i++) {
            holes = 0;
            space = 0;

            String st = sc.nextLine();

            if (st.length() < 100) {
                char[] letter = st.toCharArray();
                for (j = 0; j < st.length(); j++) {
                    if (letter[j] == ' ') {
                        space++;
                    }
                }

                for (j = 0; j < st.length(); j++) {
                    if (space == 0) {
                        if (letter[j] == 'A' || letter[j] == 'D'
                                || letter[j] == 'O' || letter[j] == 'P'
                                || letter[j] == 'R') {
                            holes = holes + 1;

                        }

                        if (letter[j] == 'B') {
                            holes = holes + 2;
                        }
                    }
                }

            }
            if (st == st.toUpperCase() && space == 0) {
                System.out.println(holes);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

这是因为[Scanner#nextInt]方法不会消耗输入的最后一个换行符,因此在下次调用Scanner#nextLine时会消耗换行符

解决方法:

  • Scanner#nextLine之后触发空白Scanner#nextInt来消费该行的其余部分,包括换行符

检查this

答案 1 :(得分:1)

正如Nighthacks所提到的,Scanner.nextInt不会前进到下一个输入(阅读https://stackoverflow.com/a/13102066/643500

以这种方式尝试:

public class Holes {

    public static void main(String[] args) {

        int holes;

        Scanner sc = new Scanner(System.in);

        int numberOfCases = sc.nextInt(); // Get input as int - not going to
                                            // advance

        String[] testCases = new String[numberOfCases];

        String line = sc.nextLine(); // Move to next

        for (int i = 0; i < numberOfCases; i++) {
            line = sc.nextLine();// Read input as line
            testCases[i] = line;
        }

        sc.close();

        for (String aCase : testCases) {

            holes = 0;

            if (aCase.length() < 100 && !aCase.contains(" ")) {

                for (int j = 0; j < aCase.length(); j++) {

                    char letter = aCase.charAt(j);

                    if (letter == 'A' || letter == 'D' || letter == 'O'
                            || letter == 'P' || letter == 'R') {
                        holes++;
                    }

                    if (letter == 'B') {
                        holes = holes + 2;
                    }
                }

                System.out.println(holes);
            }
        }
    }
}