Java Basics--"Exercise one" process Control exercises __java

Source: Internet
Author: User
"Exercise One" process Control exercises

1, Require users to enter a year, judge and output the year is a leap years or excepting .

Tip: The criteria for judging leap years are: (year%4==0&&year%100!=0) | | (year%400==0)

Survey point: the judgment condition of leap year, conditional branching process (if else)

Package T1;
Import Java.util.Scanner;
public class testyear{public
		static void main (String []args) {
		System.out.print ("Please enter Year:");
		Scanner sc=new Scanner (system.in);
		int I=sc.nextint ();				
		Read in Data
		if ((i%4==0&&i%100!=0) | | (i%400==0)) The {
		//Boolean expression represents the year modulo 4=0 and modulo 100 is not equal to 0 is a leap year or the years modulo 400 equals 0 is a leap-age
			System.out.println (i+) is a leap years. "); 
			Print result I is a leap year.
		}else{
			
			System.out.println (i+) is excepting. ");
			Otherwise the print result I is excepting.
		}
		
		}
		
		
	   /**
		If it is a whole hundred years (such as 2000,1700) to be divisible by 400 is a leap year, otherwise for excepting (2000 leap year, 1700 excepting), if the whole hundred years (such as 2008,2009), as long as the division by 4 is a leap year, can not be divisible by 4 is excepting (2008 leap 2009 excepting).
	     the leap year of February has 29 days, excepting February has 28 days. 
	     leap Year has 366 days, excepting year and 365 days. There's a 0 or no 0 years divided by four, if you do it, it's a leap year, and then there are two or three 0, divided by 400, you can divide it into leap years
	     like 1600, divide by 400, 1900 by 400, but 1600, and 1900, Therefore 1600 are leap years.

	    */



}


2. The user is required to enter two integers to determine whether the first integer is a multiple of the second integer.

Tip: Use operator%

Point of investigation: modulo%, if statement

Package T1;
Import Java.util.Scanner;
public class testmodel{public
	static void main (String []args) {
		Scanner sc=new Scanner (system.in);
		Read the data
		System.out.print ("Please enter the first integer:");
		int I=sc.nextint ();
		Enter the first integer i
		System.out.print ("Enter a second integer:");
		int J=sc.nextint ();
		Enter the second integer J
		if (i%j==0) {
			//Determine whether I Mol j is equal to 0, equals 0, and output I is i/j times of J.
			System.out.println (i+ "is" +j+ "" +i/j+ "times. ");
		} else{
			//Otherwise output I is not a multiple of J.
			System.out.println (i+) is not a "+" multiple of "+j+". ");
		}
	}

}



3. Require the user to enter a year and a month, to determine (require the use of nested if...else and switch to judge each) how many days of the month.

Point of inspection: if else, switch, leap year judgment condition

