java,当从大字符串输入到特定字符时计数的代码

4xrmg8kj  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(308)

我的问题是,如果我有一个字符串-在字符串“#”和“.”中有两种类型的字符,我想计算一下,如果每次我移到右边的3和下面的1,我会落在“#”上多少次。例如,给定的字符串如下所示:

...#.#.......##....#.......#...
.#....#.##.#..#.......#.....##.

# ..#...##.####..###.....#......

..#...##...#...#.#......#...#.#
.##.##.#...#.....#.##..##......
.#...#.#.##.###..#...#...#.....
.#..##..#....##.##....##....##.
..#...##....#..###........##...
.#..#..#.#....#.#...#.#......#.
.##.....#...#..#..#..#...###...
.#...#....#..#...........###...
.....#...........##.#......#...
.....#....##......##..#.#......
-1//stops the checking

应该发生的是以下情况,0表示降落在地面上。x表示它落在#上,也就是每次发生这种情况时计数应该增加1:

...#.#.......##....#.......#...
.#.o..#.##.#..#.......#.....##.

# ..#..0##.####..###.....#......

..#...##.0.#...#.#......#...#.#
.##.##.#...#0....#.##..##......
.#...#.#.##.###0.#...#...#.....
.#..##..#....##.##0...##....##.
..#...##....#..###...o....##...
.#..#..#.#....#.#...#.#.0....#.
.##.....#...#..#..#..#...##X...
.#...#....#..#...........###..0//notice here next round it will exceed
.....#...........##.#......#.....0..#...........##.#......#...//line get doubled as if returning to beginning of the 
.....#....##......##..#.#...........X....##......##..#.#......//same here
-1//to stop

所以在开始的时候,我移动到第三个索引,向下移动1到下一行,我检查这个字符是否是“#”,如果是,count所以在这个例子中-它移动3,所以第一行移动到。。。然后下一行是索引3,在本例中是。所以第三个是。等等。如果它落在#count。这是我的代码,但似乎不起作用-

public static Scanner reader = new Scanner(System.in);

public static void main(String[] args) {
    String a = "";//starts empty
    int cnt = 0, b = 0;//cnt++ if '#', b helps us move 3 to the right each time
    a = reader.nextLine(); //gets the string
    if (b + 3 > a.length() - 1) {//checks that the length when moving 3 doesn't exceed string length
        b = 0;// if it does exceed then go all the way to the left of the string
    }
    b += 3;//move right 3
    a = reader.nextLine(); //get next line from the input
    while (a != "-1") {//until there is -1 (to stop getting input) it continues to check
        char c = a.charAt(b -
                1);//takes whats at place b(-1 is because first index is 0 and not1) and note this is three to the right and after it moves down a row!// is line 23
        if (c == '#' && a != "") {//checks if string isn't empty and if the char equals #
            cnt++;//if yes count++
        }
        if (b + 3 > a.length() - 1) {//explained above
            b = 0;
        }
        b += 3;
        a = reader.nextLine();
    }

    System.out.println(cnt);
}

我得到的错误如下:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 26
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:702)
    at q.qq.main(Advent3.java:23)

我们将不胜感激。如果有什么地方不清楚,甚至不知道如何改进,请告诉我:)在聊天和尝试改进后,感谢代码:

public class Advent3 {
    public static Scanner reader= new Scanner(System.in);
    public static void main(String[] args){
            int b=4,cnt=0,line=1,s;
            String str="";
            char charsign;
            str=reader.nextLine();
            s=str.length();
            str=reader.nextLine();

            while(str!=""||str!="-1") {
                line++;
                s=str.length();
                if(b%str.length()==0) {
                    charsign= str.charAt(10);
                }
                else {
                    charsign= str.charAt(b%str.length()-1);
                }
                if(charsign=='#') {
                    cnt++;
                    System.out.println(cnt);
                }
                b+=3;
                str=reader.nextLine();
            }   
            System.out.println(str);

            System.out.println(cnt);
    }
    }

代码本身的工作,我认为我非常接近,但它没有打印正确的答案!我被困住了,非常感谢你的帮助谢谢!

7bsow1i6

7bsow1i61#

public static int count(Scanner scan) {
    int res = 0;
    int col = -1;
    String str;
    boolean check = false;

    while (!"-1".equals(str = scan.nextLine())) {
        if (col >= str.length())
            col %= str.length() - 1;
        if (check && str.charAt(col) == '#')
            res++;

        col += 3;
        check = true;
    }

    return res;
}

演示:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println(count(scan));  // 5
}

输入:

...#.#.......##....#.......#...
.#....#.##.#..#.......#.....##.

# ..#...##.####..###.....#......

..#...##...#...#.#......#...#.#
.##.##.#...#.....#.##..##......
.#...#.#.##.###..#...#...#.....
.#..##..#....##.##....##....##.
..#...##....#..###........##...
.#..#..#.#....#.#...#.#......#.
.##.....#...#..#..#..#...###...
.#...#....#..#...........###...
.....#...........##.#......#...
.....#....##......##..#.#......
-1

输出:

...#.#.......##....#.......#...
.#0...#.##.#..#.......#.....##.

# ..#.0.##.####..###.....#......

..#...##0..#...#.#......#...#.#
.##.##.#...1.....#.##..##......
.#...#.#.##.##2..#...#...#.....
.#..##..#....##.#3....##....##.
..#...##....#..###..3.....##...
.#..#..#.#....#.#...#.#3.....#.
.##.....#...#..#..#..#...#4#...
.#...#....#..#...........###.4.
..4..#...........##.#......#...
.....5....##......##..#.#......
-1

5

相关问题