package practice;
import java.util.Scanner;
public class CourseInquiryAndDeletion {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String courseArr[] = { "Java", "SQL", "HTML", "C#", "PHP", "C", "C++" };
System.out.println("课程表如下:");
for (int i = 0; i < courseArr.length; i++) {
System.out.print(courseArr[i] + " ");
}
System.out.print("\n\n请输入课程名称:");
String course = sc.next();
boolean flag = true; // 定义程序选择打印的标识
int index = -1; // 定义元素查找标识
for (int i = 0; i < courseArr.length; i++) {
if (course.equals(courseArr[i])) {
index = i; // 查找到对应元素后将循环变量 i 的值作为下标传给courseArr
courseArr[index] = null;
}
}
if (index == -1) {
System.out.println("\n提示:相关课程暂未发布!");
flag = false; // 接收到不规则输入后将打印标识设置为false控制相关信息输出
}
if (flag) {
System.out.println("\n移除" + course + "后的课程表:");
for (int i = 0; i < courseArr.length; i++) {
if (courseArr[i] == null) {
continue;
} else {
System.out.print(courseArr[i] + " ");
}
}
}
sc.close();
}
}
Java数组实现课程查询与删除
最新推荐文章于 2022-05-08 08:30:31 发布