JAVA: Occurrence Of Max Numbers
Tarih: 28 Kasım 2013 Yorum: 0

(Occurrence of max numbers) Write a program that reads integers, finds the largest of them, and counts its occurrences.  Assume that the input ends with number 0. Suppose that you entered 3 5 2 5 5 5 0; the program finds that the largest is 5 and the occurrence count for 5 is 4.

(Hint: maintain two variables, max and count. max stores the current max number, and count stores its occurrences.  Initially, assign the first number to max and 1 to count.  Compare each subsequent number with max.  If the number is greater than max, assign it to max and reset count to 1.  If the number is equal to max, increment count by 1.)

Sample run:
Enter numbers: 3 5 2 5 5 5 0
The largest number is 5.
The occurrence count of the largest number is 4.

 

MaxNumber.java

/**
 * Occurrence of max numbers.
 */

	import javax.swing.JOptionPane; //	import JOptionPane;
public class MaxNumber {

    public static void main(String[] args) {

    	int num,max,count;
			String Strinput = "";
			num = Integer.MIN_VALUE;
    		max = num;
    		count = 1;

		    	while(num!=0){

					Strinput	= JOptionPane.showInputDialog("Please enter number \nand enter 0 for input end.");	
					num 		= Integer.parseInt(Strinput);

		    		if(num!=0){

			    		if(num>max){

			    			max = num;
			    			count = 1;

			    		}else if(num==max){

			    			count++;

			    		}

		    		} // End of if num!=0
		    	} // End of while.

			if(max==Integer.MIN_VALUE){

				// if the first input is 0.
				System.out.print("The largest number is 0.\nThe occurrence count of the largest number is 1.");

			}else{

    			System.out.print("The largest number is "+max+".\nThe occurrence count of the largest number is "+count+".");

			}

    } // End of main method.
} // End of class.



Yorum Yok:


Yorum Yap:

Yorum yapabilmek için giriş yapmalısınız.




tema yapımcısı wordpress alexa bilgileri Webmaster Creative Commons v3 ile Lisanslanmıştır!


Akif ARSLAN 2012 - 2014 © Hiçbir Hakkı Saklı Değildir.
Sitede bulunan istediğiniz cümleyi veya içeriği, istediğiniz gibi, istediğiniz yerde, istediğiniz zaman ve istediğiniz kişilerle paylaşabilirsiniz.