Javase Foundation 6

Source: Internet
Author: User

1. Keyboard entry month, output corresponding season
 Public classTest1 { Public Static voidMain (string[] args) {//Keyboard input a month, with scanner implementationScanner sc =NewScanner (system.in); //Receive DataSystem.out.println ("Please enter a month (1-12):"); intmonth =Sc.nextint (); //Judging the month is months, depending on the month output corresponding to the season        /*if (month = = 1) {System.out.println ("Winter");        } else if (month = = 2) {System.out.println ("Winter");        } else if (month = = 3) {System.out.println ("Spring");        } else if (month = = 4) {System.out.println ("Spring");        } else if (month = = 5) {System.out.println ("Spring");        } else if (month = = 6) {System.out.println ("Summer");        } else if (month = = 7) {System.out.println ("Summer");        } else if (month = = 8) {System.out.println ("Summer");        } else if (month = = 9) {System.out.println ("Autumn");        } else if (month = =) {System.out.println ("Autumn");        } else if (month = =) {System.out.println ("Autumn");        } else if (month = =) {System.out.println ("Winter");        } else {System.out.println ("The month you entered is incorrect"); }        */                //(month==3 | | month==4 | | month==5)        if(month==1 | | month==2 | | month==12) {System.out.println (Winter); }Else if(month==3 | | month==4 | | month==5) {System.out.println (Spring); }Else if(month==6 | | month==7| | month==8) {System.out.println (Summer); }Else if(month==9 | | month==10 | | month==11) {System.out.println (Autumn); }Else{System.out.println ("The month you entered is wrong"); }    }}

2. Print all palindrome numbers
 Public classTest2 { Public Static voidMain (string[] args) {//The 5-digit number tells us the scope of the data, with a For loop implementation         for(intx=10000; x<100000; X + +) {            //get every 5 digits and then get it in bits, 10 bits, thousand, million bits            intGE = x%10; intShi = x/10%10; intQian = x/10/10/10%10; intWAN = x/10/10/10/10%10; //The data output that satisfies the condition can be            if((Ge==wan) && (shi==Qian))            {System.out.println (x); }        }    }}
3. No Death Rabbit
 Public classTest3 { Public Static voidMain (string[] args) {//Defining Arrays        int[] arr =New int[20]; //initializes the first and second months of the rabbit logarithm CArr[0] = 1; arr[1] = 1;  for(intx=2; x<arr.length; X + +) {Arr[x]= Arr[x-2] + arr[x-1]; } System.out.println ("The 20th month of the rabbit logarithm is:" +arr[19]); }}
4. Find the elements that meet the requirements in the array and

1) Define a one-dimensional array of type int with {171,72,19,16,118,51,210,7,18}
2) Find the elements that meet the requirements in the array and.
The single and 10 bits of the sum element cannot contain 7, and can only be an even number.

 Public classTest4 { Public Static voidMain (string[] args) {//defines a one-dimensional array of type int        int[] arr = {171,72,19,16,118,51,210,7,18}; //define a summation variable        intsum = 0; //iterate through an array and get to each element in the array         for(intx=0; x<arr.length; X + +) {            if((arr[x]%10! = 7) && (arr[x]/10%10! = 7) && (arr[x]%2 = = 0) ) {sum+=Arr[x]; }} System.out.println ("Sum:" +sum); }}
6. Referee Ratings

In the programming contest, 6 judges scored for the contestants, with a score of 0-100 integers.
The final part of the contestant is divided into: the average of the remaining 4 players minus a maximum score and a minimum score.
Please write code implementation. (Regardless of the decimal part)

 Public classTEST5 { Public Static voidMain (string[] args) {//defines an array of length 6        int[] arr =New int[6]; //give the judges a score by typing the keyboardScanner sc =NewScanner (system.in);  for(intx=0; x<arr.length; X + +) {            //Arr[x] = Sc.nextint ();SYSTEM.OUT.PRINTLN ("Please give a" + (x+1) + "Jury score (0-100):"); intNumber =Sc.nextint (); ARR[X]=Number ; }                //Write method implementation gets the maximum value in the array, the minimum value        intMax =Getmax (arr); intMin =getmin (arr); //Write method to sum the elements of an array        intsum =sum (arr); //(and-highest score-lowest score)/(arr.length-2)        intAvg = (sum-max-min)/(arr.length-2); //output fractions can beSystem.out.println ("The player's final score is:" +avg); }        //sum of array elements     Public Static intSumint[] arr) {        intsum = 0;  for(intx=0; x<arr.length; X + +) {sum+=Arr[x]; }                returnsum; }        //the minimum value in the array     Public Static intGetmin (int[] arr) {        intMin = arr[0];  for(intX=1; x<arr.length; X + +) {            if(Arr[x] <min) {min=Arr[x]; }        }                returnmin; }        //the maximum value in the array     Public Static intGetmax (int[] arr) {        intmax = Arr[0];  for(intX=1; x<arr.length; X + +) {            if(Arr[x] >max) {Max=Arr[x]; }        }                returnMax; }}
7. Array inversion

(1) Keyboard input data storage array of 5 int types in arr
(2) Define the method to invert the contents of the ARR array
(3) Define the method to traverse the inverted array

 Public classTest6 { Public Static voidMain (string[] args) {//defines an array of length 5        int[] arr =New int[5]; //assigning values to elements in an array via keyboard input dataScanner sc =NewScanner (system.in);  for(intx = 0; x < arr.length; X + +) {System.out.println ("Please give first" + (x + 1) + "elements"); ARR[X]=Sc.nextint (); } System.out.println ("Array element before inversion:");        PrintArray (arr); //defines the method that reverses the contents of the ARR arrayreverse (arr); System.out.println ("Inverted array element:"); //defining methods to iterate through an arrayPrintArray (arr); }        //iterating through an array     Public Static voidPrintArray (int[] arr) {System.out.print ("[");  for(intx=0;x<arr.length; X + +){            if(x = = Arr.length-1) {System.out.println (arr[x]+"]"); }Else{System.out.print (arr[x]+", "); }        }    }        /** Two explicit: return value type: void parameter list: int[] arr*/     Public Static voidReverseint[] arr) {         for(intstartindex=0,endindex=arr.length-1;startindex<=endindex;startindex++,endindex--) {            inttemp =Arr[startindex]; Arr[startindex]=Arr[endindex]; Arr[endindex]=temp; }    }}
8. Array Basic Lookup

Array element Lookup (finds the index of the first occurrence of the specified element in the array)

 Public classTest7 { Public Static voidMain (string[] args) {int[] arr = {5,4, 3, 2, 1 }; //the element to be queried is determined by the way the keyboard is enteredScanner sc =NewScanner (system.in); System.out.println ("Please enter the element you want to find:"); intNumber =Sc.nextint (); //defines a method for finding where an array element first appears//Calling Methods        intindex =GetIndex (arr, number); System.out.println ("Index:" +index); }         Public Static intGetIndex (int[] arr,intvalue) {        //iterate through the array, get to each element, compare, and if you want to wait, return the index directly at that place.                 intindex =-1;  for(intx=0; x<arr.length; X + +) {            if(Arr[x] = =value) {Index=x;  Break; }        }                returnindex; }}

Javase Foundation 6

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.