Zoklet.net

Go Back   Zoklet.net > Technology > Technophiles and Technophiliacs > Codes of all kinds

Reply
 
Thread Tools
  #1  
Old 04-08-2010, 03:18 AM
duuude duuude is offline
New Arrival
 
Join Date: Jan 2009
Thanks: 27
Thanked 12 Times in 11 Posts
Default Need some help with a java code

Well I am trying to learn how to do arrays and I tried to write this program for practice but can't quite get one thing.. My program takes hourly temperatures input from a user and outputs them, outputs their average, and outputs the highest temperature.. what I would like to do is make a for that prints all of the temperatures that are above the average but I can't quite get it.

Anyway here's the code..
Code:
import java.util.*;

public class TemperatureArray
{
	static Scanner console = new Scanner(System.in);

	public static void main(String[] args)
	{
		int[] temp = new int[8];

		int avg = 0;
		int index;
		int sum = 0;
		int hour = 1;
		int maxIndex = 0;


		System.out.println("Enter 8 hourly temperatures: ");

		for (index = 0; index < temp.length; index++)
		{
			temp[index] = console.nextInt();
			System.out.println();
		}

		System.out.println("Temperature Report");

		for (index = 0; index < temp.length; index++)
		{
			System.out.println("Temperature for Hour " + hour++ + ": " + temp[index] + " ");
		}

		for (index = 0; index < temp.length; index++) 
		{
			sum = sum + temp[index];
		}

		if (temp.length != 0)
			avg = sum / temp.length;
		else
			avg = 0;
			System.out.println();
			System.out.println("Average Temperature: " + avg); 

		for (index = 1; index < temp.length; index++)
		if (temp[maxIndex] < temp[index])
			maxIndex = index;
		else
			System.out.println();
			System.out.println("Highest Temperature: " + temp[maxIndex]);
	}
}
Reply With Quote
  #2  
Old 04-08-2010, 03:50 AM
Pooka Pooka is offline
Baron
 
Join Date: Mar 2009
Location: ITT
Thanks: 4
Thanked 117 Times in 94 Posts
Default Re: Need some help with a java code

Code:
import java.util.*;

public class TemperatureArray
{
	static Scanner console = new Scanner(System.in);
        static int sum = 0;
        static int avg = 0;
	public static void main(String[] args)
	{
		int[] temp = new int[8];
		System.out.println("Enter 8 hourly temperatures: ");

		for (int index = 0; index < temp.length; index++)
		{
			temp[index] = console.nextInt();
			System.out.println();
		}

		System.out.println("Temperature Report");
                printArray(temp);

		sum = sum(temp);
                avg = average(temp);

		aboveAverage(temp);
	}
        public static void printArray(int[] print)
        {
            System.out.println("Temperature Report");

            for (int index = 0; index < print.length; index++)
        	{
                        int hour = index+1;
			System.out.println("Temperature for Hour " + hour + ": " + print[index] + " ");
		}
        }
        public static int sum(int[] temps)
        {
            int sums = 0;
            for (int index = 0; index < temps.length; index++)
		{
			sums += temps[index];
		}
            return sums;
        }
        public static int average(int[] temps)
        {
            return sum(temps)/temps.length;
        }
        public static void aboveAverage(int[] temps)
        {
            for(int temp:temps)
            {
                if(temp > avg)
                {
                    System.out.println("Above average temps " + temp);
                }
            }
        }
}
Reply With Quote
The following users say "It is so good to hear it!":
duuude (04-08-2010)
Reply

Bookmarks

Tags
code, java

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this java? TwinkleTits Codes of all kinds 3 02-28-2010 03:53 PM
(java) wampserver problem BillGatesJR Codes of all kinds 12 02-03-2010 02:27 PM
best java game ever dij314 Fragyard 1 06-25-2009 04:26 AM
Java Googles Upsizing Trueborn Codes of all kinds 4 04-12-2009 04:35 AM
Java Help Please Anyone? Belle Codes of all kinds 2 03-09-2009 12:26 PM


All times are GMT. The time now is 03:13 AM.


Hot Topics
On IRC
Users: 4
Messages/minute: 0
Topic: "http://www.zoklet.net/..."
Users: 18
Messages/minute: 0
Topic: "dangly parts"
Users: 10
Messages/minute: 0
Topic: "vaginaboob"
Advertisements
Your ad could go right HERE! Contact us!

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.