[tut] Calculating average (not GUI)

Discussion in 'Programming General' started by Blupig, Oct 12, 2008.

[tut] Calculating average (not GUI)
  1. Unread #1 - Oct 12, 2008 at 5:04 PM
  2. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [tut] Calculating average (not GUI)

    Code:
    public class Average {
        
        public static void main(String[] args) {
        	
        	int first = 5;
        	int second = 7;
        	int third = 3;
        	
        	double avg = (first + second + third)/3;
        	
        		System.out.print(avg);
        	
    
        }
    }
    
    The first 3 lines of code for main are declaring 3 integers, which equal different numbers. These will be the 3 numbers used in the average calculation. Then, the 4th line is the double (decimal number) that the average will be, so in this case it's all the integers (ints) added together and divided by 3 (the number of ints). Then, they are printed out.
     
  3. Unread #2 - Oct 12, 2008 at 6:54 PM
  4. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    [tut] Calculating average (not GUI)

    What if I want to take an average of non ints! Give it user input when its running... It would be better that way. :D
     
  5. Unread #3 - Oct 12, 2008 at 8:49 PM
  6. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [tut] Calculating average (not GUI)

    I'm still learning Java myself and haven't learned how to do that yet :(
     
  7. Unread #4 - Oct 12, 2008 at 9:26 PM
  8. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    [tut] Calculating average (not GUI)

    Code:
    
    import java.util.*;
    
    
    public class Average 
    {
    
    	static Scanner s = new Scanner(System.in);
    
    	public static void main(String[] args) 
    	{
    
    		System.out.print("Enter 1rst int:");
    		int first = s.nextInt();
    		System.out.print("Enter 2nd int:");
    		int second = s.nextInt();
    		System.out.print("Enter 3rd int");
    		int third = s.nextInt();
    
    		double avg = (first + second + third) / 3;
    
    		System.out.print(avg);
    
    	}
    }
    There ya go. You need to use a scanner to do it.

    s.nextInt(); looks for an int input, and if its not an int it throws an error.

    Here is a method that stops the error. D:

    Code:
    public static int getInput(String output) 
    	{
    		while (true) 
    		{
    			try 
    			{
    			System.out.print(output);
    			return s.nextInt();
    			} 
    			catch (InputMismatchException e) 
    			{
    				s.next();
    			}
    		}
    
    	}
    the parameter is what text you want displayed like "Enter int A:" It basicly looks for the error which is if you put the wrong input in, if it finds it, the program exacutes the catch statement which takes the next input what ever it is, then the while loops jumps back to the start and begins again until it can get out threw the return statement. To use my method, do this


    Code:
    int first = getInput("Enter A:");

    Hope you learned something. :D
     
  9. Unread #5 - Oct 13, 2008 at 1:18 PM
  10. SingSing
    Joined:
    Oct 3, 2008
    Posts:
    112
    Referrals:
    0
    Sythe Gold:
    0

    SingSing Active Member

    [tut] Calculating average (not GUI)

    SuF, I ran your code and it didn't work.
    Do I have to import something or what?
    It said: Cannot find symbol Scanner.
    Thanks.


    EDIT: Haha, of course, just added import java.util.*; and it worked.. my bad.. , your bad too! Hehe.
     
  11. Unread #6 - Oct 13, 2008 at 2:15 PM
  12. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    [tut] Calculating average (not GUI)

    fuck, forgot to put the import... thought i had... o_O
     
  13. Unread #7 - Oct 21, 2008 at 5:26 PM
  14. GreatKhan
    Referrals:
    0

    GreatKhan Guest

    [tut] Calculating average (not GUI)

    Code:
    public class Average {
        
        public static void main(String[] args) {
    	
    	static Scanner s = new Scanner(System.in);
    	
    	int ctr=0;
    	double avg=0;
    	for (int ctr=0; ; ctr++)
        	{
    		System.out.print("Enter int #" + ctr + ":");
    
    		try {avg+= s.nextInt();}
    		catch (InputMismatchException e) 
    		{break;}
    	}
        	
    	System.out.print(avg/ctr);
        	
    
        }
    }
    would this work? havent done java in forever :( (downloading eclipse right now ^^ only 10 hours left)
     
< Hiring a programmer( Details Inside) | open new form, load a .html file? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site