Java daily exercise (i)

Source: Internet
Author: User

have been self-taught Java has no method, in addition to work in a small amount of contact, after reading the grammar, do not know how to progress, helpless ah, had to stick to brush exercises, hoping to accumulate and understand.


Title: Generate 100 random integers from 0 to 99, find the largest and smallest of them, and count the number of integers greater than 50, sort the array, and go back


Import Java.util.HashSet;
Import Java.util.Set;

public class Randomnum {

public static void Main (string[] args) {
int randomnum[] = new INT[100];

int gt50record = 0;
int maxnum = 0;
int minnum = 100;

System.out.print ("Original array Value:");
for (int i = 0; i < randomnum.length; i++) {

100 random integers between 0-99
Randomnum[i] = (int) math.round (* math.random ());
System.out.print (Randomnum[i] + "");

Number of integers greater than 50
if (Randomnum[i] > 50) {
gt50record++;
}

New Max Max, read the new value compared to the original value, if it is greater than the original value, replace the original value with the new value
if (Randomnum[i] > Maxnum) {
Maxnum = Randomnum[i];
}
if (Randomnum[i] < Minnum) {
Minnum = Randomnum[i];
}

The values in the array are evaluated one by one, and the new value is compared to each value in the new group, and the elements in the new array continue to be compared to the next element, if less than the position of the element is swapped.
for (int j = 0; J < i; J + +) {
if (Randomnum[i] < randomnum[j]) {
int tmp;
TMP = Randomnum[j];
RANDOMNUM[J] = Randomnum[i];
Randomnum[i] = tmp;

}
}

}
System.out.println ("\ n");
SYSTEM.OUT.PRINTLN ("GT Intnum has" + Gt50record);
System.out.println ("max number is:" + maxnum);
System.out.println ("min number is:" + minnum);
System.out.print ("Under Is Order Result:");
for (int i = 0; i < randomnum.length; i++) {
System.out.print (Randomnum[i] + "");
}


Go heavy
set<integer> uniq = new hashset<integer> ();
for (Integer i:randomnum) {
Uniq.add (i);
}

System.out.println ("\ n" + "Uniq:");
for (Integer I:uniq) {
System.out.print (i + "");
}

}

}

Java daily exercise (i)

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.