My head Hurts... such a simple problem but I'm stuck!
I'm supposed to do this program as an assignment and I think I'm completely stuck... Maybe it's because I have so much due for other classes and I'm looking at the problem in a different way ><.
This is what we have to do :
Create a program that asks the user to read in 10 integers and prints the maximum, the total and the average of the list of numbers to the screen.
For example, if the user entered:
10 12 16 13 4 5 21 2 6 1
the maximum entered is 21, then total is 90 and the average (total divided by number of items in list) is 9.
This is what I have so far
Code :
import java.util.Scanner;
public class LetterCounter {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final int NUMCHARS = 10;
Scanner scan = new Scanner(System.in);
int[] numbers = new int[scan.nextInt()];
System.out.print("Enter random Numbers:");
scan.nextInt();
for (int count= 0; count < NUMCHARS; count++)
{
System.out.print("Enter random Numbers:");
scan.nextInt();
}
//results
System.out.println("Total: " ) ;
System.out.println("Average: " );
System.out.println("Highest Entered: ");
}
}
Any help will be appreciated.
Thanks.
Re: My head Hurts... such a simple problem but I'm stuck!
Okay I was able to do the Total and Average.
Now how will I be able to find the Highest number entered in the array list?
This is my current code.
Code :
import java.util.Scanner;
public class LetterCounter {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// Declare Variables
Scanner scan = new Scanner(System.in); // scanner
int[] numbers = new int[10]; //array for
//
for (int count= 0; count < 10; count++)
{
System.out.print("Enter random Numbers:");
numbers[count]= scan.nextInt();
}
int total;
int average;
int highest;
total = numbers[0]+numbers[1]+numbers[2]+numbers[3]+ numbers[4]+numbers[5]+numbers[6]+numbers[7]+numbers[8]+numbers[9];
average = total / numbers.length;
//results
System.out.println("Total: "+ total ) ;
System.out.println("Average: "+average );
System.out.println("Highest Entered: ");
}
}
Re: My head Hurts... such a simple problem but I'm stuck!
Use a variable to store the highest value. Set it to 0. For every number that comes in, compare it to the highest value variable and if it's bigger, set the highest value to the new value.
Re: My head Hurts... such a simple problem but I'm stuck!
Thank you dlorde
After 2~3 minutes I was able to come up with the solution :P. Don't know why I was stuck with such a simple problem, really. xD
Final code
Code :
import java.util.Scanner;
public class LetterCounter {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// Declare Variables
Scanner scan = new Scanner(System.in); // scanner
int[] numbers = new int[10]; //array for numbers
int currenthighest = 0; // integer for highest value stored in the array
for (int count= 0; count < 10; count++)
{
System.out.print("Enter random Numbers:");
numbers[count]= scan.nextInt();
if (numbers[count]> currenthighest) //checks for highest number input
{
currenthighest= numbers[count];
}
}
int total;
int average;
total = numbers[0]+numbers[1]+numbers[2]+numbers[3]+ numbers[4]+numbers[5]+numbers[6]+numbers[7]+numbers[8]+numbers[9];
average = total / numbers.length;
//results
System.out.println("Total: "+ total ) ;
System.out.println("Average: "+average );
System.out.println("Highest Entered: " +currenthighest);
}
}
Re: My head Hurts... such a simple problem but I'm stuck!
Congratulations on solving your issue. If you are complete, please mark this thread as solved :)
http://www.javaprogrammingforums.com...ad-solved.html