刷题的狂欢-----JAVA每日三练-----第十九天
Posted 敲代码的xiaolang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了刷题的狂欢-----JAVA每日三练-----第十九天相关的知识,希望对你有一定的参考价值。
努力刷题,每日三题。-------题目来源 “牛客网”
第一题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int numbers = 0;
int words = 0;
int space = 0;
int other = 0;
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
int len = str.length();
for(int i = 0; i<len; i++){
if(str.charAt(i)>='a'&&str.charAt(i)<='z'||str.charAt(i)>='A'&&str.charAt(i)<='Z'){
words++;
}
else if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
numbers++;
}
else if(str.charAt(i)==' '){
space++;
}
else{
other++;
}
}
System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other);
}
}
第二题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Main main = new Main();
int number = console.nextInt();
System.out.println(main.palindromeNumber(number));
}
public Boolean palindromeNumber(int number){
int array[] = new int [5];
int i;
for(i=0; i<5; i++){
array[i] = number%10;
number = number/10;
}
int j = 0;
int k = 4;
while(j<k) {
if (array[j++] != array[k--]) {
return false;
} else {
return true;
}
}
return null;
}
}
第三题
import java.util.*;
public class Main {
public static void main(String[] args) {
LikeRecorder recorder = new LikeRecorderImpl();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String name = scanner.next();
recorder.like(name);
}
System.out.println(Arrays.toString(recorder.getLikeUsers()));
}
}
interface LikeRecorder {
void like(String username);
String[] getLikeUsers();
}
class LikeRecorderImpl implements LikeRecorder {
private HashSet<String> names = new HashSet();
public void like(String name) {
if (names.contains(name)) {
names.remove(name);
} else {
names.add(name);
}
}
public String[] getLikeUsers() {
return names.toArray(new String[0]);
}
}
😉😉😉有问题评论区留言,欢迎大家浏览下面的博文,并参与博主设置的投票当中。😊😊😊
https://blog.csdn.net/weixin_52605156/article/details/120689189?spm=1001.2014.3001.5501
以上是关于刷题的狂欢-----JAVA每日三练-----第十九天的主要内容,如果未能解决你的问题,请参考以下文章