A simple Java program

Source: Internet
Author: User

1. Two-point lookup, Java input and output
1  Packagecom;2 3 4  Public classfirstapp{5      Public Static voidMain (String args[])6     {7Java.util.Scanner sc=NewJava.util.Scanner (system.in);8         intN,start,end,middle;9System.out.println ("Please Enter:");Tenn=sc.nextint (); One         inta[]={-2,1,4,5,8,12,17,23,45,56,90,100}; AStart=0; -End=a.length-1; -Middle= (start+end)/2; the         intCount=0; -          while(n!=A[middle]) { -             if(n>A[middle]) { -start=Middle; +             } -             Else if(n<A[middle]) { +End=Middle; A             } atMiddle= (start+end)/2; -count++; -                 if(COUNT&GT;A.LENGTH/2)     -                      Break; -             } -         if(COUNT&GT;A.LENGTH/2) inSystem.out.println (":" +n+ "not in array"); -         Else toSystem.out.println (":" +n+ "is an array of" +middle+ "elements"); +          -         } the}
View Code

Class and object of 2.java, new initializes the object

1  Packagecom;2 classXiyourenwu {floatHeight,weight;3 String Head, Ear,hand,foot,mouth;4 voidspeak (String s)5 { 6Head= "Crooked Head";7 System.out.println (s);8 }9 }Ten  One  Public classExmple { A      Public Static voidMain (String args[]) - {Xiyourenwu zhubajie,sunwukong; -Zhubajie=NewXiyourenwu (); thesunwukong=NewXiyourenwu (); -zhubajie.height=1.80f;//object assigns a value to its own variable.  -zhubajie.weight=160f;  -Zhubajie.hand= "Two black Hands"; +zhubajie.foot= "Two Feet";  -Zhubajie.head= "Big Head";  +Zhubajie.ear= "A pair of big ears";  AZhubajie.mouth= "a big Mouth";  atsunwukong.height=1.62f;//object assigns a value to its own variable.  -sunwukong.weight=1000f;  -sunwukong.hand= "Small Hands"; -sunwukong.foot= "Two Embroidered feet";  -Sunwukong.head= "embroidered floating serves floating";  -Sunwukong.ear= "A pair of little ears";  inSunwukong.mouth= "Cherry Small Mouth"; -System.out.println ("Zhubajie Height:" +zhubajie.height); toSystem.out.println ("Zhubajie's head:" +zhubajie.head); +System.out.println ("Sunwukong Weight:" +sunwukong.weight); -System.out.println ("Sunwukong's head:" +sunwukong.head); theZhubajie.speak ("My old pig I want to marry a daughter-in-law");//object to invoke the method.  *System.out.println ("Zhubajie now head:" +zhubajie.head); $Sunwukong.speak ("Lao sun I weigh 1000 pounds, I want to cheat eight to recite Me");//object to invoke the method. Panax NotoginsengSystem.out.println ("Sunwukong now head:" +sunwukong.head); -  the     } +  A}
View Code

A simple Java program

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.