java,在大型字符串输入达到特定字符时进行计数的代码



我的问题是我是否有一个字符串-字符串'#'和'.'中有两种类型的字符如果我每次向右移动3次并向下移动1次,我想计算我在"#"上降落了多少次。例如,给定的字符串如下所示:

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

应该发生的情况如下,0表示降落在。X意味着它降落在#上,也就是每次发生这种情况时计数应该增加一的时候:

...#.#.......##....#.......#...
.#.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,如果计数为yes,我会检查该字符是否为"#"++在这个例子中,它移动了三个,所以第一行移动到。。。然后向下到下一行,该行将是索引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);
}
}

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

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

最新更新