多次更改索引值
Posted
技术标签:
【中文标题】多次更改索引值【英文标题】:Changing the index value multiple times 【发布时间】: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 【参考方案1】:看起来您编写的代码可以顺利通过这些步骤。如果你能够让某件事工作 1 次,但需要它发生不止一次,你将需要一个 循环。这样您就可以根据需要多次执行同一组操作。
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";
// Loop through this logic 3 times
for(int i=1; i<= 3; i++)
// 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 + ">");
通过在循环中包含int index = ((int)Math.random() * Capitals.length);
行,这允许我们运行同一组指令 3 次,但每次都使用一个新的索引值。
【讨论】:
我试过你说的例子,但每次这样做,它只是一遍又一遍地具有相同的索引值。 编辑:没关系你的方式确实解决了我的问题我只需要添加一个额外的(或两个到 math.random @ChristopherAcosta 记得接受他的回答,因为它解决了你的问题。以上是关于多次更改索引值的主要内容,如果未能解决你的问题,请参考以下文章