Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: anyone know how to fix this ?

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post anyone know how to fix this ?

    hello guys , can anyone tell me what should i do to change the output number from 193 to 193.5 for the average number . the last number always keep missing when i get the output . should i add any other code ? thanx for your cooperation . i also attach the code and output for from what i have done .

    p/s : sorry for my bad english .

    images removed by moderator because they linked to a bad site -KevinWorkman
    Last edited by KevinWorkman; June 21st, 2011 at 10:56 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: anyone know how to fix this ?

    The images you originally supplied link to a site that contains pornographic advertisements. That's not great, as some of us are at work, and some of us have wives and stuff.

    If you want to post code, post the text, not an image. Use the code tags and post an SSCCE. Don't post screenshots of your code. Same goes with error messages or stack traces.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: anyone know how to fix this ?

    owh sorry . i didnt know that . newbie here . okay then , i will post the code . thanks for your advice .

    so this is the coding for what i have done . still cant get what i want .
    import java.util.*;
    class  lab1{
            public static void main(String[] args){
            int sum=0,count=0;
            Scanner input=new Scanner(System.in);
            System.out.println("Enter a Number: ");
            int array[]=new int[10];
            for(int i=0;i<array.length;i++){
                int num=input.nextInt();
                array[i]=num;
            }
            int maxValue = array[0];
            for(int i=1;i < array.length;i++){
            if(array[i] > maxValue){
            maxValue = array[i];
            }
            }
            for(int i=0;i<array.length;i++){
                sum+=array[i];
                if(array[i]==maxValue){
                    count++;
                }
            }
            System.out.println("Total Of Numbers is: "+sum);
            System.out.println("Average of Numbers is: "+sum/array.length);
            System.out.println("Largest No: "+maxValue+" which occurs "+count+" times.");
            Arrays.sort(array);
            System.out.println("Numbers in ascending order: ");
            for(int i=0;i<array.length;i++){
              System.out.println(array[i]);
            }
            System.out.println("Numbers in descending order: ");
            for(int i=array.length-1;i>=0;i--){
              System.out.println(array[i]);
            }
        }
    }
    Last edited by KevinWorkman; June 21st, 2011 at 11:07 AM. Reason: added highlight tags

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: anyone know how to fix this ?

    When posting code, make sure you use the highlight tags. I've added them for you this time.

    But your whole problem is that you're using ints, which are only whole numbers (0, 1, 2, not 1.5 or 2.3). That's fine for things that are only whole numbers (counts of things, indexes, etc), but if you want to have decimals (for things like averages), you have to use double or float.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: anyone know how to fix this ?

    okay , next time i will use the highlight tags , thanx once again for your advice . im really humble here . -__-" let me try using double or float , hope it works .

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: anyone know how to fix this ?

    Note that you do not have to convert all your variables to double if it doesn't make sense. All you need to do is cast your sum to a double when doing the calculation.
     System.out.println("Average of Numbers is: " + (double)sum/array.length);

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: anyone know how to fix this ?

    hey junky , it works man . thank u so much .

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: anyone know how to fix this ?

    If you click 'Go Advanced' you can attach images directly to your posts.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.