(Java) Statistical occurrences of randomly generated letters

Source: Internet
Author: User

The results of the output are referenced as follows:

List of programs:

1. Produce random number

Generate random number public
class Randomcharacter {public
	static char getrandomcharacter (char Ch1,char CH2) {return
		( Char) (Ch1+math.random () * (ch2-ch1+1));
	}
	Generates random lowercase letters public
	static char Getrandomlowercaseletter () {return
		getrandomcharacter (' A ', ' Z ');
	}
	Generates random uppercase letters public
	static char Getrandomuppercaseletter () {return
		getrandomcharacter (' A ', ' Z ');
	}
	Generates 0-9 random digits between
	the public static char Getrandomdigitcharacter () {return
		getrandomcharacter (' 0 ', ' 9 ');
	}
	public static char Getrandomcharacter () {return
		getrandomcharacter (' \u0000 ', ' \uffff ');
	}


2, randomly generated 100 lowercase letters, statistics 26 letters each occurrence of the number of

Randomly generates 100 lowercase letters, counts the number of occurrences of 26 letters public class Countlettersinarray {public static void main (string[] args) {char[] Ch
		Ars=createarray ();
		System.out.println ("The lowercase letters are:");
		Displayarray (chars);
		Int[] Counts=countletters (chars);
		System.out.println ();
		System.out.println ("The occurences of each letter are:");
		
	Displaycounts (counts);
		public static char[] Createarray () {//Generate 100 random lowercase letters char[] chars=new char[100];
		for (int i=0;i<chars.length;i++) chars[i]=randomcharacter.getrandomlowercaseletter ();
	return chars;
				public static void Displayarray (char[] chars) {//per line 20 display letter for (int i=0;i<chars.length;i++) {if ((i+1)%20==0)
			System.out.println (chars[i]+ "");
		else System.out.print (chars[i]+ "");
		} public static int[] Countletters (char[] chars) {//Returns the number of occurrences per letter int[] Count=new int[26];
		for (int i=0;i<chars.length;i++) count[chars[i]-' a ']++;
	return count; public static void Displaycounts (int[] count) {//displays 26 letters and their corresponding occurrences.10 for (int i=0;i<count.length;i++) if ((i+1)%10==0) System.out.println (count[i]+ "-" + (char) (i+ ' a ') + "") per line;
	else System.out.print (count[i]+ "-" + (char) (i+ ' a ') + "");
 }
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.