Package T1;
Import Java.util.Scanner;
		public class testday{public static void Main (String []args) {Scanner sc=new Scanner (system.in);
		Read the Data System.out.print ("Please enter Year:");
		int I=sc.nextint ();
		Enter year I System.out.print ("Please enter month:");
		int J=sc.nextint (); Enter the month J if (i%4==0&&i%100!=0) | | (i%400==0)) {if (j==2) {System.out.println (i+ "+j+" month has 29 days.)
			"); }else if (j==4| | j==6| | j==9| | j==11) {System.out.println (i+ "+j+" month has 30 days.)
			"); }else{System.out.println (i+ "+j+" month has 31 days.
			"); }}else{if (j==2) {System.out.println (i+ "+j+" month has 28 days.)
			"); }else if (j==4| | j==6| | j==9| | j==11) {System.out.println (i+ "+j+" month has 30 days.)
			"); }else{System.out.println (i+ "+j+" month has 31 days.
			");
		} System.out.print ("Please enter Year:");
		int I1=sc.nextint ();
		Enter the year I1 System.out.print ("Please enter month:");
		int J1=sc.nextint (); Enter month J1 switch (J1) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:system.out.println (i1 + "Year" +j1+ month has 31 days.
			");
Case 4:			Case 6:case 9:case 11:system.out.println (i1+ "+j1+" month has 30 days.)
			"); Case 2:if (i%4==0&&i%100!=0) | | (i%400==0)) {System.out.println (i1+ "+j1+" month has 29 days.)
			"); }else{System.out.println (i1+ "+j1+" month has 28 days.
			");
			} break; DEFAULT:SYSTEM.OUT.PRINTLN ("entered incorrectly.")
					
			
		"); }/** 1, 3, 5, 7, 8, 10, 12, 31 days will never be the difference 4, 6, 9, 11, 30 days February (leap year 29 days, excepting 28 days calculation) 365 day leap year 366 days



4. Require the user to enter a student's score (1~100) and use the switch structure to determine what grade the score is (A, B, C, D, F ).

Tip: Switch (SCORE/10)

Research point: switch statement, int

Package T1;
Import Java.util.Scanner;
public class testscore{public
	static void main (String []args) {
		System.out.print ("Please enter your score (0~100):");
		Scanner sc=new Scanner (system.in);//Read Data	
		int i=sc.nextint ();
		Switch (I/10) {//integral variable/10= integer case
			10:if (i>100) {
				System.out.println ("Your score entered incorrectly.) ");
					} break;
			Case 9:system.out.println ("Your score is a, excellent.") ");
			Case 8:system.out.println ("Your grades are B, good.") ");
			Case 7:system.out.println ("Your grades are C, good.") ");
			Case 6:system.out.println ("Your grades are D, pass.") ");
			Case 5: Case
			4:system.out.println ("Your grade is E, come on.") ");
			Case 3: Case
			2:
			box 1: Case
			0:system.out.println ("Your grade is F, you should try.") ");
			Default:System.out.println ("Your grades have been typed incorrectly.") "); break;}}}





5, use while loop to find all odd and 1~100 within.

Investigate point: While statement

Package T1;
public class testwhile{public
	static void main (String []args) {
		int i,sum;			Define two integer variables I,sum
		i=1;				Initialize variable i with initial value of 1
		sum=0;				Initializes the variable sum with an initial value of 0 while
		(i<100) {
			sum=sum+i;		sum=1+3+5+ ...
			i=i+2;			Ensure that addends are
			
		all odd}
		System.out.println ("1~100 all odd and for:" +sum);
			
	}
	



6. Use while loop to find the sum of the 2+22+222+2222+22222 of the equation. p=p*10+2;

Investigate point: While statement

Package T1;
public class testwhile2{public
	static void main (String []args) {
		int i,sum;//define variable i,sum
		i=2;//initialization Variable i
		sum=0;//initializes the variable sum while
		(i<22222) {
			sum=sum+i*10+2;
			i=i*10+2;
		}
		SYSTEM.OUT.PRINTLN (sum);
		
			
	}
	


7, please program to verify the "Corner Valley conjecture": For any natural number, if it is odd, multiply it by 3 plus 1, and if the even number divides it by 2, this gets a new one, and then calculates it by the odd, even-numbered calculation rule, which will eventually get 1.

As for the natural numbers, These rules are followed by 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1.

requires the user to enter a natural number (positive integer), and then the programming output in turn to get 1 of the process.

Tip: For natural number n

do{

if () {n=n*3+1}else{}

}while (n!=1);

Observation point: do {}while (); statement

Package T1;
Import Java.util.Scanner;
public class testdowhile{//Corner Valley conjecture public
	static void main (String []args) {
		Scanner sc=new Scanner (system.in);
		Read the data
		System.out.print ("Please enter a natural number:");
		int I=sc.nextint ();
		do{
			if (i%2==1) {
					i=i*3+1;
				} else{
					i=i/2;
				}
			System.out.print (i+ ",");
	
		} while (i!=1);

	}
}




8 . Judge and output an integer that can be divisible by 3 and divisible by 6 within 500.

Investigate point: For loop, if statement

Package T1;
public class test10{public
	static void main (String []args) {for
		(int i=1;i<500;i++) {					//for loop 1~499
		if ((i%3==0) && (i%6==0)) {					//Meet Condition
				System.out.print (i+ "");
			}
	}}


9, using the FOR loop of nested programming output the following graphics:

*                  *

***              ***

*****          *****

*******      *******

*********  *********

Point of investigation: nesting for loops

Package T1;
public class test11{public
	static void main (String []args) {
		int n = ten;
		for (int i=1;i<=n;i++) {
			///First loop for
			(int j=1;j<=i*2-1;j++) {
				System.out.printf ("*");
			}
			Second loop for
			(int j=1;j<= (n-i) *4+1;j++) {
				System.out.print ("");
			}
			Third cycle for
			(int j=1;j<=i*2-1;j++) {
				System.out.print ("*");
			}
			
			System.out.println ();}}


10, using the for loop of nested programming output the following graphics:

*

***

*****

*******

*********

*******

*****

***

*

Hint: The graph can be divided into the upper and lower two parts respectively output

Point of investigation: nesting for loops

Package T1;
public class test12{public
	static void Main (String [] args) {//is divided into two parts, each part of the block printing graphics
	
		//upper part
		int n = 5;//graph Rows
		F or (int i=1;i<=n;i++) {
			//first loop, print triangle for
			(int j=1;j<=n-i;j++) {
				System.out.print ("");
			}
			Second loop, print middle triangle for
			(int j=1;j<=i*2-1;j++) {
				System.out.print ("*");
			}
			The third loop is the same as the first loop for
			(int j=1;j<=n-i;j++) {
				System.out.print ("");
			}
			
			
			
			System.out.println ();
		}
		Lower part
		int m = 5;
		for (int i=1;i<=m;i++) {
			///First loop for
			(int j=1;j<=i;j++) {
				System.out.print ("");
			}
			Second loop for
			(int j=1;j<=2* (m-i) -1;j++) {
				System.out.print ("*");
			}
			Third loop for
			(int j=1;j<=i;j++) {
				System.out.print ("");
			}
			SYSTEM.OUT.PRINTLN ()//NewLine
		}}
	}



11, the problem of moving bricks: 36 Bricks, 36 people move, men move 4, women move 3, two children carry 1 pieces.

Ask for a man, a woman, a child, a few people.

Point of investigation: nesting for loops

Package T1;
public class test13{public
	static void main (String []args) {//for loop nesting
		for (int n=0;n<=9;n++) {//Assuming all men, it takes 9 people for
			(int m=0;m<=12;m++) {//Suppose all women, need 12 for
				(int x=0;x<=36;x+=2) {//Suppose all children, up to 36, two children lift a brick, one plus two
					if ((4*n+3*m+x/2==36) && (n+m+x==36)) {//Find the Relationship type 36 bricks, 36 people move
						System.out.println ("Man:" +n+ "man, Woman:" +m+ "person, Child:" + X+ "A. ");
						
					}
				}
			}
		}
	
	
	
	}
}


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.