Review University--Freshman Java Experiment--experiment three use __java of branch and cycle

Source: Internet
Author: User
experimental purposes

Master the branching structure of structured programming.

Learn to implement a single branch with a if/else statement.

Learn to implement multiple branches with IF/ELSE statements and Swich statements.

Master the cyclic structure of structured programming.

Learn to implement loops using a while statement, a do-while statement, and a for statement.

Learn to control the loop termination condition, the normal exit of the loop, the use of the continue statement, the break statement. Experimental Content

1. Write a application program, you can calculate the equation output ax2 + bx + c = 0 root, a,b,c three coefficients by the keyboard input

2. Write a application program that inputs 10 integers from the keyboard to calculate their arithmetic mean, maximum and minimum values.

3. Give a natural number n, in the console print side length of n Diamond.

For example: when n=3

*

***

*****

***

* Special Tips

1. In Boolean expressions, to determine whether two quantities A, b are equal, should be written as a==b; if written as a=b, assigning a value of B to a, not a Boolean expression, a syntax error occurs: Type mismatch:cannot convert from int to Boolean

2. Even inequalities cannot be used in Boolean expressions. For example 10<i<20 should be written i>10 && i<20

3. If statement, while statement, and a Boolean expression in the For statement, do not add a semicolon (;), for example
if (a>=3);
A=a+b;
Indicates that if the condition is met, an empty statement is executed, and the entire if statement is completed, and then the next statement is executed, so any value in the example above will execute a=a+b;.
In particular, in the while loop, an infinite loop is created. For example while (I<=100); i++ because the loop body is empty and cannot change the value of I, the loop will continue to execute.

4. In the Do-while () statement, the expression after the while must be followed by a semicolon.

5. If you want to execute multiple statements or have multiple statements in the loop body if the condition is met, be sure to enclose the statements in curly braces.

6. Else is always paired with the nearest neighbor if it is not already paired. If you want to change the pairing principle, use curly braces.

7. In the switch statement, if you execute only one condition item and exit the switch statement, do not forget to add a break;

8. Try not to declare variables in the circulation body, if you declare variables in the circulation body and initialize, then each cycle will have to apply for the variable memory and for its value, affect the efficiency of operation, or even logic errors.

9. In the arithmetic operator "/", if the number on both sides of the symbol is integral, the resulting result is an integral type, so in order to get the floating-point type, one of the numbers should be the floating-point type. For example, the average, can be written as sum/10.0


—————————————————————————————————————————————————————————————————————————————

Here's my Code.

—————————————————————————————————————————————————————————————————————————————


Import Java.util.Scanner;

public class Shiyan3 {public
	static void Main (string[] arg) {
		int n, I, A, J;		
		while (true) {
			n=0;
			System.out.println ("The max * num:");
			Scanner scan = new Scanner (system.in);
			if (Scan.hasnext ())
				n = scan.nextint ();

			for (i = 1; I <= 2 * n-1; i++) {
				a = Math.Abs (n-i);
				for (j = 1; J <= A; j +) {
					System.out.print ("");
				}
				for (j = 1; J <= 2 * (N-A)-1; j + +) {
					System.out.print ("*");
				}
				System.out.println ();}}}


Now look at that time to write the code is pretty good, although there is no annotation, naming arbitrary, but in addition to these shortcomings, the rest are the advantages, such as the Code concise (because the experiment requirements on these, haha).

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.