Results 1 to 6 of 6
  1. #1
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default the count on my program is not working as i would like it to

    The program is meant to take in user entered numbers (until the user enters 0) and evaluate which is the highest of the numbers that were entered and show the occurrence of the no.

    eg:
    Enter numbers: 1 2 3 7 7 6 5 5 0
    The largest number is 7
    Its occurrence count is 2

    I cant figure out where to put my occurrence count and get the accurate result for it.

    Java Code:
    import java.util.*;
    
    public class Max
    {
      public static void main(String[] args)
      {
        Scanner input = new Scanner(System.in);
    
        System.out.print("Enter numbers: ");
          int a = input.nextInt();
          int count = 0; 
       
          int large = -1;    
      
        do
         {    
           a = input.nextInt(); 
           
           
           if ( large < a )
            {
              large = a;
                      
            }  
           
           if( a == large )
            {
              count++;
            }
          
          
     
         } while ( a != 0);
    
         
        System.out.println("The largest number is: " + large);
        System.out.print("The occurance count of the largest number is: " + count);
        
    
      }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,945
    Rep Power
    4

    Default Re: the count on my program is not working as i would like it to

    You're almost there. You need to reset count each time you assign "a"to "large."

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: the count on my program is not working as i would like it to

    Try using math.max(int1,int2) if the max doesn't work for you. Its just easier, you could make an array, and have it holt the numbers and be one of the ints. As for occurrence, the result could be an int, and have a variable set to 0 (lets call it stool), and then after all the inputs are entered, have it like read them again or something, and idk if this works, you could try if(input.nextInt==whatever variable you set the result of math.max as) stool++; then return stool before end of program, and have the occurrences be "..."+stool. Idk, im pretty new at java, idk if this will work.

  4. #4
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: the count on my program is not working as i would like it to

    I realized that after i posted this. Thanks.

  5. #5
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: the count on my program is not working as i would like it to

    I am a beginner, haven't learned about arrays yet. I got it to work though. Thanks.

  6. #6
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: the count on my program is not working as i would like it to

    Yeah I just recently learned about them too. Only been learning for 6 months now.

Similar Threads

  1. count every 45 records and prfix the count number
    By dkr786 in forum New To Java
    Replies: 8
    Last Post: 02-15-2013, 07:31 PM
  2. Replies: 6
    Last Post: 04-25-2012, 09:17 PM
  3. program not working
    By Dax Le Foundry in forum New To Java
    Replies: 7
    Last Post: 08-09-2011, 08:10 PM
  4. Replies: 2
    Last Post: 05-27-2011, 09:55 PM
  5. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •