package itheima_shuju02_danlianbiao;
import java.util.Scanner;
public class HeroNode {
public static void main(String[] args) {
ControlLink control = new ControlLink();
Scanner input = new Scanner(System.in);
while(true){
System.out.println("添加英雄--->输入a(addhero)");
System.out.println("显示英雄--->输入s(showhero)");
String N = input.next();
switch (N){
case "a":
control.addHero(control.getHero());
break;
case "s":
control.showHero(control.getHero());
break;
default:
System.out.printf("退出系统!");
return;
}
}
}
}
class ControlLink{
private Hero hero = new Hero(0,"","");
Hero temp = hero;
public Hero getHero() {
return hero;
}
public Hero addHero(Hero hero){
while(true){
if (temp.next == null){
break;
}
temp = temp.next;
}
Scanner input = new Scanner(System.in);
System.out.printf("请输入英雄编号:");
int number = input.nextInt();
System.out.printf("请输入英雄姓名:");
String name = input.next();
System.out.printf("请输入英雄绰号:");
String nickname = input.next();
temp.next = new Hero(number,name,nickname);
return temp;
}
public void showHero(Hero hero){
hero = hero.next;
while (true){
if (hero == null){
break;
}
System.out.println(hero.toString());
hero =hero.next;
}
}
}
class Hero{
int number;
String name;
String nickname;
Hero next;
public Hero(int number, String name, String nickname) {
this.number = number;
this.name = name;
this.nickname = nickname;
}
@Override
public String toString() {
return "Hero{" +
"number=" + number +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
11-23
301
07-07
667
11-08
271
11-08
140