Java BASIC Programming 50 questions (13-15 questions) detailed

Source: Internet
Author: User
Tags pow

First, describe

1, an integer, it plus 100 is a complete square number, plus 168 is a complete square number, what is the number?
Procedure analysis: Within 100,000 to judge, the number plus 100 after the root, and then the number plus 168 after the root, if the square is equal to the original number of the result.

2. Enter a certain day of the year to determine the day of the year?

Program Analysis: Take March 5 as an example, should first add up the first two months, and then add 5 days that is the first day of the year, special cases, leap years and enter the month greater than 3 o'clock need to consider more than one day.

3, enter three integers x, y, Z, please put these three numbers from small to large output.

Program Analysis: Put the smallest number on the X, first compare x with Y, if x>y the value of x and Y, and then compare with X and Z, if x>z the value of x and Z to Exchange, so that X is the smallest.


Second, the source code

1. Procedure 1

Package tong.yue.hong;/** * An integer, plus 100 is a complete square number, plus 168 is a complete square number, what is the number? Procedure analysis: Within 100,000 to judge, the number plus 100 after the root, and then the number plus 168 after the root, if the square is equal to the original number of the result. * @author Tong * */public class Squarenumber {public static void main (string[] args) {int result=0;for (int i=0;i<100001 ; i++) {if (Iscompsqrt (i+100) && iscompsqrt (i+168)) {result = I;break;}} System.out.println ("The number to be asked is:" +result); System.out.println ("----------"); for (int i=0;i<100001;i++) {if (Iscompsqrt (i+100) && iscompsqrt (i+168)) { result = I;break;}} System.out.println ("The number to be asked is:" +result);} Determine the total square number, using the cyclic enumeration method, the efficiency is relatively low private static Boolean iscompsqrt (int n) {Boolean iscomp = false;for (int i=1;i<math.sqrt (n) +1 i++) {//After the number is squared, find a number between 1 and the square root of the number, the square of the number equals the number judged//Call the Java Math.pow (i,2) function, ask for the square if (N==math.pow (i,2)) {Iscomp = true; Break;}} return iscomp;} Determine the total square number, using the root after the integer and the original number equal to determine the private static Boolean issqrt (int n) {double result = MATH.SQRT (n); if (result== (int) result) { return true;} else {return false;}}}

Operation Result:



2. Procedure 2

Package Tong.yue.hong;import java.util.scanner;/** * Enter a certain day of the year, judging the day is the first of the year? Program Analysis: Take March 5 as an example, should first add up the first two months, and then add 5 days that is the first day of the year, special cases, leap years and enter the month greater than 3 o'clock need to consider more than one day. * @author Tong * */public class days {public static void main (string[] args) {//with non-numeric characters as spacers scanner scanner = new Scanner (S ystem.in); System.out.println ("Please enter a Date: year (4 digits)--month (1-12)--Day (1-31):"); int year = Scanner.nextint (), while (year<0| | year>2050) {System.out.println ("Input year does not meet the requirements, please enter according to the actual year:"); years = Scanner.nextint ();} int month = Scanner.nextint (); while (month<0| | MONTH&GT;12) {System.out.println ("Input month does not meet the requirements, please enter according to the actual year (1-12):"); month = Scanner.nextint ();} int day = Scanner.nextint (), while (day<0| | day>31) {System.out.println ("Input date does not meet the requirements, please enter according to the actual year (1-31):");d ay = Scanner.nextint ();} Scanner.close (); Computedatebyarray (Year,month,day); computedatebycirculation (year,month,day);} Use the switch statement to determine the month and output the result private static void computedatebycirculation (int year, int month, Int. day) {int days = 0;for (int i = 1; I < month; i++) {switch (i) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:days +=31;break;case 4:case 6:case 9:case 11:days +=31;break;case 2:if (year%400==0| | year%4==0&&year%100!=0) {days +=29;} else {days +=28;}}} System.out.println (year+ "-" +month+ "-" +day+ "is the first" + (Days+day) + "Day of the year. ");}  Stores the number of days of each month in an array, loops out the value of the array to add to the number of days private static void Computedatebyarray (int year, int month, int day) {int[] DaysPerMonth = {31,28,31,30,31,30,31,31,30,31,30,31};//leap year judgment, can be divisible by 400, or divisible by 4 can not be divisible by 400, leap year February 29 days, common year 28 days if (year%400==0| | year%4==0&&year%100!=0) {dayspermonth[2] = 29;} int days = 0;for (int i = 1; i < month; i++) {//array starting with 0 index, so subscript minus one day +=dayspermonth[i-1];} System.out.println (year+ "-" +month+ "-" +day+ "is the first" + (Days+day) + "Day of the year. ");}}

Operation Result:



3. Procedure 3

Package Tong.yue.hong;import java.util.scanner;/** * Enter three integers x, y, Z, please put these three numbers from small to large output. Program Analysis: We try to put the smallest number on the X, first compare x with Y, if x>y the value of x and Y, and then compare X with Z, if x>z the value of X and Z, this will make x the smallest. * @author Tong * */public class Sortthreenum {public static void main (string[] args) {Scanner Scanner = new Scanner (System . in); System.out.println ("Please enter the first integer:"); int x = Scanner.nextint (); System.out.println ("Please enter a second integer:"); int y = Scanner.nextint ();   System.out.println ("Please enter the third integer:"), int z = scanner.nextint (); Scanner.close (); sort (x, y, z); sort2 Compare the size of three numbers, first place the minimum value in the X variable, and then compare the Y and z position data to the size of the private static void sort (int x,int y,int z) {if (x>y) {int t = X;x = Y;y = t;} if (x>z) {int t = X;x = Z;z = t;} if (y>z) {int t = Z;z = Y;y = t;} System.err.println ("Three integers are sorted after the result is:" +x+ "" +y+ "" +z);} Compare the size of a three number private static void Sort2 (int x,int y,int z) {if (x>y) {int temp = x; x = Y;y = temp;} The above steps indicate that x and Y are placed in the X variable, and now if z is smaller than x, z is the minimum, x second, y Max if (x>z) {int temp = x; x = z;z = Y;y = temp;} else {//now if z is larger than x, then x is the minimum value, Z and y continue to compare size if (z<y) {int temp = y; y = z;z = temp;}} System.err.println ("Three integers are sorted after the result is:" +x+ "" +y+ "" +z);}}

Operation Result:



Java BASIC Programming 50 questions (13-15 questions) detailed

Related Article

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.