【发布时间】:2022-01-20 15:15:19
【问题描述】:
好的,所以对于学校作业,我必须制作数组并打印出数组国家/地区的索引 3 次或更多次不同的时间,我不知道该怎么做。顺便说一句,如果这有帮助,这是一个符石任务
错误:需要获取 3 个或更多不同的数字来打印所述国家/地区的索引
public class Countries
{
public static void main(String[] args)
{
// 1. Declare 4 arrays and initialize them to the given values.
// Countries: China, Egypt, France, Germany, India, Japan, Kenya, Mexico, United Kingdom, United States
// Capitals: Beijing, Cairo, Paris, Berlin, New Delhi, Tokyo, Nairobi, Mexico City, London, Washington D.C.
// Languages: Chinese, Arabic, French, German, Hindi, Japanese, Swahili, Spanish, English, English
// Filenames for map images: China.jpg, Egypt.jpg, France.jpg, Germany.jpg, India.jpg, Japan.jpg, Kenya.jpg, Mexico.jpg, UK.jpg, US.jpg
String[] Countries = {"China", "Egypt", "France", "Germany", "India", "Japan", "Kenya", "Mexico", "United Kingdom", "United States"};
String[] Capitals = {"Beijing", "Cairo", "Paris", "Berlin", "New Delhi", "Tokyo", "Nairobi", "Mexico City", "London", "Washington D.C."};
String[] Languages = {"Chinese", "Arabic", "French", "German", "Hindi", "Japanese", "Swahili", "Spanish", "English", "English"};
String[] Filenames = {"China.jpg", "Egypt.jpg", "France.jpg", "Germany.jpg", "India.jpg", "Japan.jpg", "Kenya.jpg", "Mexico.jpg", "UK.jpg", "US.jpg"};
// 2. Pick a random number up to the length of one of the arrays and save in the variable index
int index = ((int)Math.random() * Capitals.length);
// 3. Print out the info in each array using the random index
System.out.println(Countries[index]);
System.out.println(Capitals[index]);
System.out.println(Languages[index]);
// Example of showing image files using an array called images (your array name above may be different)
// (this will only work in Active Code)
Countries obj = new Countries();
obj.printHTMLimage(Filenames[index]);
}
// This method will just work in Active Code which interprets html
public void printHTMLimage(String filename)
{
String baseURL = "https://raw.githubusercontent.com/bhoffman0/CSAwesome/master/_sources/Unit6-Arrays/6-1-images/";
System.out.print("<img src=" + baseURL + filename + ">");
}
}
【问题讨论】:
-
把代码放在一个循环中迭代3次?预期的输出是什么?
-
那么预期的输出应该是这个imgur.com/a/nGfdvG1