sc.next() 与 sc.nextLine() 的辨别使用
作者:
胡歌-此生不换
,
2022-11-18 20:31:24
,
所有人可见
,
阅读 431
sc.next() 与 sc.nextLine() 的辨别使用:
771. 字符串中最长的连续出现的字符
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-- > 0){
String str = sc.next();
int len = str.length();
char[] s = str.toCharArray();
char c = '0';
int k = 0;
for(int i = 0; i < len; i++){
int j = i + 1;
while(j < len && s[j] == s[i]) j++;
if(j - i > k){
k = j - i;
c = s[i];
}
i = j - 1;
}
System.out.printf("%c %d\n", c, k);
}
}
}
770. 单词替换
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String[] s = sc.nextLine().split(" ");
String a = sc.next();
String b = sc.next();
for(String c: s){
if(c.equals(a) == true) System.out.printf("%s ", b);
else System.out.printf("%s ", c);
}
}
}