Need help with a simple Java code

Discussion in 'Programming General' started by amazingBRIAN, May 6, 2013.

Need help with a simple Java code
  1. Unread #1 - May 6, 2013 at 7:10 PM
  2. amazingBRIAN
    Joined:
    Mar 11, 2012
    Posts:
    426
    Referrals:
    0
    Sythe Gold:
    0
    Two Factor Authentication User

    amazingBRIAN Forum Addict

    Need help with a simple Java code

    Okay so i really suck at programming and don't why i took the class. We started using for loops and now i'm really stuck.

    So i need to create a program that asks the user for temperatures for 5 different day's and then averages out the 5 temperatures. (Using a simple FOR loop)

    For example:

    Day 1:80
    Day 2:68
    Day 3:76
    Day 4:77
    Day 5:66

    Average temperature:73.4

    If anyone can help me create a program like this using a simple for loop that would be great, i would pay for this but it seems like a really easy code to do. :/ I COULD pay but it wouldn't be much
     
  3. Unread #2 - May 8, 2013 at 3:50 AM
  4. yoloswag
    Joined:
    Feb 25, 2013
    Posts:
    9
    Referrals:
    0
    Sythe Gold:
    0

    yoloswag Newcomer

    Need help with a simple Java code

    You can pay me if you want :)

    Code:
    import java.util.Scanner;
    public class test
    { 
    	static Scanner input = new Scanner (System.in);
    	public static void main (String[] args)
    	{
    		double i;
    		double sum = 0;
    		int count = 0;
    		for(int y = 0; y < 5; ++y)
    		{
    			System.out.println("input");
    			i = input.nextDouble();
    			sum = sum + i;
    			++count;
    		}
    	System.out.println("Average Temperature is " + sum / (count));
    	}
    }
    
     
  5. Unread #3 - May 20, 2013 at 1:47 AM
  6. thcncr
    Joined:
    May 16, 2013
    Posts:
    51
    Referrals:
    0
    Sythe Gold:
    0

    thcncr Member

    Need help with a simple Java code

    You'll never get very far in programming if you can't do something as simple as a for loop and an average calculation. My advise would be to next time re-read the chapter and ask your instructor for help before asking someone else to do it entirely. I struggled with Java my first year in college, but if you ask the people designated to help you, you will go much further, get a better grade, and feel more fulfilled in your work.
     
  7. Unread #4 - May 21, 2013 at 2:46 AM
  8. MohtasaUnique
    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235

    MohtasaUnique Grand Master
    Retired Global Moderator

    Need help with a simple Java code

    He didn't post for your lectures, he posted for help. It's not your business if he wants to take the lazy way out.


    Code:
    import java.util.*;
    
    public class TemperatureAverage
    {
    	public static void main(String[] args)
    	{
    		System.out.println("Enter a temperature for 5 days.");
    		double[] temps = new double[5];
    		double totalTemp = 0;
    		double average = 0;
    		Scanner scan = new Scanner(System.in);
    		for(int i = 0; i < 5; i++)
    		{
    			System.out.print("Day " + (i+1) + ": ");
    			temps[i] = scan.nextDouble();
    		}
    		for(int i = 0; i < 5; i++)
    		{
    			totalTemp += temps[i];
    		}
    		average = totalTemp/5;
    		System.out.println("The average of the temperatures is " + average);
    	}
    }







    Here's the code with comments to explain each step if it interests you.
    Code:
    // Importing java utilites are necessary when using objects such as Scanner or Random.
    import java.util.*;
    
    // All programs require a class. Conventionally, public class NameHere{} is used.
    public class TemperatureAverage
    {
    	// This is the main method, and will usually look the same as written below.
    	public static void main(String[] args)
    	{
    		// Printing is as you see below. If you wish to print a message, enclose it in quotations.
    		System.out.println("Enter a temperature for 5 days.");
    		// You must initating a few variables BEFORE you have to use them. It's easiest to do all of the method variables in one block so you can refer to them quickly
    		double[] temps = new double[5]; // Double will be used so you can have decimal places. This is conventional, since you want to find the average-- which is often a decimal number
    		double totalTemp = 0; // It's easiest to set all variables to the double if your result is going to be double. This way, you don't have to cast integers as doubles later on.
    		double average = 0; // Setting your variables to 0 when you decalre them is conventional. This is called initating the variables, and it will be updated later as you perform your calculations
    		// This line of code below will create a Scanner object. That's it. The object you created, called "scan" here, won't do anything yet until you tell it to.
    		Scanner scan = new Scanner(System.in);
    		// So begins the first for loop.
    		for(int i = 0; i < 5; i++) // Notice the format within the parameters. You're creating a new integer 'i' and setting it to 0. SEMICOLON! Then you're saying you want this loop to iterate 5 times before exiting. SEMICOLON! That's accomplished with the third bit of code, i++. This will add 1 to i at the END of each loop, after the body is executed.
    		{
    			// Here's the body. This will execute 5 times before i is no longer less than 5, which means the loop will exit when i = 5.
    			System.out.print("Day " + (i+1) + ": "); // NOTICE, I can have variables inside print statements, and they are not enclosed with quotations. ALSO NOTICE that you MUST separate quoted text from variables (and also variables from other variables, if you're printing two) with a + sign. This is cruitial. It's called "concatination", and without it, java won't know when a string ends and the variable begins.
    			// Also notice the use of 'i'. In the for loop, you create the variable 'i', and set it equal to 0. Therefore, in the above statement (0+1) will be printed since that's the value of i.
    			// Below, you see us opening the array, using 'i' as the index number. Since 'i' is set to 0, temps[i] is actually temps[0]. After the loop iterates once, 'i' will be equal 1 because of the i++ in the parameters. And so on.
    			temps[i] = scan.nextDouble();
    			// Therefore, the iterations will look like this:
    			// temps[i] = temps[0] = your input from the scanner object
    			// temps[i] = temps[1] = your next input from the scanner object
    			// ...
    			// temps[i] = temps[5] = your next input from the scanner object
    			// and the for loop will then exit because i = 5, and is not less than 5.
    		}
    		// Here's another for loop that we will use to open the temps[] array, and add each value, one at a time, to totalTemp.
    		for(int i = 0; i < 5; i++)
    		{
    			totalTemp += temps[i]; // NOTE the operator += is the same as totalTemp = temps[i] + totalTemp. So totalTemp is 0 initially, so this would look like 0 = temps[i]+0.
    			// The iterations will look like this:
    			// totalTemp += temps[i] = temps[0] = whatever you put in previously
    			// totalTemp (updated) += temps[i] = temps[1] = whatever you put in previously
    			// ...
    			// totalTemp (updated with temps[0 through 4] += temps[i] = temps[5] = whatever you put in previously
    			// And the loop ends because i = 5.
    		}
    		// Update the value of "average" with the totalTemp (the added value of all the temps you put in) divided by 5.
    		average = totalTemp/5;
    		// Print the average temperature.
    		System.out.println("The average of the temperatures is " + average);
    	}
    }
     
< List of most commonly used Nouns in English vocab | c# homework >

Users viewing this thread
1 guest


 
 
Adblock breaks this site