gpt4 book ai didi

java - For Loop 不会破坏 Java 吗?

转载 作者:行者123 更新时间:2023-12-02 03:31:48 25 4
gpt4 key购买 nike

我一直在 Codeforces 中解决这个问题,我能够编写代码,但 for 循环没有中断。 “if”条件下的中断不起作用。

这是链接: https://codeforces.com/problemset/problem/1167/A

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

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();

for(int i=0;i<n;i++){
int x=sc.nextInt();
String y=sc.next();

int c=x;

for(int j=0;j<x;j++){ // This For loop is not breaking
if(y.charAt(j)==8){
break;
}
else{
c--;
}
}
if(c>=11){
System.out.println("YES" );
}
else{
System.out.println("NO" );
}
}
}
}

Input: 13
7818005553535
Output(Expected): YES that is c=12
Output: No that is c=0

最佳答案

您实际上是在检查位置 j 上的 char 是否是代码为 8 的 unicode 字符,而不是字符“8”。而 Unicode 字符巫码 8 是 BS(退格键),所以你基本上是在检查字符是否是退格键。

您需要检查:

if(y.charAt(j) == '8')  // character literal

if(y.charAt(j) == 56)  // integral value of character

if(y.charAt(j) == '\u0038') // character escape sequence (Hex Value)

Unicode 字符代码: https://unicode-table.com/pl/#basic-latin

关于java - For Loop 不会破坏 Java 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56880903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com