BLOG1
第一次题目集:
(1)(2)(3)前言 设计与分析 总结
本次习题集共有9道题,题目量较多,但总体难度较小,主要考察课本前九章内容,也是老师要求自学的部分,这些内容其实与C语言较接近,所以自学起来接受的也比较快。七道题基本都考察了Scanner数据输入的用法。
第一题考察的是身体质量指数测算,在上学期C语言学习当中也做过类似的题目,主要考察的是if语句,难度简单;第二题长度质量计量单位换算,简单考察了double数据类型和的用法;第三题考察奇数求和,通过简单的遍历即循环语句相加实现,难度比较简单;第四题考察房产税费计算,通过if语句实现,难度较简单;第五题考察的是游戏角色选择,考察了字符数组,通过定义字符数组,然后采用if语句输入相对应的数字来选择不同角色,也不难;第六题是考察学号识别,比前几题要难度大一点,通过将输入的学号转化为字符串类型,在通过获取字符串中的内容来完成;第七题考察巴比伦法求近似值,难度较易,虽然题目看似复杂,但其实可以直接通过题目所给的公式求解,使用if语句便能完成;第八题考察二进制数值提取,难度中等,难度主要在于如何解决不满足的特殊情况,使用了字符串中的获取内容,以及遍历字符串来获取所需要的内容;第九题考察如何判断三角形类型,只要知道三角形的性质使用if语句实现判断不同三角形即可,难度简单。
(2)结果
第二次题目集
(1)前言:第二次题目集有四道题,相比第一次难度提升跨度较大,需要我们花较多时间去理解与练习,主要考察的是对类与对象的理解。题目也都较长,需要我们有耐心的去一步一步分析然后设计,题目也都给了测试用例,也可以通过题目所给测试用例去改进代码,达到题目所提要求。前两道是菜单计价系统,刚开始看题时有些懵,因为刚开始对类与对象的理解还不太深,题目也比较长,很难再短时间内用代码去实现,所以花了比较长的时间,最后也没能拿到满分。
(2)设计与分析
7-1菜单计价程序-1实现了一个简单的点餐系统,主要分为三个类:Dish、Menu和Main。Dish类表示一个菜品,包含菜品名称和单价,以及计算不同份量菜品价格的方法getPrice()。
class Dish{
String name;
int unit_price;
Dish(){}
Dish(String name,int price)
{
this.name = name;
this.unit_price = price;
}
float getPrice(int portion){
float bl[] = {1,1.5f,2};
return Math.round(unit_price*bl[portion-1]);
}
}
Menu类表示一个菜单,包含多个菜品信息,可以添加新菜品。其中用一个Dish数组dishes保存所有菜品信息,实现了添加菜品的方法add()和查找菜品的方法serveDish()。
class Menu{
public Dish[] dishes;//菜品数组,保存所有菜品信息
public void add(Dish dish)
{
int In = 0;
if(dishes!=null)
In = dishes.length;
Dish[] tmp = new Dish[In+1];
if(In>0)
{
System.arraycopy(dishes,0,tmp,0,In);
}
tmp[In] = dish;
dishs = tmp;
}
Dish serveDish(String dishName){
if(dishes==null)
return null;
for(int i=0;i<dishes.length;i++)
{
if(dishes[i].name.equals(dishName))
return dishes[i];
}
return null;
}
}
Main类是程序的入口,实现了主要逻辑。首先创建一个Menu对象,添加了四个菜品,然后进入循环,接收用户输入的菜品和份数,从菜单中查找菜品并计算价格,累加到总价中。当用户输入"end"时,退出循环,输出总价。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
float totalPrice=0;
Menu menu = new Menu();
Dish d = new Dish("西红柿炒蛋",15);
menu.add(d);
d = new Dish("清炒土豆丝",12);
menu.add(d);
d = new Dish("麻婆豆腐",12);
menu.add(d);
d = new Dish("油淋生菜",9);
menu.add(d);
Scanner scan = new Scanner(System.in);
while(true){
String str = scan.nextLine();
if(str.equals("end")){
break;
}
String [] s = str.split(" ");
String dishName = s[0];
int portion = Integer.parseInt(s[1]);
boolean flag = false;
for(int i=0;i<menu.dishes.length;i++){
if(dishName.equals(menu.dishes[i].name)){
float price = menu.dishes[i].getPrice(portion);
totalPrice += price;
flag = true;
}
}
if(flag==false){
System.out.println(dishName+" does not exist");
}
}
System.out.println((int)totalPrice);
}
}
整个程序相对第二个较简单,主要考察对类和对象的理解,以及数组的使用。
7-2菜单计价程序-2
import java.util.Scanner;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Menu menu = new Menu();
Order order = new Order(menu);
while (true)
{
String line = input.nextLine();
if (line.equals("end")) {
break;
}
String[] array = line.split(" ");
String name = array[0];
if (name.matches("\\d+"))
{
if (array.length == 2 && array[1].equals("delete")) {
int num = Integer.parseInt(array[0]);
Record r = order.findRecordByNum(num);
if (r != null) {
order.delARecordByOrderNum(num);
}
else {
System.out.println("delete error");
}
} else {
int orderNum = Integer.parseInt(array[0]);
String dishName = array[1];
int sizeType = Integer.parseInt(array[2]);
int num = Integer.parseInt(array[3]);
Dish d = menu.searchDish(dishName);
if (d == null) {
System.out.println(dishName + " does not exist");
continue;
}
Record r = order.addARecord(orderNum, d, sizeType, num);
System.out.println(r.orderNum + " " + r.d.name + " " + r.getPrice());
orderNum++;
}
}
else
{
int price = Integer.parseInt(array[1]);
menu.addDish(name, price);
}
}
System.out.print( order.getTotalPrice());
}
}
class Dish {
String name;
int price;
public int getPrice(int sizeType) {
if (sizeType == 1) {
return Math.round(price);
} else if (sizeType == 2) {
return Math.round(price * 1.5f);
} else if (sizeType == 3) {
return Math.round(price * 2.0f);
} else {
return Math.round(price);
}
}
}
class Menu {
HashMap<String, Dish> map = new HashMap<String, Dish>();
public Dish searchDish(String name) {
return map.get(name);
}
public Dish addDish(String name, int price)
{
Dish dish = new Dish();
dish.name = name;
dish.price = price;
map.put(name, dish);
return dish;
}
}
class Record {
int orderNum;
Dish d;
int sizeType;
int num;
public Record(int orderNum, Dish d, int sizeType, int num) {
this.orderNum = orderNum;
this.d = d;
this.sizeType = sizeType;
this.num = num;
}
public int getPrice() {
return d.getPrice(sizeType) * num;
}
}
class Order {
Record[] records = new Record[100];
int size = 0;
Menu menu;
public Order(Menu menu) {
this.menu = menu;
}
public int getTotalPrice() {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += records[i].getPrice();
}
return sum;
}
public Record addARecord(int orderNum, Dish d, int sizeType, int num) {
if (size > 100) {
return null;
}
Record r = new Record(orderNum, d, sizeType, num);
records[size] = r;
size++;
return r;
}
public void delARecordByOrderNum(int orderNum) {
int pos = -1;
for (int i = 0; i < size; i++) {
if (records[i].orderNum == orderNum) {
pos = i;
break;
}
}
if (pos != -1) {
if (pos != size - 1) {
for (int i = pos + 1; i < size; i++) {
records[i - 1] = records[i];
}
}
size--;
}
}
public Record findRecordByNum(int orderNum) {
for (int i = 0; i < size; i++) {
if (records[i].orderNum == orderNum) {
return records[i];
}
}
return null;
}
}
(2)设计与分析
这段代码实现了一个简单的餐厅点餐系统,主要包括了菜单管理和订单管理两个模块。其中,菜单管理使用了HashMap来存储菜品信息,订单管理使用了Record数组来存储订单记录。具体实现逻辑如下:
1.首先创建一个Menu对象和一个Order对象,用于管理菜单和订单信息。
2.进入循环,不断读入用户输入的命令,直到输入"end"为止。
3.如果输入的命令是一个数字,则表示用户要下单。此时需要解析命令,取出订单号、菜品名称、规格和数量等信息,并根据菜单信息计算出订单金额。然后将订单信息添加到Order对象中,并输出订单号、菜品名称和金额等信息。
4.如果输入的命令不是数字,则表示用户要添加新的菜品。此时需要解析命令,取出菜品名称和价格等信息,并将菜品信息添加到Menu对象中。
5.当用户输入"end"时,跳出循环,计算订单总金额,并输出。
6.在Order对象中,可以通过订单号来查找订单记录,也可以通过订单号来删除订单记录。在添加订单记录时,需要先检查Order对象中是否还有空位,如果没有则无法添加。
7.在菜单管理中,使用HashMap来存储菜品信息,可以通过菜品名称来查找菜品信息。菜品信息包括菜品名称和价格等信息。
(3)踩坑心得:
- Record类和Order类最好分开,实现更加清晰的代码结构。
- 可以增加一些错误处理机制,比如输入错误时给出提示信息,避免程序崩溃。
(4)主要困难及改进意见:
这段代码实现了一个餐厅点餐系统,相比上一个代码,主要改进了以下几个方面:
1.使用了更加合理的类和对象设计,将订单信息封装在Record类中,将菜单信息封装在Menu类中,将订单和菜单信息作为Order类的属性,实现了更加清晰的代码结构。
2.使用了HashMap来存储菜单信息,实现了更快的查找速度。
3.使用正则表达式判断输入字符串是否为数字,避免了类型转换错误。
4.使用了更加规范的命名方式,提高了代码的可读性。
5.使用了更加优雅的代码风格,减少了代码冗余。
(5)总结:仔细读题,先根据题目要求分析再设计类然后在关注题目所给测试用例改进细节,总之就是要耐心。
7-3 jmu-java-日期类的基本使用
(1)代码
import java.util.Scanner;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main{
public static boolean stringTure(String a){
int location = a.lastIndexOf('-');
int len = a.length();
if(len - location - 1 == 2)
return true;
else
return false;
}
public static boolean isTrue(String a1,String a2,int month1,int day1, int year2,int month2,int day2){
if(stringTrue(a1)&&srtingTrue(a2)){
if(year1==year2){
if(month1==month2){
if(day1==day2){
return true;
}
else if(day1==day2){
return true;
}
else{
return false;
}
}
else if (month1<month2){
return true;
}
else{
return false;
}
}
else if(year1<year2) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
public static boolean isTrue(String a,int year, int month, int day ){
if(stringTure(a)){
if(year < 0||month <= 0||month > 12||day <= 0||day > 31){
System.out.println(a+"无效!");
return false;
}
else {
if(month == 4||month==6||month==9||month==11){
if(day > 30){
System.out.println(a+"无效!");
return false;
}
}
else if (month == 2){
if(!isLeapYear(year)){
if(day > 28){
System.out.println(a+"无效!");
return false;
}
}
else {
if(day > 29){
System.out.println(a+"无效!");
return false;
}
}
}
else{
return true;
}
return true;
}
}
else{
return false;
}
}
public static boolean isLeapYear(int year){
return year % 400==0||(year%4==0&&year%100!=0);
}
public static void printMessage(String a,int year,int month, int day){
if(isTrue(a,year,month,day)){
if(isLeapYear(year)) {
System.out.printf(a);
System.out.println("是闰年.");
}
System.out.printf(a);
System.out.print("是当年第");
printYear(year,month,day);
printMonth(day);
printWeek(year,month,day);
}
else{
System.out.println(a+"无效!");
}
}
public static void printYear(int year, int month,int day){
int sumofday = 0;
for(int i = 1;i < month;i++){
sumofday += getNumberOfMonthDays(year,i);
}
sumofday += day;
System.out.print(day+"天,当周第");
}
public static void printWeek(int year ,int month, int day){
Calendar c = new GregorianCalendar(year ,month-1, day);//因为一月为0
int week= c.get(Calendar.DAY_OF_WEEK)-1;
if (week==0)
week=7;
system.out.println(week+"天.");
}
public static int getTotalNumberOfDays(int year1, int month1, int day1,int year2, int month2, int day2){
int total = 0;
int del = 0;
for(int i = year1;i < year2;i++){
if(isLearYear(i))
total += 366;
else
total += 365;
}
for(int i = 1;i < month2;i++){
total += getNumberOfMonthDays(year2,i);
}
total += day2;
for(int i = 1;i < month1;i++){
del +=getNumberOfMonthDays(year1,i);
}
del+=day1;
total-=del;
return total;
}
public static int getNumberOfMonthDays(int year,int month){
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
return 31;
else if (month==2){
if(isLeapYear(year))
return 29;
else
return 28;
}
else {
return 30;
}
}
public static void printMessage(String a1,String a2,int year1,int month1, int day1,int year2,int month2,inty day2){
if(isTrue(a1,year1,month1,day1)&&isTrue(a2,year2,month2,day2)){
if(isTrue(a1,a2,year1,month1,day1,year2,month2,day2)){
System.out.printf(a2);
System.out.printf("与");
System.out.printf(a1);
System.out.printf("之间相差");
difDays(year1,month1,day1,year2,month2,day2);
difMonth(year1,month1,day1,year2,month2,day2);
difYears(year1,month1,day1,year2,month2,day2);
}
else{
System.out.printf(a2);
System.out.printf("早于");
System.out.printf(a1);
System.out.printf(",不合法");
}
}
else{
System.out.printf(a1);
System.out.printf("或");
System.out.printf(a2);
System.out.printf("中有不合法的日期");
}
}
public static void difDays(int year1, int month1, int day1,int year2, int month2, int day2){
int difdays;
difdays = getTotalNumberOfDays(year1,month1,day1,year2,month2,day2);
System.out.print(difdays+"天,所在月份相差");
}
public static void difMonths(int year1, int month1, int day1,int year2, int month2, int day2){
int difmomnth = month2-month1;;
System.out.print(difmonth+"天,所在年份相差");
}
public static void difYears(int year1, int month1, int day1,int year2, int month2, int day2){
int difyear = year2-year1;
System.out.print(difyear+".");
}
public static void main (String[] args){
Scanner in = new Scanner(System.in);
String a = in.nextLine();
String[] dates = a.split("-");
int[] date = new int[3];
for(int i = 0; i<dates.length;i++){
date[i] = Integer.parseInt(dates[i]);
}
int year = date[0];
int month = date[1];
int day = date[2];
printMessage(a,year,month,day);
String a1 = in.next();
dates = a1.split("-");
for (int i = 0;i < dates.length;i++){
date[i] = Integer.parseInt(dates[i]);
}
int year1 = date[0];
int month1 = date[1];
int day1 = date[2];
String a2 = in.next();
dates = a2.split("-");
for(int i = 0;i<dates.length;i++){
date[i] = Integer.parseInt(dates[i]);
}
int year2 = date[0];
int month2 = date[1];
int day2 = date[2];
printMessage(a1,a2,year1,month1,day1,year2,month2,day2);
}
}
(2)设计与分析
这段代码中共有一个类,名为Main。它包含了多个静态方法,具体作用如下:
1.stringTrue(String a):判断字符串a中最后一个"-"后面的字符数是否为2,如果是则返回true,否则返回false。
2.isTrue(String a1, String a2, int month1, int day1, int year2, int month2, int day2):判断两个日期字符串a1和a2是否合法,并且判断a1是否早于a2。如果两个日期都合法且a1早于a2,则返回true,否则返回false。
3.isTrue(String a, int year, int month, int day):判断日期字符串a是否合法,并且判断年月日是否符合规范。如果日期合法且符合规范,则返回true,否则返回false。
4.isLeapYear(int year):判断年份是否为闰年。如果是闰年,则返回true,否则返回false。
5.printMessage(String a, int year, int month, int day):输出日期字符串a所代表的日期是不是闰年,以及该日期在当年中是第几天,第几周。
6.printYear(int year, int month, int day):计算该日期在当年中是第几天,并输出。
7.printWeek(int year, int month, int day):计算该日期是星期几,并输出。
8.getTotalNumberOfDays(int year1, int month1, int day1, int year2, int month2, int day2):计算两个日期之间相差的天数。
9.getNumberOfMonthDays(int year, int month):计算该年该月有多少天。
10.printMessage(String a1,String a2,int year1,int month1, int day1,int year2,int month2,int day2):判断给出日期是否为合法日期。
(3)踩坑心得:
1.一定不能忘了不规范日期的情况
2.变量名的拼写错误有点多,导致前后不一致,所以变量名尽量精简。
(4)改进意见:
部分函数有代码重复,可以优化;函数printYear、printMonth、printWeek缺少实现,需要补充。
(5)总结:
函数名要准确,注意拼写错误。
7-4 小明走格子
(1)源码:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws IOException{
StreamTokenizer re = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
re.nextToken();
int p = (int)re.nval;
int a[] = new int[10010];
int b[] = new int[100];
int i,j,max = 0;
for(i=0;i<p;i++)
{
re.nextToken();
a[i] = (int)re.nval;
if(max < a[i]) max = a[i];
}
b[0] = 1;b[1] = 1;b[2] = 2;b[3] = 4;b[4] = 8;
for(i=5;i<=max;i++) b[i] = 2 * b[i - 1] - b[i - 5];
for(i=0;i<p;i++)
System.out.println(b[a[i]]);
}
}
(3)设计与分析
由题意得,每一次跳跃的步数只能是1、2、3、4、5中的一个,且不能连续跳两次相同的步数。因此可以用动态规划求解,设 bi 表示跳到第 �i 个台阶的方案数,则有状态转移方程:
b[i] = 2 * b[i - 1] - b[i - 5]
第三次题目集
7-1菜单计价程序-3
(1)源码:
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class Die {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Menu menu = new Menu();
Order[] orders = new Order[100];
int orderIdx = 0;
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.equals("end")) {
break;
}
String[] parts = line.split(" ");
if (parts[0].equals("table")) {
String[] time = parts[3].split("/");
int year = Integer.parseInt(time[0]);
int month = Integer.parseInt(time[1]);
int day = Integer.parseInt(time[2]);
time = parts[4].split(":");
int hour = Integer.parseInt(time[0]);
int minute = Integer.parseInt(time[1]);
int second = Integer.parseInt(time[2]);
orders[orderIdx] = new Order(Integer.parseInt(parts[1]), year, month, day, hour, minute, second);
orderIdx++;
} else if (parts[1].equals("delete")) {
int orderNum = Integer.parseInt(parts[0]);
Record record = orders[orderIdx - 1].findRecordByNum(orderNum);
if (record == null) {
System.out.println("delete error");
} else {
orders[orderIdx - 1].delARecordByOrderNum(orderNum);
}
} else {
String dishName = parts[1];
Dish dish = menu.searthDish(dishName);
if (dish == null) {
dish = menu.addDish(dishName, Integer.parseInt(parts[2]));
}
int orderNum = Integer.parseInt(parts[0]);
int portion = Integer.parseInt(parts[2]);
int num = Integer.parseInt(parts[3]);
orders[orderIdx -1].addARecord(orderNum, dish, portion, num);
}
}
for (int i = 0; i < orderIdx; i++) {
System.out.println(orders[i].toString());
}
}
public static class Menu {
private List<Dish> dishes;
public Menu() {
dishes = new ArrayList<>();
}
public Dish searthDish(String name) {
for (Dish dish : dishes) {
if (dish.getName().equals(name)) {
return dish;
}
}
return null;
}
public Dish addDish(String name, int price) {
Dish dish = new Dish(name, price);
dishes.add(dish);
return dish;
}
}
public static class Dish {
private String name;
private int price;
public Dish(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
}
public static class Order {
private int tableNum;
private List<Record> records;
private Date date;
public Order(int tableNum, int year, int month, int day, int hour, int minute, int second) {
this.tableNum = tableNum;
records = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day, hour, minute, second);
}
public void addARecord(int orderNum, Dish dish, int portion, int num) {
Record record = new Record(orderNum, dish, portion, num);
records.add(record);
}
public Record findRecordByNum(int orderNum) {
for (Record record : records) {
if (((Object) record).getOrderNum() == orderNum) {
return record;
}
}
return null;
}
public void delARecordByOrderNum(int orderNum) {
Record record = findRecordByNum(orderNum);
if (record != null) {
records.remove(record);
}
}
public int getTableNum() {
return tableNum;
}
public Date getDate() {
return date;
}
}
}
(2)设计与分析:
这段代码实现了一个简单的点餐系统,包含了菜单、菜品、订单和记录等类。主要功能包括添加菜品、查询菜品、添加订单和记录、删除订单记录等。其中菜品使用了列表存储,订单和记录使用了列表和时间日期类存储。整个系统的流程是从输入开始,根据输入内容进行相应的操作,最后输出订单内容。
(3)踩坑心得:
1.一定要注意审题,不要遗漏题目要求;
2.要写根据题目要求去设计每一个类并理解类与类之间的关系再去设计代码
(4)改进建议:
多加注释,便于修改与解读。
(5)总结
代码结构虽然还算清晰,逻辑也算简单易懂,但是缺少了一些必要的注释和异常处理。
7-2 7-3 重复数据以及去除重复数据
(1)代码:
7-2:import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<Integer>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
set.add(sc.nextInt());
}
sc.close();
if (set.size() < n) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
7-3:import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int f = sc.nextInt();
int flag=0;
String str = sc.nextLine();
while(sc.hasNext())
{
String s = sc.nextLine();
str = str + s;
}
String[] arr = str.split(" ");
LinkedHashSet<String> list = new LinkedHashSet<String>();
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
for (String w : list) {
if(flag==0)
{
System.out.print(w);
flag = 1;
}
else System.out.print(" "+w);
}
} }
(2)设计与分析
7-2:这段代码实现了判断给定的一组数中是否有重复元素的功能。具体来说,代码中定义了一个HashSet集合,读入n个数并将其加入集合中,最后判断集合的大小是否小于n,如果小于n,则说明有重复元素,输出“YES”,否则输出“NO”。因为HashSet集合不允许有重复元素,所以如果集合大小小于n,则说明有重复元素。
而7-3:读取用户输入的字符串,去重后输出
1.通过Scanner类获取用户输入的整数f,表示接下来要输入的字符串个数。
2.通过Scanner类的nextLine()方法读取一行字符串,这里是为了读取上一步输入整数f后剩余的换行符。
3.使用while循环,通过Scanner类的nextLine()方法逐行读取用户输入的字符串,并将它们拼接成一个字符串str。
使用String类的split()方法将字符串str按照空格分割成一个字符串数组arr。
4.创建一个LinkedHashSet集合list,用于存储去重后的字符串。
5.使用for循环将字符串数组arr中的每个字符串添加到集合list中,由于集合的特性,重复的字符串只会被添加一次。
6.使用for-each循环遍历集合list中的每个字符串,并输出到控制台。由于要保证输出的字符串之间只有一个空格,这里使用了一个flag变量来控制输出格式。如果是第一个字符串,则不需要加空格,之后的字符串都需要在前面加一个空格。
7-4 单词统计与排序
此题难度适中,想要实现单词统计与排序的功能,首先要从控制台输入一个字符串,使用replaceAll方法去掉字符串中的逗号和句号,并使用split方法将字符串按空格分割成单词数组。
再创建一个TreeMap对象tMap,用于存储单词和单词长度,其中TreeMap会根据键的自然顺序进行排序,这里使用String.CASE_INSENSITIVE_ORDER 参数表示不区分大小写。
然后遍历单词数组,将每个单词作为键,单词长度作为值存储到tMap中。
接着创建一个List对象tList,将tMap中的entrySet转换为List,用于对单词按长度进行排序。
然后使用Lambda表达式对tList进行排序,排序规则为按照单词长度从大到小排序。
最后使用迭代器遍历tList,输出每个单词。由于排序规则为按照单词长度从大到小排序,所以输出的单词会按照长度从大到小的顺序输出。
7-5面向对象编程(封装性)
设计与分析:
1.通过Scanner类从控制台输入学生信息,包括学号、姓名、年龄和专业;
2.定义了一个名为Student的内部类,包含学号、姓名、年龄和专业等属性,以及构造方法、setter方法和print方法;
3.调用无参构造方法,并通过setter方法进行设值,创建了一个学生对象student1;
4.调用有参构造方法,创建了一个学生对象student2;
5.分别对学生student1和学生student2进行输出,输出学生的学号、姓名、年龄和专业信息。
最后实现输入学生信息并输出学生信息的作用。
7-6 GPS测绘中度分秒转换
(1)代码
import java.util.Scanner;
public class Main{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
double c = in.nextDouble();
double d = a+(b*1.0/60)+(c*1.0/3600);
String result = String.format("%.6f",d);
System.out.println(a+"°"+b+"′"+c+"″"+" "+"="+" "+result);
} }
此题较简单,只需要根据用户然后通过
d = a+(b*1.0/60)+(c*1.0/3600)表达式即可完成求解。
7-7 判断两个日期的先后,计算间隔天数、周数
(1)源码
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
String[] c = a.split("-");
String[] d = b.split("-");
LocalDate date_1 = LocalDate.of(Integer.parseInt(c[0]), Integer.parseInt(c[1]), Integer.parseInt(c[2]));
LocalDate date_2 = LocalDate.of(Integer.parseInt(d[0]), Integer.parseInt(d[1]), Integer.parseInt(d[2]));
if (date_1.isBefore(date_2)) {
System.out.println("第一个日期比第二个日期更早");
long day = date_1.until(date_2, ChronoUnit.DAYS);
long week = date_1.until(date_2, ChronoUnit.WEEKS);
System.out.println("两个日期间隔" + day+ "天");
System.out.println("两个日期间隔" + week + "周");
} else {
System.out.println("第一个日期比第二个日期更晚");
long day = date_2.until(date_1, ChronoUnit.DAYS);
long week = date_2.until(date_1, ChronoUnit.WEEKS);
System.out.println("两个日期间隔" + day + "天");
System.out.println("两个日期间隔" + week + "周");
}
}
}
(2)设计与分析
此题也比较简单,直接通过 Scanner 类获取用户输入的两个日期字符串 a 和 b。再使用 String 类的 split() 方法将日期字符串按照 "-" 分割成年、月、日三个部分,存储到数组 c 和 d 中,然后使用 LocalDate 类的 of() 方法将数组 c 和 d 中的年、月、日信息转换成 LocalDate 对象 date_1 和 date_2,接着使用 isBefore() 方法比较 date_1 和 date_2 的大小,如果 date_1 比 date_2 更早,则输出 "第一个日期比第二个日期更早",否则输出 "第一个日期比第二个日期更晚"。
使用 until() 方法计算 date_1 和 date_2 之间的天数和周数,并输出结果。
7-5,6,7
(3)踩坑心得:
7-6注意要使用 Java 8 中的新时间 API,可以方便地处理日期和时间相关的问题。同时,由于输入的日期格式固定为 "yyyy-mm-dd",因此没有进行输入格式的检查和处理。
(4)主要困难及改进意见:
刚开始一些测试点不明确拿不到满分,总是出现非零返回或者答案错误,但是测试结果正确的情况。
还是要严格按照题目要求去编写代码,代码不能过于复杂,否则会造成非零返回。
(5)总结:第三次题目集总体而言除了点菜系统不算太难,这些题目主要还是考察对类与对象的理解以及封装,同时也需要我们对数据输入转换,有参无参构造有更深刻的理解并学会更好的运用。