我如何打印/推荐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]);
}
}