gpt4 book ai didi

Java打印 vector 的输入并删除具有特定编号的 vector

转载 作者:太空宇宙 更新时间:2023-11-04 09:19:32 25 4
gpt4 key购买 nike

我有一个类Runner,它有namestartnrweightage。我应该能够创建并打印 Runner。

public class Runner {
private String name;
private int startnr;
private int weight;
private int age;

public Runner(String name, int startnr, int weight, int age){
this.name = name;
this.startnr = startnr;
this.weight = weight;
this.age = age;
print();
}

public static void main(String[] args){
createRunner();
}

public void print(){
System.out.println("Name: " + name);
System.out.println("Startnr: " + startnr);
System.out.println("Weight: " + weight);
System.out.println("Age " + age);
}

public static void createRunner(){
Runner One = new Runner("Two", 0,76,24);
Runner Two = new Runner("One", 1, 80, 21);
}
}

然后我有一个类 ControlRunner ,它应该允许管理运行者,即

  • 可以(使用扫描仪)读取运行者
  • 每个运行者都有一个独特的startnr
  • 运行者应该位于Vector
  • 应该可以打印出所有的运行者
  • 应该可以删除具有特定 startnr 的运行者

这是我到目前为止所拥有的:

import java.util.Scanner;
import java.util.Vector;

public class ControlRunner {

static Scanner sc = new Scanner(System.in);
private static int startnr = 1;
private static Vector v;
private static String output, name;
private static int weight, age;

static Runner myrunner;

public static void main(String[] args){
createLaeufer();
}

public static void createLaeufer(){

System.out.println("Write the amount of runners you want to create");

Vector<Runner> vector = new Vector<>();

int amount = sc.nextInt();

for (int i = 1; i <= amount; i++) {

System.out.println("Write the name of the runner: ");
name = sc.next();

System.out.println("Write the weight of the runner: ");
weight = sc.nextInt();

System.out.println("Write the age of the runner: ");
age = sc.nextInt();

myrunner = new Runner(name, startnr, weight, age);
vector.add(myrunner);
// v.forEach(Runner -> myrunner.print());
startnr++;

}

System.out.println("Do you want to print all runners? (Y/N) ");
output = sc.next();
if (output.equals("Y")) {
vector.forEach(Runner -> myrunner.print());
} else {
return;
}


System.out.println("Do you want to delete a runner? (Y/N) ");
String delete = sc.next();
if (delete.equals("Y")){
System.out.print("Write the starting number: ");
int nummer = sc.nextInt();
vector.remove(nummer - 1);
} else {
return;
}
}
}

我遇到的问题是我不知道如何通过 vector 打印所有运行者。 (目前,它只打印一个)。另一个问题是它在删除运行程序后终止,因为 else 只是给出返回。 (我认为这个结构不好,因为我是 Java 新手)。我该如何改进?

最佳答案

在这部分中,您不使用 vector ,仅打印最后的数据。

System.out.println("Do you want to print all runners? (Y/N) ");
output = sc.next();
if (output.equals("Y")){
System.out.println("Name: " + name);
System.out.println("Startnr: enter code enter code herehere" + (startnr - 1));
System.out.println("Weight: " + weight);
System.out.println("Age " + age);
} else {
return;
}

如果你想打印所有 vector 信息,你需要这样的东西:

v.forEach(runner -> runner.print())

此外,您还可以在循环内创建 vector ,因此您可以为每个运行者创建 vector ,然后忘记它。我认为你需要在创建运行者的循环之前创建 vector 。

关于Java打印 vector 的输入并删除具有特定编号的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58508768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com