In the development of some tables to be numbered, and the number is unique, their own time generated under, I think there may be concurrency situation. So on-line check, is randomly generated. Here's how:Method one (use the current time accurate to milliseconds, intercept any few ) Date date = new date ();SimpleDateFormat SDF = new SimpleDateFormat ("yyyymmddhhmmssss"); String formdate =sdf.format (date);System. out . println (formdate);String no = formdate.substring (10);System. out . prin
Java to obtain the available UDP port number. The TCP method is similar to this one.
Method 1: If you do not mind obtaining the port number range, you can use the assumramsocket constructor to define 0 as its port number. The system will assign it an idle Port Number:
public static DatagramSocket getRandomPort() throws SocketException {DatagramSocket s = new DatagramSocket(0);return s;}
Method 2: If you want to use a specific range of port
Package WZS. arithmetics; import Java. util. arrays; // generates 10 non-repeated numbers at random from 1 to 10. Public class test_wzs33 {public static void main (string [] ARGs) {int COUNT = 0; // generate the number counter int temp; // randomly generated number Boolean isexist = false; // determine whether the number exists true, false int [] arr = new int [10]; while (count
Output result:
[10
int randomnumber= (int) (Math.random () *8) +1; The comment is: Get a random integer from 1 to 8./*************************************************************************> File Name:GuessNumber.java> author:ma6174> Mail: [email protected]> Created time:2016 October 14 Friday 08:01 14 seconds************************************************************************/Import Java.util.Scanner;Class Guessnumber {public static void Main (string[] args) {int randomnumber = (int) (Math.random () *100) +
/*2. Determine the number of primes between 1-100 and the output of all primes.(The function of determining whether a number is prime is extracted into a method, which can be called directly in the loop)*/public class primenumber{public static void Main (String[]args){int count = 0;Generates 1~100 between A for loopfor (int i=1;i{if (IsPrime (i)) {System.out.println (i);count++;}}SYSTEM.OUT.PRINTLN ("Total number of primes between 1~100" +count);}Determine if a number is primepublic static Boole
PackageCn.magicdu.algorithm; Public classCirclenumber { Public Static voidMain (string[] args) { for(inti=10;i){ if(Iscirclenumber (i)) {System.out.println (i); } } } /*** Determine if it is a palindrome number *@paramnum *@return */ Private Static BooleanIscirclenumber (intnum) { intOldvalue=num; intNewvalue=0; while(num>0) {NewValue=newvalue*10+num%10;//get a bit of a numbernum/=10;//num reduces one bit } returnoldvalue==NewValue; }}
are not very concerned about whether you are fully accurate and use BigDecimal (double), pay attention to the special case of the double itself, the specification of the double itself defines several specific double values (Infinite,-infinite,nan ), do not pass these values to BigDecimal, or you will throw an exception.question two: Is it not to throw away the fractional part by forcing the double into int? int x= (int) 1023.99999999999999; x=1024 Why?The reason is that the binary cannot accura
Today would like to decompile the jar to save the source code, the default is preceded by a line number, do not know how this gadget set to remove the line number?Post-compilation:find its installation path, mine is: C:\Program Files\decomp. The path is then added to the environment variable path. First, the jar file to be deserialized, unzip to the same folder as the jar file name and enter the following command in the command input box: Jad winrar-o-r-df:\am-sjava f:\amclientsdk\**\*.classWher
Given a binary tree containing digits from only 0-9 , each root-to-leaf path could represent a number.An example is the Root-to-leaf path 1->2->3 which represents the number 123 .Find The total sum of all root-to-leaf numbers.For example, 1 / 2 3The Root-to-leaf path 1->2 represents the number 12 .The Root-to-leaf path 1->3 represents the number 13 .Return the sum = + = 25 .Subscribe to see which companies asked this question1 /**2 * Definition for a binary tree node.3 * public class Tr
Here's a small example to learn how to get a number in a stringImport Java.util.regex.matcher;import Java.util.regex.Pattern; Public classTest { Public Static void Main(string[] args) {String strinput ="[Email protected];33]; String regEx ="[^0-9]";//Match numbers within a specified range //pattern is a compiled expression pattern for regular expressionsPattern p = pattern.compile (regEx);//A Matcher object is a state machine that matches a str
Requirements:m is defined as a property of the class, the constructor must be defined as m assignment, and when m is greater than 99999 , the output error message " the number is too large"and is no longer executed. Public classT {Private intm; PublicTintm) {Super(); This. m =m; } Public intGetm () {returnm; } Public voidSetm (intm) { This. m =m; } Public voidM () {if(m>99999) {System.out.println ("The number is too large"); } Else{String str=m+ ""; System.out.println ("A t
Floating-point values do not apply to financial calculations that prohibit rounding errors. For example, command System.out.println (2.0-1.1) will print 0.8999999999999999999999999 instead of 0.9 as people think. The main reason is that the floating-point value is represented by a binary system, and the fractional 1/10 is not accurately represented in the binary system. It's as if the decimal doesn't exactly represent 1/3. If you need to have no rounding errors in the numerical calculations, you
To convert a string into a character arrayFirst letter judgmentIdea: How to get the first letterARR[0] The first element of the array is the first letterA number evaluates to True to a number false to a non-numeric"0123456789". Contains (arr[0]+ "");The alphabet determines that one of them is a letter.(arr[0]>= ' z ' arr[0]Other index positions can be judged simply by traversing the arrayNote: When writing this code, it should be a judgment to write the verification, after all the interpretation
Public Static LongSumdigits (LongN) { LongTotal=0; LongNumber=N; while(number!=0) { Total=total+number%10; number= (number-number%10)/10; } returnTotal ; } Public Static voidtestsumdigits () {System.out.println ("Enter a Long Integer:"); Scanner input=NewScanner (system.in); Longn=Input.nextlong (); Longresult=sumdigits (n); System.out.print (The sum of every number of the integer is: "+result); }A way for Java to return the sum of
Given an input string, reverse the string word by word. For Example,given s = "The Sky is Blue" and return "Blue is Sky the". Update (2015-02-12): For C programmers:try to solve it in-place in O (1) space.Test instructions: Reverse the word in the string, pay attention to the processing of the space!!!Publicclasssolution{publicstringreversewords (String s) {intlength=s.length (); //char[]ch=s.tochararray (); if (s==null| | length==0) returns;intend=length; stringbuilderret=newstringbuilder (); f
efficient multiplication operationclassDemo3_operator { Public Static voidMain (string[] args) {/** */ //shift left, move left a few is multiplied by 2 power several times//System.out.println (// -//System.out.println (// - /*00000000 00000000 00000000 00001100 12 complement (0) 0000000 00000000 00000000 000011000 24 complement ( 00) 000000 00000000 00000000 0000110000 48 complement*/ //shift right, move to the right, and divide by 2 power several times.//System.out.println
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.