gpt4 book ai didi

java - 构造函数和数组列表

转载 作者:搜寻专家 更新时间:2023-11-01 01:59:35 27 4
gpt4 key购买 nike

<分区>

我正在尝试创建一个(美国总统)测验程序。我想创建一个数组列表(总裁编号,总裁姓名,开始年份,去年)。

由于某种原因,我的构造函数不起作用,我不得不构建另一个方法,但第二种方法导致 StackOverFlowError(这是我第一次使用这个网站来回答问题,我想我来到了给定站点名称的正确位置)

这是我的代码:

public class Presidents 
{

public int presidentNumber;
public String presidentName;
public int startingYear;
public int endingYear;

public Presidents(int PresidentNumber, String NameOfPresident, int StartingYear, int EndingYear)
{
this.presidentNumber = PresidentNumber;
this.presidentName = NameOfPresident;
this.startingYear = StartingYear;
this.endingYear = EndingYear;

}

public Presidents() {

}

public Presidents [] PresidentList ()
{
Presidents [] president = new Presidents [45];
president [0] = Presidents(1, "George Washington", 1789, 1797);
president [1] = Presidents(2, "John Adams", 1797, 1801);
president [2] = Presidents(3, "Thomas Jefferson", 1801, 1809);
president [3] = Presidents(4, "James Madison", 1809, 1817);
president [4] = Presidents(5, "James Monroe", 1817, 1825);
president [5] = Presidents(6, "John Quincy Adams", 1825, 1829);
president [6] = Presidents(7, "Andrew Jackson", 1829, 1837);
president [7] = Presidents(8, "Martin Van Buren", 1837, 1841);
president [8] = Presidents(9, "William Henry Harrison", 1841, 1841);
president [9] = Presidents(10, "John Tyler", 1841, 1845);
president [10] = Presidents(11, "James K. Polk", 1845, 1849);
president [11] = Presidents(12, "Zachary Taylor", 1849, 1850);
president [12] = Presidents(13, "Millard Fillmore", 1850, 1853);
president [13] = Presidents(14, "Franklin Pierce", 1853, 1857);
president [14] = Presidents(15, "James Buchanan", 1857, 1861);
president [15] = Presidents(16, "Abraham Lincoln", 1861, 1865);
president [16] = Presidents(17, "Andrew Johnson", 1865, 1869);
president [17] = Presidents(18, "Ulysses S. Grant", 1869, 1877);
president [18] = Presidents(19, "Rutherford B. Hayes", 1877, 1881);
president [19] = Presidents(20, "James A. Garfield", 1881, 1881);
president [20] = Presidents(21, "Chester A. Arthur", 1881, 1885);
president [21] = Presidents(22, "Grover Cleveland", 1885, 1889);
president [22] = Presidents(23, "Benjamin Harrison", 1889, 1893);
president [23] = Presidents(24, "Grover Cleveland", 1893, 1897);
president [24] = Presidents(25, "William McKinley", 1897, 1901);
president [25] = Presidents(26, "Theodore Roosevelt", 1901, 1909);
president [26] = Presidents(27, "William Howard Taft", 1909, 1913);
president [27] = Presidents(28, "Woodrow Wilson", 1913, 1921);
president [28] = Presidents(29, "Warren G. Harding", 1921, 1923);
president [29] = Presidents(30, "Calvin Coolidge", 1923, 1929);
president [30] = Presidents(31, "Herbert Hoover", 1929, 1933);
president [31] = Presidents(32, "Franklin D. Roosevelt", 1933, 1945);
president [32] = Presidents(33, "Harry S. Truman", 1945, 1953);
president [33] = Presidents(34, "Dwight D. Eisenhower", 1953, 1961);
president [34] = Presidents(35, "John F. Kennedy", 1961, 1963);
president [35] = Presidents(36, "Lyndon B. Johnson", 1963, 1969);
president [36] = Presidents(37, "Richard Nixon", 1969, 1974);
president [37] = Presidents(38, "Gerald Ford", 1974, 1977);
president [38] = Presidents(39, "Jimmy Carter", 1977, 1981);
president [39] = Presidents(40, "Ronald Reagan", 1981, 1989);
president [40] = Presidents(41, "George H. W. Bush", 1989, 1993);
president [41] = Presidents(42, "Bill Clinton", 1993, 2001);
president [42] = Presidents(43, "George W. Bush", 2001, 2009);
president [43] = Presidents(44, "Barack Obama", 2009, 2017);
president [44] = Presidents(45, "Donald Trump", 2017, 2018);

return president;
}

private Presidents Presidents(int i, String string, int j, int k) {
return Presidents (i, string, j, k);
}

}
import java.util.Scanner;
import java.util.Random;


public class PresidentsQuiz {

public static void main (String [] args)
{
System.out.println("Do you know the 45 presidents of the United States? Enter a number between 1 and 45");

Scanner kb = new Scanner (System.in);
int input = kb.nextInt();

while (input < 1 || input > 45)
{
System.out.println("Enter a number between 1 and 45");
input = kb.nextInt();
}

Presidents president = new Presidents ();
Presidents [] presidentList = president.PresidentList();
System.out.println(presidentList [0]);
kb.close();

}

}

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