Results 1 to 3 of 3
  1. #1
    bobbyj7 is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Write a Java program that reads integers finds the largest and counts its occurrence.

    This program runs great, but i need to input a q at the end of the sequence. How do I set this program up without the requirment to enter a q to get the desired result? Please Help.

    import java.util.Scanner;



    public class vend

    {

    public static void main (String[] args)

    {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter numbers(q to end input): ");

    //initialize num1 and max to be the first int read

    //and the count is 1 at that point.

    int num1, max = input.nextInt();

    int count = 1;



    while(input.hasNextInt()){

    num1 = input.nextInt();

    if (num1 > max){

    //found a new max. set it to num1 and reset the counter

    max = num1;

    count = 1;

    }

    else if (num1 == max){

    count++;

    }

    }



    // Output largest.

    System.out.println("The largest number is " + max);

    System.out.println("the occurrence count of the largest number is " + count);

    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Write a Java program that reads integers finds the largest and counts its occurre

    Please go through BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    saustin6354 is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default Re: Write a Java program that reads integers finds the largest and counts its occurre

    so where did you add the q i cant find where to add it

Similar Threads

  1. Replies: 5
    Last Post: 03-30-2012, 09:53 PM
  2. Replies: 0
    Last Post: 12-02-2011, 10:03 AM
  3. Replies: 13
    Last Post: 10-31-2010, 09:42 PM
  4. Write a Program that reads two times in military format
    By HPcompaq256 in forum New To Java
    Replies: 10
    Last Post: 02-26-2010, 05:38 AM
  5. How to write integers into file?
    By dj kourampies in forum New To Java
    Replies: 1
    Last Post: 08-20-2009, 05:52 AM

Posting Permissions

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