Java Fundamentals 02

Source: Internet
Author: User

Expression: A statement or part of a statement consisting of operands (operators) and operands

All expressions have a return type

1) The return type of the assignment expression is the type of the variable being assigned, and the value of the variable is returned as the result

2) The statement that defines the variable has no return type (return type is void)

3) Output statement has no return type

Any one expression can be calculated with other expressions, as long as the return type satisfies the requirement

Return type: The type of the result of a code operation

1. Prompt the user to enter the width of the rectangle, and then calculate and output the perimeter of the rectangle

Import Java.util.Scanner;

public class Problem1 {

public static void Main (string[] args) {

Scanner scan=new Scanner (system.in);

SYSTEM.OUT.PRINTLN ("Please output rectangle width:");

Double width=scan.nextdouble ();

SYSTEM.OUT.PRINTLN ("Please output Rectangle High:");

Double height=scan.nextdouble ();

Double zhouchang=width*2+height*2;

System.out.println ("The perimeter of the rectangle is:" +zhouchang);

}

}

Output Result:

Please output the width of the rectangle:

3

Please output the height of the rectangle:

6

The perimeter of the rectangle is: 18.0

2. Achieve the following effect:

Please enter the first number: 3

Please enter a second number: 6

3*6=18

3+6=9

3/6=0.5

3-6=-3

Import Java.util.Scanner;

public class Problem2 {

public static void Main (string[] args) {

Scanner scan=new Scanner (system.in);

System.out.print ("Please enter the first number:");

int Num1=scan.nextint ();

System.out.print ("Please enter a second number:");

int Num2=scan.nextint ();

SYSTEM.OUT.PRINTLN (num1+ "*" +num2+ "=" +num1*num2);

System.out.println (num1+ "+" +num2+ "=" + (num1+num2));

System.out.println (num1+ "/" +num2+ "=" + (double) num1/num2);

System.out.println (num1+ "-" +num2+ "=" + (num1-num2));

}

}

Output Result:

Please enter the first number: 3

Please enter a second number: 6

3*6=18

3+6=9

3/6=0.5

3-6=-3

3. Use the console and the three-mesh operator to write the following procedure:

Please enter the first number: 2

Please enter a second number: 3

The first number you enter is smaller than the second number.

Import Java.util.Scanner;

public class Problem3 {

public static void Main (string[] args) {

Scanner scan=new Scanner (system.in);

System.out.print ("Please enter the first number:");

int Num1=scan.nextint ();

System.out.print ("Please enter a second number:");

int Num2=scan.nextint ();

Mode 1

String A,b,c;

A= "The first number you enter is larger than the second number";

B= "The first number you enter is smaller than the second number";

c= "two numbers equal";

String Result=num1>num2?a: ((num1==num2)? c:b);

Mode 2

String result=num1==num2? " Two numbers equal ":(num1>num2?" The first number you entered is larger than the second number ":" The first number you entered is smaller than the second number ");

SYSTEM.OUT.PRINTLN (result);

}

}

Output Result:

Please enter the first number: 3

Please enter a second number: 2

The first number you enter is larger than the second number

4. Use the console to prompt the user to enter the current time, if less than or equal to 12 points output good morning, if between 12 and 18 points output good afternoon, otherwise the output good evening.

Import Java.util.Scanner;

public class Problem4 {

public static void Main (string[] args) {

Scanner scan=new Scanner (system.in);

System.out.print ("Please enter the current time (12.32 = 12:32):");

Double time=scan.nextdouble ();

String result=time<0? " You entered the wrong time ":(time>0&&time<=12?" Good morning, ":(time>12&&time<18?" Good afternoon ":" Good Evening "));

SYSTEM.OUT.PRINTLN (result);

}

}

Output Result:

Please enter the current time (12.32 is 12:32): 1.3

Good morning

5. Prompt the user to enter a positive integer, according to the user's input to determine whether the number is odd or even, and output results. (If the user enters a negative number or 0, the error is incorrect)

