public static void forloop(String[] animals) // here the array has been passed as an argument
{
for(int i =0; i<5; i++) //for loop to print the below
//print 5 times using the different animal names.
{
int endangered = 20;
String answer;
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 10 left in the wild.");
}
}
}
1. I want this to show up after the loop has finished and after the user has inserted numbers of animals. But this prints out right after the first loop. e.g. 5000, panthers.
public static void print(String[]animals, String answer)
{
System.out.println(answer + "," + animals[0]);
System.out.println(answer + "," + animals[1]);
System.out.println(answer + "," + animals[2]);
System.out.println(answer + "," + animals[3]);
System.out.println(answer + "," + animals[4]);
}
Aucun commentaire:
Enregistrer un commentaire