1.程序可以工作,但每次输入用户后,它都会打印出第二种方法,我不想这样做,也不知道如何更改它。
import java.util.Scanner;//import scanner so user can input
class arrays
{
public static void main(String[] param)
{
String[] animals = arrays(); // creating string array to store information
forloop(animals);// passing the array to the method forloop
System.exit(0);
} //end main method
public static String[] arrays() //array method
{
String[] animals = new String[5]; //array to store 5 animals
animals[0] = "Komodo Dragon"; //animals stored
animals[1] = "Manatee";
animals[2] = "Kakapo";
animals[3] = "Florida Panther";
animals[4] = "White Rhino";
return animals;
}
public static void forloop(String[] animals) // here the array has been passed as an argument
{
int endangered = 20;
String answer = "";
for(int i = 0;i<5; i++) //for loop to print the below
//print 5 times using the different animal names.
{
System.out.println(animals[i] + ":");
System.out.println("How many are left in the wild?"); // prints the question
Scanner scanner = new Scanner(System.in); // allows the user to input
answer = scanner.nextLine();
int count = Integer.parseInt(answer);
if(count<=endangered) // if statement used to print out the smallest number types by the user
{
System.out.println("The most endangered animal is the " + animals[i] + "."); // prints out the most endangered animal
System.out.println("There are only " + answer + " left in the wild.");
}
print(animals, answer, i);
}
}
public static void print(String[]animals, String answer, int i)
{
System.out.println(answer + ", " + animals[0]);
}
}2.我希望上述方法只在用户输入全部5个问题后打印。我希望它打印在逗号分隔的列表形式(适合于电子表格)。
例如:
5000,Komodo Dragon 5000是用户输入的内容。
8000,Manatee
91,Kakapo
100,佛罗里达豹
18,白犀牛
发布于 2016-12-05 20:10:28
有很多种方式。最简单的方法可能是将用户的输入附加到数组中的字符串值中。使用来自用户的计数更新各自的记录,以便:
animals[0] = "5000, Komodo Dragon";
这将需要额外的努力,如果您想要更新记录以后。您可以使用其他数据结构来维护记录,比如Maps,这甚至可能更简单。然而,一种方法是二维数组,这样就可以把它想象成5x2个表。现在,数组初始化可以是二维的,因此第一列可以保存动物名称,并且可以保留空以供计数。
public static String[][] arrays() //array method
{
String[][] animals = new String[5][2]; //array to store 5 animals
animals[0][0] = "Komodo Dragon"; //animals stored
animals[1][0] = "Manatee";
animals[2][0] = "Kakapo";
animals[3][0] = "Florida Panther";
animals[4][0] = "White Rhino";
return animals;
}forloop方法现在必须更新每个动物记录的第二列,这样我们就可以按如下方式更新它:
public static void forloop(String[][] animals) // here the array has been passed as an argument
{
int endangered = 20;
String answer = "";
Scanner scanner = new Scanner(System.in); // allows the user to input
for(int i = 0;i<5; i++) //for loop to print the below
//print 5 times using the different animal names.
{
System.out.println(animals[i][0] + ":");
System.out.println("How many are left in the wild?"); // prints the question
answer = scanner.nextLine();
int count = Integer.parseInt(answer);
animals[i][1] = String.valueOf(count);
if(count<=endangered) // if statement used to print out the smallest number types by the user
{
System.out.println("The most endangered animal is the " + animals[i][0] + "."); // prints out the most endangered animal
System.out.println("There are only " + answer + " left in the wild.");
}
}
//close the scanner
scanner.close();
print(animals);
}在上面的方法中,请注意,animals[i][1] = String.valueOf(count);更新i给出的每一行的第二列。由于同一个实例可以多次使用,所以scanner已经被带到了loop的上面。此外,在循环结束后,它被关闭,print方法被从循环中取出,并且由于我们所需要的是二维数组,所以该方法的签名也发生了变化。现在,就像在普通表中一样,我们将记录存储在行和列中,因此我们可以在数组上迭代以打印记录。因此,可以将print方法更改如下:
public static void print(String[][]animals){
for (String[] animal: animals)
System.out.println(animal[1] + ", " + animal[0]);
}由于数组现在是二维的,所以需要在main方法中进行小的更改。
发布于 2016-12-05 19:51:05
打印语句在for循环中。也许可以考虑将您的print()更改为save(),并将每个结果存储在一个答案数组中,稍后您可以将这些答案全部打印出来。例如:
results[i] = answer + ", " + animals[i];
而不是打印方法中的system.out.println。最后,您可以循环遍历这个和System.out.println(results[i]);。
https://stackoverflow.com/questions/40981766
复制相似问题