gpt4 book ai didi

java - 如何打印/引用第 7 号人物?

转载 作者:行者123 更新时间:2023-12-01 20:11:12 25 4
gpt4 key购买 nike

如何打印/引用第 7 号人员?谢谢!

我已经尝试了多种方法,但是我无法引用第 7 个人。我是数组新手,明天到期,所以任何帮助都会很棒。我看了 youtube 上的一些教程,但我就是不明白出了什么问题?重新定义数组的方式与我看到的示例不同。

import java.text.*;           // to use Decimal Format

public class TwoD_ArrayDriver

{

public static void main(String[] args)

{

DecimalFormat myFormat;

// to get 2 decimals every time

myFormat = new DecimalFormat("#.00");



Person p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21;

p1 = new Person("Abby", "Arthur"); p2 = new Person("Bubba","Brown"); p3 = new Person("Chuckie", "Cheese"); p4 = new Person("Don", "Drysdale"); p5 = new Person("Ernie","Eastwood");

p6 = new Person("Flo", "Fauntroy"); p7 = new Person("Gabby", "Giffords"); p8 = new Person("Hank","Hoover"); p9 = new Person("Indy", "Imhauf"); p10 = new Person("Jim","Jones");

p11 = new Person("Ken", "Koopman"); p12 = new Person("Larry", "Lancelot"); p13 = new Person("Michael", "Moore" ); p14 = new Person("Nina","Nonesuch"); p15 = new Person("Oscar","OToole");

p16 = new Person("Pat","Pompous"); p17 = new Person("Quincy","Quinton"); p18 = new Person("Ralph","Rancid"); p19 = new Person("Steven","Simpson"); p20 = new Person("Tim","Tinker");

p21 = new Person("Uncle","Usher");

Person[] allPersons = {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21};

Person[] mondayAppts = {p1, p2, p3, p4, p5};

Person[] tuesdayAppts = {p6, p7, p8};

Person[] wednesdayAppts = {p9, p10, p11, p12};

Person[] thursdayAppts = {p13, p14, p15, p16, p17};

Person[] fridayAppts = {p18, p19, p20, p21};



//Task #1 Print the id, first name, and last name of Person #7, using tuesdayAppts

int intToPrint = 0;

String fNameToPrint = "unknown";

String lNameToPrint = "unknown";

System.out.println("Person #7 "+intToPrint+" "+fNameToPrint+" "+lNameToPrint);

最佳答案

由于您尝试通过 tuesdayAppts 作为 Person[] tuesdayAppts = {p6, p7, p8}; 访问变量 p7 ,您应该执行 tuesdayAppts[1],因为它位于数组的第二个位置。

示例(输出:Person #2 BBB bbb)

class Person {

private static int idGen = 0;
private int id;
private String firstName;
private String lastName;

public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.id = ++Person.idGen;
}

public String toString() {
return "Person #" + this.id + " " + this.firstName + " " + this.lastName;
}

}

class Example {

public static void main(String[] args) {
Person a, b, c;
a = new Person("AAA", "aaa");
b = new Person("BBB", "bbb");
c = new Person("CCC", "ccc");
Person[] personArray = {a, b, c};
System.out.println(personArray[1]);
}

}

关于java - 如何打印/引用第 7 号人物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46678852/

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