Java BASIC Programming Exercises

Source: Internet
Author: User

1, write the program to achieve a given 4 integers from the large to small order.

package Homework01;import Java.util.scanner;public class HomeWork01 {static int    number=4;            Enter 4 numbers stored in the array static int[] T1 = new Int[number]; public static void Main (string[] args) {HomeWork01 jiejie=new HomeWork01 (); Jiejie.shunxun ();}        void Shunxun () {System.out.println ("Please enter 4 numbers:");           Scanner in_t1 = new Scanner (system.in);//Loop input array for (int i=0;i<number;i++) {t1[i]=in_t1.nextint ();}            for (int i = 0; i < t1.length; i++) {int pos = i;            for (int j = i + 1; j < T1.length; J + +) {if (T1[pos] > t1[j]) pos = j;                } if (pos! = i) {T1[i] = T1[i] + T1[pos];                T1[pos] = T1[i]-T1[pos];            T1[i] = T1[i]-T1[pos]; }} for (int i = t1.length-1; I >= 0; i--) System.out.print (t1[i] + "\ t");}} 

2, write the program to find the root of a two-time equation.

Package Homework02;import Java.util.scanner;public class homework02//=b^2-4ac value, if less than 0, a two-time equation without root. If it equals 0, The two-time equation has two equal roots. If greater than 0, the unary two-time equation has two unequal real-root {public static void main (String [] args) {Scanner sc = new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Input 2 coefficients"); int a = Sc.nextint (); SYSTEM.OUT.PRINTLN ("Input 1 coefficients"); int b = Sc.nextint (); SYSTEM.OUT.PRINTLN ("Input 0 coefficients"), int c = Sc.nextint (), if ((b*b-4*a*c) <0) {//Determine if the equation has a solution System.out.println ("The equation has no solution!) "); return;} Else{system.out.println (the equation has a solution!) ");} Double X1 = (-B + math.sqrt (b*b-4*a*c))/2*a;double x2 = (-b-math.sqrt (b*b-4*a*c))/2*a; System.out.println ("root is" + x1 + "\ T" + x2);}}

3, write the program, enter a character, determine whether it is a lowercase letter, if it is, convert it to a large

Write mother, otherwise, do not convert.

Package Homework03;import Java.util.scanner;public class HomeWork03 {public static void main (string[] args) {// The ASCLL value of the lowercase letter is 65-90system.out.println ("Please enter a letter: \ n") for the ASCLL value of 97-122//capital letters; Scanner input = new Scanner (system.in); char Zimu=input.next (). charAt (0);          if (zimu>=97&&zimu<=122) {//determines whether the lowercase letter is          System.err.println ("The letter is lowercase");          Zimu= (char) (zimu-32);//If it is lowercase, it is converted to an uppercase letter          System.err.println ("The uppercase letter after conversion is:" +zimu);          }           else{         System.out.println ("The letter is not a lowercase letter!") ");                  }}}

4. Enter 3 positive numbers to determine if a triangle can be formed.

Package Homework04;import Java.util.scanner;public class HomeWork04 {public static void main (String [] args) {int A;int b;i NT C; System.out.println ("Please enter three positive integers:"); Scanner in=new Scanner (system.in); A=in.nextint (); B=in.nextint (); C=in.nextint (); if (a<=0| | b<=0| | c<=0) {System.out.println ("the input must be a positive integer!") ");} if ((a+b) >c&& (a+c) >b&& (b+c) >a) {System.out.println ("can make a triangle!") ");} Else{system.out.println ("Can't make a triangle!") ");}}}

5, write the program, on the input of the year, month, day, given that the day is the number of days of the year?

Package Homework05;import Java.util.scanner;public class HomeWork05 {public static void main (String[]args) {Scanner sc = New Scanner (system.in); System.out.print ("year"); int year=sc.nextint (); System.out.print ("month"); int month=sc.nextint (); System.out.print ("Day"); int day=sc.nextint (); int Days=0;switch (month) {case 12:days+=30;case 11:days+=31;case 10:days+ =30;case 9:days+=31;case 8:days+=31;case 7:days+=30;case 6:days+=31;case 5:days+=30;case 4:days+=31;case 3:if ((year%4 ==0&&year%100!=0) | | (year%400==0)) {days+=29;} else{days+=28;} Case 2:days+=31;case 1:days+=day;} System.out.print ("First" + Days + "Day");}}

6, write the program, from the keyboard input an arbitrary number between 0~99999, judge the number of inputs is a few

Number?

Package Homework06;import Java.util.scanner;public class HomeWork06 {public static void main (String[]args) {Scanner sc = New Scanner (system.in); System.out.print ("Please enter an arbitrary number between 0~99999"); int number=sc.nextint (); if (number/10000>=1&&number/10000< {System.out.println (number+ "\ t is a 5-digit number");} else if (number/1000>=1) {System.out.println (number+ "\ t is 4-digit");} else if (number/100>=1) {System.out.println (number+ "\ t is 3-digit");} else if (number/10>=1) {System.out.println (number+ "\ t is 2-digit");} else if (number/1>=1) {System.out.println (number+ "\ t is 1-digit");}}}

7, write a program, given a student's performance, give the corresponding level:

90~100 Excellent

80~89 Good

70~79 Medium

60~69 Pass

0~59 failed.

Package Homework07;import Java.util.scanner;public class HomeWork07 {public static void main (string[] args) {HomeWork07 J Iejie=new HomeWork07 (); Jiejie.chengjie ();} void Chengjie () {Scanner sc = new Scanner (system.in); System.out.println ("Please enter student score:"), int a = Sc.nextint (), if (a>=90&&a<=100) {System.out.println ("the student's score is" + A + "\ t excellent performance");} else if (a>=80&&a<90) {System.out.println ("the student's score is" +a+ "\ t has good results");} else if (a>=70&&a<80) {System.out.println ("the student's score is" +a+ "\ t grade is Medium");} else if (a>=60&&a<70) {System.out.println ("the student's score is" +a+ "\ T has passed");} Else{system.out.println ("The student's performance is" +a+ "\ T failed the Grade");}}

8, write the program, the input of an integer, in reverse order output the number. For example, the input is 3578,

The output is 8753.

Package Homework08;import Java.util.scanner;public class HomeWork08 {public static void main (String[]args) {Scanner sc = New Scanner (system.in); System.out.println ("Please enter an integer:"); int read = Sc.nextint ();//Method one     reverse () APISystem.out.println ("Method one:"); StringBuilder SB  =  new StringBuilder (string.valueof (read)); System.out.println (Sb.reverse ());//Method Two   converts a string into a character array, an inverted output string str= read + ""; char Fuzu[]=str.tochararray (); String temp= ""; for (int a=fuzu.length-1;a>=0;a--) {temp=temp+fuzu[a];} System.out.println ("Method two:"); SYSTEM.OUT.PRINTLN (temp);}}

9, using while loop, calculates the sum of all 3 multiples of 1~200.

Package Homework09;public class HomeWork09 {public static void main (string[] args) {//with a while loop, computes the sum of all multiples of 3 between 1~200. int A=1;int Sum=0;while (a<=200) {if (a%3==0) {sum=sum+a;} a++;} System.out.println ("Sum of all multiples of 3 between 1~200:" +sum);}}

10, write the program, output the 200~500 between all primes.

Package Homework10;public class HomeWork10 {public static void main (string[] args) {int num=200;while (num<=500) {Boole An tag=true;//prime number tag for (int d=2;d<=num-1;d++) {if (num% d==0) {tag=false;break;}} if (tag) {//If it is a prime System.out.println (num);} num++;}}}

11, write procedures to solve the "hundred money to buy hundred chickens" problem. Rooster Five Money One, hen three money A, chick

A money of three, the existing hundred money to buy hundred chickens, how many kinds of buying method?

Package Homework11;public class HomeWork11 {public static void main (string[] args) {/*, write programs to solve the "hundred money buy Hundred chickens" issue. * The Rooster Five Money One, the hen three money one, * the chicken one money three, * the existing hundred money wants to buy hundred chickens, altogether how many kinds buys the law? */for (int g=0;g<=20;g++) {for (int. m=0;m<=33;m++) {for (int x=0;x<=100-g-m;x++) {if (x% 3==0 && 5*g+m*3+x /3 = = && g+m+x ==100) {System.out.println ("Rooster" +g+ "only Hen" +m+ "only Chick" +x+ "only");}}}}}

12. Use the loop statement to output the following graphic.

#

# # #

# # # # #

# # # # # # #

# # # # # # # # #

Package Homework12;public class HomeWork12 {public static void main (string[] args) {int aa=-1;for (int a=0;a<5;a++) {aa+ =2;for (int b=1;b<=aa;b++) {System.out.print ("#"  );} System.out.println ();}}}

13, verify "Ghost Valley conjecture": For any natural number, if odd, it is multiplied by 3 plus 1;

Even, divide it by 2, get a new number, and then calculate it in accordance with the above calculation rule

Go on, and eventually get 1.

Package Homework13;import java.util.Random;; public class HomeWork13 {public static void main (string[] args) {/* * verify "Ghost Valley conjecture": For any natural number, if odd, * multiply it by 3 plus 1; if even, divide it by 2. Get a new number, * and then calculate according to the above calculation rule, * keep going on, and eventually get 1. */int num; Random rd=new random ();//integer.max_value is the largest integer num=1+rd.nextint (integer.max_value);//range of Generation -2[31]----2[31]-1// System.err.println (Rd.nextint (100));//Generate several >=0 and <100system.out.println ("The original number is" +num); while (Num!=1) { System.out.println ("The new number produced is" +num); if (num%2==0) {//even NUM=NUM/2;} else{num=num*3+1;}} SYSTEM.OUT.PRINTLN (num);}}

14, programming to find all the "total number" between 1~10000, the total number is the sum of all the factors of the number of the number. For example, 6 of the factors are 1, 2, 3, and 6=1+2+3, so 6 is a complete number.

Package Homework14;public class HomeWork14 {public static boolean isyinzi (int num) {int sum=0;//determine if an integer is an exact number for (int d=n um-1;d>=1;d--) {if (num%d==0) {Sum+=d;}} return sum==num;} public static void Main (string[] args) {//TODO auto-generated Method stub/* * Programming for all "complete numbers" between 1~10000, * Total number is the sum of all the factors of the number * on the Number of the numbers. For example, a factor of 6 has 1, 2, 3, * and 6=1+2+3, so 6 is a full number */for (int a=1;a<=1000;a++) {int num=a;if (num)) {Isyinzi (num);}}}

15, the sum of the numbers of an integer can be divisible by 9, then the number can be divisible by 9. Programming validation to

Integer can be divisible by 9.

Package Homework15;import Java.util.random;public class HomeWork15 {public static void Panduan (int num) {int b=num/100;// hundred int s=num%100/10;//10-digit int g=num%10;//single digit if (num%9==0) {System.out.println (num+ "can be divisible by 9"); if ((b+s+g)%9==0) { System.out.println ("At the same time" +num+ "the sum of the individual digits can be divisible by 9");} Else{system.out.println ("But the sum of the individual digits of the" +num+ "cannot be divisible by 9");}} Else System.out.println ("Next test!");} public static void Main (string[] args) {random rd=new random (); int shu=10+rd.nextint (n); Shu =shu *9;panduan (shu);}}

16, the monkey eat peach problem. The monkey picked a few peaches on the first day, then ate half of it,

and ate one more. The next day he ate half the remaining peaches and ate one more. Eat every day after

Half the rest of the day before the zero one. On the 10th day when you want to eat a peach left, the first day of the total

How many peaches are picked off?

Package Homework16;public class HomeWork16 {public static void main (string[] args) {int total=1;int day=10;for (int i = 10 ; I >0; i--) {System.out.println ("+day+" days, there are Peaches "+total+"); total= (total+1) *2;day--;}}

17, Narcissus number refers to an n-digit (N≥3), its number on each bit of the sum of the power of N and so on

to it itself. (Example: 1^3 + 5^3 + 3^3 = 153). Programming to find the number of all three-bit daffodils.

Package Homework17;public class HomeWork17 {public static void main (string[] args) {for (int num=100;num<1000;num++) { if (Isshuixian (num)) {System.out.println (num);}}} Judge whether a number is Narcissus number public static Boolean isshuixian (int num) {int b=num/100;int s=num%100/10;int G=num%10;return Math.pow (b, 3) +math.pow (S, 3) +math.pow (g, 3) ==num?true:false;}

18, known xyz+yzz=532, where x, Y, Z is a number, programming to find the values of x, Y and Z.

19, Classical problems: There are a pair of rabbits, from the 3rd month after birth every month, a pair of rabbits, rabbits

Zichang to the third month after a month and a pair of rabbits, if the rabbit is not dead, ask the total number of rabbits per month

For how much?

Package Homework19;import Java.util.scanner;public class HomeWork19 {/* * Classical questions: A pair of rabbits, * from the 3rd month after birth a pair of rabbits every month, * the rabbit after the third month Every month a pair of rabbits, * if the rabbit will not die, * Ask the total number of rabbits each month? */public int rubbit (int mon) {if (mon<=2) {return 1;} Else{return rubbit (mon-1) +rubbit (mon-2);}} public static void Main (string[] args) {int r=1;int rr=1;int rrr=1; System.out.println ("Method one:"); for (int a=1;a<=12;a++) {//12 months if (a<=2) {r=1;} else{//the current month equals the first two months and r=rr+rrr;rrr=rr;rr=r;} System.out.println (r*2);} System.out.println ("Method two, the number of rabbits in the specified month:"); HomeWork19 jisuan=new  HomeWork19 (); System.out.println ("Please enter the month:"); Scanner sc=new Scanner (system.in); int yue=sc.nextint (); The number of rabbits in System.out.println (yue+ "Month is" + (Jisuan.rubbit (yue)) *);}}

20, the decomposition of a positive integer factorization. For example: Enter 90 and print out 90=2*3*3*5.

Package Homework20;import Java.util.scanner;public class HomeWork20 {public static void main (string[] args) { System.out.println ("Please enter an integer:"); Scanner sc=new Scanner (system.in); int Num=sc.nextint (); System.out.println (num+ "of the quality Factor is:"); for (int i=2;i<num;i++) {while (num%i==0) {num/=i; System.out.print (i+ "");}} System.out.print ("  " +num);}}

Java BASIC Programming Exercises

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.