2019.12.17 面向对象习题

/**
* TimeTraveler.java
* com.DuiXiang
*
* Function: TODO
*
* ver date author
* ──────────────────────────────────
* 2019年12月17日 17671
*
* Copyright (c) 2019, TNT All Rights Reserved.
*/

package com.DuiXiang;

/**
* ClassName:TimeTraveler Function: TODO ADD FUNCTION Reason: TODO ADD REASON
*
* @author 17671
* @version
* @since Ver 1.1
* @Date 2019年12月17日 下午4:25:49
*
* @see
*/
public class TimeTraveler {
public String[][] times = new String[5][2];

// 添加事件
public void add(String time, String event) {
boolean full = false;
for (int i = 0; i < times.length; i++) {
if (times[i][0] == null) {
times[i][0] = time;
times[i][1] = event;
full = false;
break;
}
}
if (full) {
System.out.println("数组已满");
} else {
System.out.println("添加成功!");
}
}

// 修改事件
public void update(String time, String event) {
boolean find = false;
for (int i = 0; i < times.length; i++) {
if (times[i][0] != null) {
if (times[i][0].equals(time)) {
times[i][1] = event;
find = true;
System.out.println("修改成功");
}
}
}
if (!find) {
System.out.println("没有找到该位置的数据!");
}
}

// 展示事件
public void show() {
for (int i = 0; i < times.length - 1; i++) {
for (int j = 0; j < times.length - i - 1; j++) {
if (times[j][0] != null && times[j + 1][0] != null) {
if (Integer.parseInt(times[j][0]) > Integer.parseInt(times[j + 1][0])) {
String[] temp = times[j];
times[j] = times[j + 1];
times[j + 1] = temp;
}
}
}
}
System.out.println("时间\t\t\t 事件");
for (int i = 0; i < times.length; i++) {

if (times[i][0] == null) {
System.out.println("该位置没有数据");
} else {
System.out.print(times[i][0] + "\t\t" + times[i][1]);

}
System.out.println();
}
System.out.println("**********************************");
}
}

************************************************************************************************************************************************************************************************************

/**
* Test12.java
* com.DuiXiang
*
* Function: TODO
*
* ver date author
* ──────────────────────────────────
* 2019年12月17日 17671
*
* Copyright (c) 2019, TNT All Rights Reserved.
*/

package com.DuiXiang;

import java.util.Scanner;

/**
* ClassName:Test12
* Function: TODO ADD FUNCTION
* Reason: TODO ADD REASON
*
* @author 17671
* @version
* @since Ver 1.1
* @Date 2019年12月17日 下午4:49:17
*
* @see
*/
public class Test12 {
public static TimeTraveler t1=new TimeTraveler();
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
menu();
}
public static void menu() {
boolean flag=true;
while(flag) {
System.out.println("1.添加时间和事件");
System.out.println("2.修改时间和事件");
System.out.println("3.展示时间和事件");
System.out.println("4.退出");
System.out.println("请选择:");
String choice=scanner.nextLine();

switch (choice) {
case "1":
// System.out.println("请输入时间:");
// String time=scanner.nextLine();
// System.out.println("请输入事件:");
// String event=scanner.nextLine();
// t1.add(time, event);
System.out.println("请按照格式 时间(20191217);事件 录入数据:");
String data=scanner.nextLine();
String[] temp=data.split(";");
t1.add(temp[0], temp[1]);
break;
case "2":
System.out.println("请输入要修改的时间:");
String time1=scanner.nextLine();
System.out.println("请输入要修改的事件:");
String event1=scanner.nextLine();
t1.update(time1, event1);
break;
case "3":
t1.show();
break;
case "4":
flag=false;
System.out.println("欢迎下次再来穿越!");
break;

default:
System.out.println("无效命令,请重新输入!");
break;
}
}
}

}

猜你喜欢

转载自www.cnblogs.com/aojie/p/12055838.html