Java题目练习
⭕题目一: 求绝对值,平方根,对数,正弦值
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
double num = scanner.nextDouble();
//write your code here......
System.out.println(Math.abs(num));
System.out.println(Math.sqrt(num));
System.out.println(Math.log(num));
System.out.println(Math.sin(num));
}
}
💯思路解析
本题目思路是:"直接使用java内置的math函数使用即可"
⭕题目二: 输出某一年的各个月份的天数
🌟代码演示
import java.util.Calendar;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int year = console.nextInt();
//write your code here......
Calendar c = Calendar.getInstance();
for(int month = 1; month <= 12; month++){
c.set(year,month,1);
c.add(Calendar.DATE,-1);
int days = c.get(Calendar.DATE);
System.out.println(year+"年"+month+"月:"+days+"天");
}
}
}
💯思路解析
本题目思路是:"使用对应的Calendar里面的方法"
⭕题目三:日期换算
🌟代码演示
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Scanner in = new Scanner(System.in);
String str1 = in.nextLine();
//write your code here......
//去除空格,剩下的合并为数组
String[] arr_timeFormat = str1.split(" ");
if(arr_timeFormat.length != 6){
System.out.println("您输入的数据不合理");
}else{
//合并
String combineTimeString = arr_timeFormat[0] + "-" + arr_timeFormat[1] + "-" + arr_timeFormat[2] + " "
+ arr_timeFormat[3] + ":" + arr_timeFormat[4] + ":" + arr_timeFormat[5];
//合并过后转化为Date类型
Date newDateFormat = sdf.parse(combineTimeString);
System.out.println("北京时间为:" + sdf.format(newDateFormat.getTime()));
System.out.println("纽约时间为:" + sdf.format(newDateFormat.getTime() - 12*60*60*1000));
}
}
}
💯思路解析
本题目思路是:"使用SimpleDateFormat进行格式化"
⭕题目四:判断学生成绩
🌟代码演示
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
//write your code here......
if(score>=0&&score<=100){
System.out.println(score);
}else {
System.out.println("分数不合法");
}
}
}
💯思路解析
本题目思路是:"直接用if,else进行判断即可"
⭕题目五:字符串去重
🌟代码演示
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
scanner.close();
HashSet<Character> hs = new HashSet<>();
//write your code here......
//set集合是不允许重复的,不包含当前字符就添加进去即可
for(char c : str.toCharArray()){
if(!hs.contains(c)){
hs.add(c);
}
}
for (char c:hs) {
System.out.print(c);
}
}
}
💯思路解析
本题目思路是:"set集合是不允许重复的,不包含当前字符就添加进去即可"
⭕题目六:集合遍历
🌟代码演示
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> list = new ArrayList<>();
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();
int num4 = scanner.nextInt();
int num5 = scanner.nextInt();
scanner.close();
list.add(num1);
list.add(num2);
list.add(num3);
list.add(num4);
list.add(num5);
System.out.print("普通for循环:");
//write your code here......
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+" ");
}
System.out.println();
System.out.print("增强for循环:");
//write your code here......
for (Integer integer : list) {
System.out.print(integer+" ");
}
System.out.println();
System.out.print("迭代器遍历:");
//write your code here......
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
}
}
💯思路解析
本题目思路是:"直接使用三种遍历方式即可"
⭕题目七:排队系统
🌟代码演示
import java.util.*;
public class Main {
public static void main(String[] args) {
Guest guest1 = new Guest("小明",false);
Guest guest2 = new Guest("小军",false);
Guest vipGuest = new Guest("小红",true);
Deque<Guest> deque = new ArrayDeque<>();
deque.add(guest1);
deque.add(guest2);
//write your code here......
deque.addFirst(vipGuest);
System.out.println(deque);
}
}
class Guest{
String name;
Boolean vip;
@Override
public String toString() {
return name;
}
public Guest(String name, Boolean vip) {
this.name = name;
this.vip = vip;
}
}
💯思路解析
本题目思路是:"直接使用addFirst即可"
⭕题目八:首尾交替出队
🌟代码演示
import java.util.ArrayDeque;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayDeque deque = new ArrayDeque();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String name = scanner.next();
// 初始化队列中的数据
deque.offerLast(name);
}
// write your code here......
int i = 0;
while(!deque.isEmpty()){
if(i % 2 == 0){
System.out.println(deque.pollFirst());
}else{
System.out.println(deque.pollLast());
}
i++;
}
}
}
💯思路解析
本题目思路是:"先判断队列是否为空,再对应使用添加pollFirst()即可"
作者:KJ.JK
文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习