Basic programming exercises for NLP, random, and arraylist

Source: Internet
Author: User
Generate 6 1 ~ Random integer between 33, add to the set, and traverse
Package arraylist; import java. util. arraylist; import java. util. Random;/** train of thought * 6 ---> 6. fori * 1 ~ Between 33 ---> XXX. nextint (33) + 1; * random integer ---> random xxx = new random (); * Add to set ---> arraylist <integer> xxx = new arraylist <> (); list. add (); * traverse ---> XXX. fori; ---> list. get (INDEX); ***/public class arraylist_4 {public static void main (string [] ARGs) {random r = new random (); arraylist <integer> List = new arraylist <> (); For (INT I = 0; I <6; I ++) {int num = R. nextint (33) + 1; List. add (Num);} system. out. println (list); For (INT I = 0; I <list. size (); I ++) {system. out. print (list. get (I) + "");}}}
Customize four student objects, add them to the set, and traverse
Package arraylist; import class_objiect.student; import Java. util. arraylist;/** train of thought * Custom four student objects ---> Student one = new student (); * student two = new student (); * student three = new student (); * student four = new student (); * Add to set ---> arraylist <student> List = new arraylist <> (); & list. add (content); * traverse ---> list. fori ---> list. get (INDEX); **/public class arraylist_5 {public static void main (string [] ARGs) {arraylist <student> List = new arraylist <student> (); student one = new student ("Andy", 18); student two = new student ("bill", 19); student three = new student ("Lily", 32 ); student four = new student ("Jane", 12); list. add (one); list. add (two); list. add (three); list. add (four); For (INT I = 0; I <list. size (); I ++) {system. out. println ("name:" + list. get (I ). getname () + "" + "Age:" + list. get (I ). getage () + "years old ");}}}
Define a method to print a set in a specified format (arraylist type as a parameter), use {} to expand the set, and use @ to separate each element. For the format, see {element @ element }.
Package arraylist; import Java. util. arraylist;/** train of thought * arraylist <data type> xxx = new arraylistanbul <> (); **/public class arraylist_6 {public static void main (string [] ARGs) {// create a collection arraylist <integer> List = new arraylist <> (); // Add an element list. add (1); list. add (3); list. add (5); list. add (7); // call method print (list);} public static void print (arraylist <integer> List) {system. out. print ("{"); // print {for (INT I = 0; I <list. size (); I ++) {if (I = List. size ()-1) {system. out. print (list. get (I); // print the number} else {system. out. print (list. get (I) + "@"); // print "number @"} system. out. println ("}"); // print }}}
Definition 1 ~ Between 30 (including 1 and 30), all methods of the even Element Set (arraylist type as return value)
Package arraylist; import Java. util. arraylist; import Java. util. random;/** train of thought * between 1-30 ---> XXX. nextint (30) + 1; * All even elements ---> if (I % 2 = 0) {even number} * method public static string method (arraylist <string> List) **/public class arraylist_7 {public static void main (string [] ARGs) {arraylist <integer> List = new arraylist <> (); For (INT I = 1; I <= 30; I ++) {if (I % 2 = 0) {list. add (I) ;}} system. out. println (list );}}
At the beginning of the game, an integer number ranging from 1 to is randomly generated. The player guesses a number guessnumber, which will compare with the number. The system prompts that the number is large or small until the player guesses it and the game ends.
Package random;/* random thought ---> random r = new random (); 1 ~ 100 --> int number = R. nextint (100) + 1; conjecture ---> expect SC = new expect (system. in); ---> int guessnumber = SC. nextint (); If (guessnumber> Number) {large} If (guessnumber <number) {small} If (guessnumber = number) {guessed it} guessnumber should be a loop; **/import Java. util. random; import Java. util. random; public class random_4 {public static void main (string [] ARGs) {random r = new random (); random SC = new random (system. in); int number = R. nextint (100) + 1; system. out. println ("Enter:"); For (INT I = 0; I <100; I ++) {int guessnumber = SC. nextint (); If (guessnumber> Number) {system. out. println ("big");} else if (guessnumber <number) {system. out. println ("");} else if (guessnumber = Number) {system. out. println ("guessed ");}}}}

Basic programming exercises for NLP, random, and arraylist

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.