Import Java.util.Scanner;

public class Problem5 {

public static void Main (string[] args) {

Scanner scan = new Scanner (system.in);

System.out.print ("Please enter a positive integer:");

int num = Scan.nextint ();

String result=num>0? (num%2==0? ") The number you entered is even ":" The number you entered is odd "):" You entered the wrong number ";

SYSTEM.OUT.PRINTLN (result);

}

}

Output Result:

Please enter a positive integer: 3

The number you entered is odd

Please enter a positive integer: 2

The number you entered is even

Please enter a positive integer: 0

The number you entered is incorrect

7. A financial company launches a financial services, the service rules are as follows:

If the user's financial amount is below 500,000 yuan, the annual income is calculated according to 4%.

If the user's financial amount is more than 500,000 yuan (including 500,000), the annual income is calculated according to 4.5%.

If the user's financial amount exceeds 2 million, in addition to financial benefits, but also to give the user the amount of 10% of the income

Develop a console application that allows users to enter financial and financial periods (years) to calculate and output the benefits that users will receive when their financial maturity expires.

Import Java.util.Scanner;

public class Problem7 {

public static void Main (string[] args) {

Scanner scan=new Scanner (system.in);

System.out.println ("Please enter the amount of money you want to enter (unit: million):");

int Money=scan.nextint ();

System.out.println ("Please enter the financial period you want to enter (in years):");

int Year=scan.nextint ();

Sum1 for Scenario 1: If the user's financial amount is below 500,000 yuan, the annual income is calculated according to 4%.

Double sum1= (double) 4*money*year/100;

Sum2 for Scenario 2: If the user's financial amount is more than 500,000 yuan (including 500,000), the annual income is calculated according to 4.5%.

Double sum2= (double) 45*money*year/1000;

SUM3 for Scenario 3: If the user's financial amount exceeds 2 million, in addition to the financial benefits, but also to give the user the amount of 10% of the income

Double sum3= (double) 45*money*year* (10+1)/10000;

String income= (money<=0?

"The amount you entered is incorrect":

(money>0&&money<=50?) ("Your financial gain is:" +SUM1):

(money>50&&money<=200? ") Your financial gain is: "+SUM2:" Your financial gain is: "+sum3)");

System.out.println (income);

}

}

Output Result:

Please enter the amount of money you want to enter (in million):

20

Please enter the financial period (in years) you want to enter:

1

Your financial gain is: 0.8

Please enter the amount of money you want to enter (in million):

50

Please enter the financial period (in years) you want to enter:

1

Your financial gain is: 2.25

Please enter the amount of money you want to enter (in million):

300

Please enter the financial period (in years) you want to enter:

1

Your financial gain is: 14.85

8. Let the user enter height (cm) and weight (kg), using the BMI calculation method to determine the user's physical condition (thin, normal, overweight)

BMI calculation method:

BMI = weight (kg) divided by height (m) squared

Underweight if BMI is less than 18.5

Overweight if BMI is greater than 22.9

Others are normal

Import Java.util.Scanner;

public class Problem8 {

public static void Main (string[] args) {

Scanner scan=new Scanner (system.in);

System.out.println ("Please enter your height (in cm)");

Double tall=scan.nextdouble ();

System.out.println ("Please Enter your weight (unit: Catty)");

Double weight=scan.nextdouble ();

Double bmi= (WEIGHT/2)/((tall/100) * (tall/100));

String result=bmi<18.5? " Skinny ":(bmi>22.9?" Overweight ":" normal ");

SYSTEM.OUT.PRINTLN (result);

}

}

Output Result:

Please enter your height (in cm)

171

Please enter your weight (unit: Jin)

125

Normal

9. Write a temperature conversion application that allows the user to enter a Celsius temperature (integer) and convert it to the Fahrenheit temperature output (integer).

Fahrenheit temperature = (5/9) * (Celsius temperature-32)

Import Java.util.Scanner;

public class Problem9 {

public static void Main (string[] args) {

Scanner scan=new Scanner (system.in);

System.out.println ("Please enter a Celsius temperature (integer)");

int Tpr=scan.nextint ();

Double result= ((double) 5/9) * (TPR-32);

int finalresult= (int) result;

SYSTEM.OUT.PRINTLN (result);//See if the result of the output is correct

System.out.println ("Fahrenheit temperature:" +finalresult);

}

}

Output Result:

Please enter a Celsius temperature (integer)

35

Fahrenheit Temperature: 1

10. Use the console output statement to directly output the value of the variable after the execution of the following assignment statement (if there is an error, indicate the error).

int a = 2.24;

int a = 2.24f;

float k = 2.24;

2.24f = float k;

float k = 2.24f;

int A + = 1;

int a = 2.1 * 2;

int a = 2/3;

int a = 5/2;

int a = 5.0/2;

int a = 5.0/2.0;

bool A = 5.0/2.0==7.5/3;

bool A = 5/2 = = 7.5/3;

bool A = 5.0/2.0==7.5/3 && 5/2==7.5/3;

bool A = 5/2 = = 7.5/3 | | 5.0/2.0 = = 7.5/3;

int a=1; a=a++;

int a=1; A=++a;

int a=1; A+=a;

int a=1; a+=a++;

int a=1; A+=++a;

int a=1; a+= (a++) + (++a);

int a=1; a+= (a++)-(++a) * (a--);

int a=1; A=a/3==0?a++:++a;

int a=1; A+=a/3==0?a++:++a;

int a=1; A=a/3!=0?a++:++a;

int a=1; a+=! (a/3==0)? A++:++a;

float F = 2.0*3.0;

int a = 1; A = A++%++a;

int a = 1; a%= a++%++a;

int a = 1; A = ((a+a++) *5+ ++a) *a;

int a = "1" +2;

int a = (1+2) + "3";

String a = (1+2) + "3";

String a = 1+2+ "3";

String a = "3" +2+ "1";

String a = "3" +2+1;

String a = ("3" +2) +1;

int a = 5/((3+1)%5==0?7:2) *6/3;

public class PROBLEM10 {

public static void Main (string[] args) {

System.out

. println ("int a = 2.24; Error. 2.24 is a double type, and a variable A that assigns it to type int cannot use implicit conversions. ");

System.out

. println ("int a = 2.24f; error. 2.24 is a double type that is explicitly converted to a float type and cannot be assigned to a variable a of type int. ");

System.out

. println ("float k = 2.24; error. 2.24 The default double type, which cannot be assigned to a variable k of type float, is considered likely to overflow. ");

System.out.println ("2.24f = float k; error. The left side must be a variable, and 2.24f is a constant ");

System.out.println ("float k = 2.24f; correct, k=2.24");

SYSTEM.OUT.PRINTLN ("int A + = 1; error. The variable A is initialized before the variable is evaluated, that is, the A is assigned first. ");

System.out

. println ("int a = 2.1 * 2; error. The return type of an assignment expression is the type of the variable being assigned, the return type of the assignment expression is type double, and a is the int type. ");

SYSTEM.OUT.PRINTLN ("int a = 2/3; correct. A=0 ");

SYSTEM.OUT.PRINTLN ("int a = 5/2; correct. A=2 ");

System.out

. println ("int a = 5.0/2; error. The return type of an assignment expression is the type of the variable being assigned, the return type of the assignment expression is type double, and a is the int type. ");

System.out

. println ("int a = 5.0/2.0; error. The return type of an assignment expression is the type of the variable being assigned, the return type of the assignment expression is type double, and a is the int type. ");

System.out.println ("Boolean a = 5.0/2.0==7.5/3; correct. A is false ");

System.out.println ("Boolean a = 5/2 = = 7.5/3; correct. A is false ");

System.out

. println ("Boolean a = 5.0/2.0==7.5/3 && 5/2==7.5/3; correct. A is false ");

System.out

. println ("Boolean a = 5/2 = = 7.5/3 | | 5.0/2.0 = = 7.5/3; correct. A is true ");

SYSTEM.OUT.PRINTLN ("int a=1; a=a++; correct. A=2 ");

SYSTEM.OUT.PRINTLN ("int a=1; A=++a; correct. Output a value of 1 ");

SYSTEM.OUT.PRINTLN ("int a=1; A=++a; correct. Output a value of 2 ");

SYSTEM.OUT.PRINTLN ("int a=1; A+=a; correct. Output a value of 2 ");

SYSTEM.OUT.PRINTLN ("int a=1; a+=a++; correct. Output a value of 2 ");

SYSTEM.OUT.PRINTLN ("//int a=1; A+=++a; correct. Output a value of 3 ");

SYSTEM.OUT.PRINTLN ("int a=1; a+= (a++) + (++a); Output a value of 5 ");

SYSTEM.OUT.PRINTLN ("int a=1; a+= (a++)-(++a) * (a--); Output a value of-7 ");

SYSTEM.OUT.PRINTLN ("int a=1; A=a/3==0?a++:++a; correct. Output a value of 1 ");

SYSTEM.OUT.PRINTLN ("int a=1; A+=a/3==0?a++:++a; correct. Output a value of 2 ");

SYSTEM.OUT.PRINTLN ("int a=1; A=a/3!=0?a++:++a; correct. Output a value of 2 ");

SYSTEM.OUT.PRINTLN ("int a=1; a+=! (a/3==0)? A++:++a; right. Output a value of 2 ");

System.out

. println ("float f = 2.0*3.0; error. The return type of an assignment expression is the type of the variable being assigned, and the assignment expression defaults to a double type and cannot be assigned to a variable of type float ");

SYSTEM.OUT.PRINTLN ("int a = 1; A = A++%++a; correct. Output a value of 1 ");

SYSTEM.OUT.PRINTLN ("int a = 1; A%= a++%++a, right. Output a value of 0 ");

SYSTEM.OUT.PRINTLN ("int a = 1; A = ((a+a++) *5+ ++a) *a; Output a value of 39 ");

SYSTEM.OUT.PRINTLN ("int a =" + "\" "+" 1 "+" \ "" + "+2;"

+ "error. An assignment expression returns a string type and cannot be assigned a variable a "of type int");

SYSTEM.OUT.PRINTLN ("int a = (1+2) +" + "\" "+" 3 "+" \ ";"

+ "error. An assignment expression returns a string type and cannot be assigned a variable a "of type int");

System.out.println ("String a = (1+2) +" + "\" "+" 3 "+" \ ";"

+ "correct. Output a value of 123 ");

System.out.println ("String a =" + "\" "+" 3 "+" \ "+2+" + "\" "+" 1 "

+ "\"; "+" is correct. Output a value of 321 ");

System.out.println ("String a =" + "\" "+" 3 "+" \ "+2+1;"

+ "correct. Output a value of 321 ");

System.out.println ("String a = (" + "\" "+" 3 "+" \ "+2) +1;"

+ "correct. Output a value of 321 ");

SYSTEM.OUT.PRINTLN ("int a = 5/((3+1)%5==0?7:2) *6/3; Output a value of 4 ");

}

}

11 title: An integer, which plus 100 is a complete square number, plus 168 is a complete square number, what is the number?

public class P2 {

public static void Main (string[] args) {

for (int a=0;a<100000;a++) {

int b=a+100;

int c=a+268;

int x= (int) math.sqrt (b);

int y= (int) math.sqrt (c);

if (x*x==b&&y*y==c) {

System.out.println (a);

}

}

}

}

Output Result:

21st

261

1581

?

Java Fundamentals 02

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.