forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHowManyPets.java
More file actions
47 lines (36 loc) · 1.03 KB
/
HowManyPets.java
File metadata and controls
47 lines (36 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Scanner;
public class HowManyPets
{
public static void main(String[] args)
{
numberOfPets();
System.out.println();
}
private static void numberOfPets()
{
Scanner in = new Scanner(System.in);
System.out.println("How many pets?");
int numberOfPets = in.nextInt();
in.nextLine();
int[] numPets = new int[numberOfPets];
//System.out.println("Length is: " + numPets.length);
int currentPet = 0;
String[] petNames = new String[numPets.length];
while (currentPet < numPets.length)
{
System.out.println("Enter name of pet #" + (currentPet + 1));
petNames[currentPet] = in.nextLine();
currentPet++;
}
getNames(petNames);
}
private static void getNames(String[] names)
{
int index = 0;
while(index < names.length)
{
System.out.println("Animal #" + (index + 1) + ": " + names[index]);
index++;
}
}
}