My last step involves getting the integer percent of the sum.
So if I enter:
2
1
1
2
The output should be:
2, which is 33.333% of the sum.
1, which is 16.666% of the sum.
1, which is 16.666% of the sum.
2, which is 33.333% of the sum.
Since I am very new to arrays, I am extremely puzzled. I don’t understand how to get the percent since user can enter any amount of integers. If it was only 2 integers, just say 2 and 2, they each would be 50 percent
import java.util.Scanner;
public class Integers {
/* program 7-1*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many numbers will you enter?");
int size = keyboard.nextInt();
int[] entry = new int[size];
System.out.println("Enter " + entry.length + " integers, one per line:");
int sum = 0;
for (int index = 0; index < entry.length; index++)
{
entry[index] = keyboard.nextInt();
sum += size;
}
System.out.println("The sum is " + sum + "." + "\nThe numbers are:" );
}
}
Now that you have all entries and the sum, you are almost there:
100.0, and then divide by thesum. Note the dot zero at the end of100.0– it’s there on purposeprintf. Note, however, that the percent sign%needs to be escaped.