Basic flow of java-day01-microcomputer and program

Source: Internet
Author: User
Tags bitwise logical operators

/*

The cross-platform reason for Java:JVM

JVM: responsible for Execution of Java programs

Jre:java Program Runtime Environment, contains the jvma

JDK: Development Kit that contains the JRE

Why set the installation path of the JDK in the environment variable path : to use bin in any path executable commands under the directory

java source program : first compiled into JVM byte code that can be executed using javac command, then use java instruction execution bytecode file (must be a main method of the bytecode file)

Identifier: Seven letters,0-9,_ $, cannot start with a number, cannot have the same name as a keyword

Keywords: words that are special meanings given by Java

Constant : Fixed amount : string Integer decimal boolean character null

*/

Class Demo{public static void Main (string[] args)//JVM run the portal {System.out.println (66.66); SYSTEM.OUT.PRINTLN (66); System.out.println ("Hello"); System.out.println (TRUE);         System.out.println (' W '); }}import java.util.scanner;class Demo2 {public static void main (string[] args) {System.out.println (66); SYSTEM.OUT.PRINTLN (020); System.out.println (0x55);/* variable: The data stored in the program can change the number of variables to store only one number */scanner sc = new Scanner (System . in); SYSTEM.OUT.PRINTLN ("Input first number"); int a = Sc.nextint ();        System.out.println ("Enter a second number"); int c = Sc.nextint (); System.out.println (a+c); int b;}} Class Demo3 {public static void main (string[] args) {/* Data type: Determines the size of a variable's memory space integer: Byte 1 bytes short 2 bytes int 4 bytes Long 8 bytes  Float type: float 4 double 8 character: Char 2 byte Unicode boolean: Boolean 1 bytes */int A;         Integers are treated by default as int type byte b =60;short s = 3276;long c = 234354345435l; The decimal default is treated as double type float f = 45.6f;double d = 34.45d;char ch = ' good '; char CH2 = ' t '; Boolean boo = True;boolean Boo2 = false;}} Class Demo4 {public static void Main (string[] args) {//the level of the data type has a high or low score: from lowest to highest byte char,short-->int-->long-->float-->doublebyte B = 127;//Automatic type Conversion 00000000 00000000 00000000 00000110 The first three bytes are automatically cut off by the internal 00000110=6//int--->BYTESYSTEM.OUT.P Rintln (b); byte a = 34;int c = 45;float f =34.5f;double d = 56.7;double sum = a+c+f+d;//usually goes from low to high, it automatically goes to byte z = (byte) (b+10 );//high-level to low--typically requires forced type conversion//00000000 00000000 00000000 10001001--->10001001--> 01110111=119syste M.out.println (z); int m = ' a '; System.out.println (m);//asciichar ch = 97;        SYSTEM.OUT.PRINTLN (CH); System.out.println ((char) 5);}}




Class Demo5{public static void Main (string[] args) {//Escape character: Change the meaning of the character behind it by ' \ ' \t//system.out.println ("\" hello\ "\ n \ ' Worl D!\ ' ");//windows system: \ r \ n Unix Linx \n//string and any type of data is connected, and ultimately the string int a = 97; System.out.println ("haha" +a+a);//+ is the connection symbol System.out.println ("haha" + (a+a)); System.out.println ("a=" +a+ "Woeiurio");}} Class Demo6 {public static void main (string[] args) {System.out.println (5/2); System.out.println ( -5%9);//The positive or negative of the result is determined by the dividend int m = 3,n;//m++;//m = M+1;//system.out.println ("m=" +m);//n = m++;//++ in the rear, First, the value of M is assigned to the left variable, and then the value of M is increased by 1,n = ++m;//++ in the front edge, the value of M is increased by 1, and then to the left variable System.out.println ("m=" +m+ "n=" +n); int a = 135; System.out.println (a%10+a/10%10+a/100);//+=-+ *=/+%= = m+=4;//m=m+4;short s = 5;//s = s+3;//compilation is not compiled through s+=3;//, internal do The auto-convert System.out.println ("s=" +s);//relational operator: > >= < <= = =! There are only two possible relationships: true: falseint x = 34,y=56; System.out.println (X>y);  System.out.println (x!=y);//logical operators: && and | | or int yu =67,math = 89; System.out.println (yu>80 && math>/*true && true =truetrue && false = Falsefalse && true = Falsefalse && false = False   */System.out.println (yu>60 | | math>80); /* true | | True =truetrue | | false = TrueFalse | | true = TrueFalse | | False = False */SYSTEM.OUT.PRINTLN (!) (       yu>89)); &&amp: Short Circuit and: if the expression on the left is false, the expression on the right is no longer evaluated & the expression on the right is calculated regardless of whether the left side is true or false/| |: Short-circuiting or: If the expression on the left is real, the expression on the right is no longer evaluated |  Whether it's true or false on the left, the expression on the right is calculated System.out.println (6|2);//ternary operator:? : int c = 5>6?1:2; System.out.println (c);}}




/*1. The most efficient way to figure out 2 times 8 equals a few? 2*82. Swaps the values of two integer variables (no third-party variables required) 3. There are three integers, which gets the maximum value, which is done by the ternary operator.        4: Convert a decimal number to 16 (using the bitwise operator)       */class  demo7{public static void Main (string[] args) {//1. The most efficient way to figure out 2 times 8 equals a few? 2*8           /*   0010  -------*///system.out.println (2<<3);      2. Swap the values of two integer variables (no third-party variables required)  int a = 5,b = 6;  /* A = a+b;//a and B and may overflow  B = A-C;  A = a-B;  A = A^b;  b = a^b;//a^b^b    a = a^b;//a^b^a  //using a third-party variable  int c;  c = A;  A = b;  b = C;  System.out.println ("a=" +a+ ", b=" +b);  3. There are three integers, get the maximum value, complete by the ternary operator.  int x = 123,y = 45,z = 678;  int m = x>y?x:y;  int Max =m>z?m:z;  System.out.println ("max=" +max); *  ///4: Convert a decimal number to 16 (using bitwise operator)  int num =;  int n1 = num&15;    num = num>>>4;  System.out.print (num);  if (n1>9)  System.out.print ((char) (n1-10+ ' a '));}} System.out.println ();


Class Demo8 {public static void main (string[] args) {int a = 4;/*if (a>1) {System.out.println ("Hello world!"); System.out.println ("haha");}    Else{system.out.println ("Heihei");        System.out.println ("Heihei2");}    */if (a<-1) if (a<-5) System.out.println ("Heihei"); else System.out.println ("hehe");//else always corresponds to the IF (a>1) System.out.println ("a>1") that is closest to him and has no corresponding else if (A&GT;2);        System.out.println ("a>2"); if (a>3) System.out.println ("a>3"); Multiple conditional judgment structure if (a>1) System.out.println ("a>1"), else if (a>2) System.out.println ("a>2"), else if (a>3) System.out.println ("a>3"); else System.out.println ("invalid");}} Requirement 1: Varies according to user-defined values. Print the corresponding week. Requirement 2: Prints the season to which the month belongs, based on the month used for the designation. 3,4,5 Spring 6,7,8 Summer 9,10,11 Fall 12, 1, 2 winter class Demo9{public static void Main (string[] args) {int week = 3;if (week==1) Sy Stem.out.println ("Monday"), else if (week==2) System.out.println ("Tuesday"), else if (week==3) System.out.println ("Week 3"); else if (week==4) System.out.println ("Week 4"), else if (week==5) System.out.println ("Week 5"), else if (week==6) System.out.println ("Week 6"), else if (week==7) System.out.println ("Sunday");        ELSESYSTEM.OUT.PRINTLN ("Invalid");//Demand 2: According to the month specified by the user, print the season to which the month belongs. 3,4,5 Spring 6,7,8 Summer 9,10,11 Fall 12, 1, 2 winter int month = 11;if (month>=3 && month<=5) System.out.println ("Spring"); E LSE if (month>=6 && month<=8) System.out.println ("Summer"); else if (month>=9 && month<=11) System.out.println ("Autumn"), else if (month==12 | | month ==1 | | month==2) SYSTEM.OUT.PRINTLN ("Winter"); ElseSystem.out.println ( "Invalid");//other wording if (month<1 | | month>12) SYSTEM.OUT.PRINTLN ("invalid"); else if (month>=3 && month<=5) System.out.println ("Spring"), else if (month>=6 && month<=8) System.out.println ("Summer"); else if (month>=9 && month<=11) System.out.println ("Autumn"), Else System.out.println ("Winter");}} /*switch Statement format: switch (expression) {case value 1: EXECUTE statement; break;case value 2: Execute statement; Break;......default: Execute statement; Break;}*/class demo10{public        static void Main (string[] args) {int a = 6; Curly braces that encounter break or end will onlyStop executing switch (a)//byte short int char jdk1.5 (enum) jdk1.7 string {default:System.out.println ("0"); case 1:system.out.println ("1 Case 2:system.out.println ("2"), Case 3:system.out.println ("3"), Break;case 4:system.out.println ("4");}        Requirement 2: Print the season to which the month belongs according to the user specified month. 3,4,5 Spring 6,7,8 Summer 9,10,11 Fall 12, 1, 2 winter int month = 11;switch (month) {case 3:case 4:case 5:system.out.println ("Spring"); Brea K;case 6:case 7:case 8:system.out.println ("Summer") break;case 9:case 10:case 11:system.out.println ("Autumn"); Break;case 12:            Case 1:case 2:system.out.println ("Winter"); DEFAULT:SYSTEM.OUT.PRINTLN ("invalid"); If the value of an expression is a Boolean type or a number in a range, you can only use the IF structure//if the value of the expression is a definite value, and in the data types of byte short int char, use switch to compare//implement a arithmetic int a = 23        , B = 4; char ch = ' * '; switch (CH) {case ' + ': System.out.println (a+b); Break;case '-': System.out.println (A-B); Break;case ' * ': Sy            Stem.out.println (a*b); Break;case '/': System.out.println (A/b); break; DEFAULT:SYSTEM.OUT.PRINTLN ("invalid"); /* Loop structure: Perform the same or a specific function while (Loop bar) {code that needs to be executed repeatedly----loop body}do{code that needs to be executed repeatedly---loop body}while (loop condition); */class demo11{public static void Main (string[] args) {int i=6;   while (i<=5) {System.out.println ("Hello world!");        i++;}   First judge the condition, if the condition is true, then executes the loop body once, then goes back to judge the condition, if the condition also is true, then executes the loop body once,//so repeatedly, until the condition is false, the loop ends int j = 6;   do {System.out.println ("Hello world!");   j + +;   }while (j<=5); Limit the execution of the loop body once, and then determine whether the condition is true, if the condition is true, and then execute the loop body until the condition is false, the loop ends//do{}while (); The loop body is executed at least once, while () {} The loop does not perform}}/*for at the start of the condition ( initialization expression, loop conditional expression, post-loop action expression) {EXECUTE statement;}*/class Demo12 {public static void main (string[] args) {for (int i=1;i<=5;i++) {Syste M.out.println ("Hello world!");} First executes the expression 1, then the condition, if the condition is true, executes the loop body, then executes the expression 3, then judges the condition, if the condition is true, then executes the loop body//Executes the expression 3 again, then judges the condition, until the condition is false, the end System.out.println (i);        Could not find the symbol int j=1;   while (j<=5) {System.out.println ("Hello world!"); j + +;} System.out.println (j); Variable use scope (scope): Starting from the position where the variable is defined, the variable that is used in the curly brace end//loop of the variable at the end of the loop if it is no longer used, suitable for loop (save memory), otherwise appropriate to select while () or do{}while ();}} Class Demo13 {public static void main (StrinG[] (args) {//Omit expression 1 int i=1;for (; i<=5;i++) {System.out.println ("Hello world!");} Omit expression 2----dead loop for (int i=1;;        i++) {System.out.println ("Hello world!");} Omit expression 3for (int i=1;i<=5;) {System.out.println ("Hello world!"); i++;} Three expressions are omitted for (;;) ---dead Loop {}}}



Here are the assignments and my answers. It's too simple for me to do it!

1. Calculate the value problem for n+ (n-1) + (n-2) +....+3+2+1: Calculates the value of the series n+ (n-1) + (n-2) +....+3+2+1, where the value of n is entered by the user, for example: If the number entered is 8, the value of 8+7+6+5+4+3+2+1 is calculated, The calculated result should be 36import Java.text.decimalformat;import Java.util.scanner;public class Test {public static void main (string[]   args) {Scanner in= new Scanner (system.in);   int i= in.nextint ();   int count=0;   for (int j=i;j>=1;j--) {count+=j; } System.out.println (count);}} 2. Calculate the number of positive integers and problems: Write a program that allows the user to enter a positive integer value, and then calculate the number of the numbers, for example: if the number of inputs is 123, the result is 6import Java.text.decimalformat;import Java.util.scanner;public class Test {public static void main (string[] args) {Scanner in = new Scanner (system.in); int i = i N.nextint (); int result = 0;while (i > 0) {int Shuzi = i% 10;i = i/10;result + = Shuzi;} SYSTEM.OUT.PRINTLN (result);}} 3. Find the factorial of a number for example: 5! 5*4*3*2*1 0!==1import Java.text.decimalformat;import Java.util.scanner;public class Test {public static void main ( String[] args) {Scanner in = new Scanner (system.in); int x = In.nextint (); int result = 1;for (int i = 1; i <= x; i++) {R Esult *= i;} SystEM.OUT.PRINTLN (result);}} 4. Find the value of S=A+AA+AAA+AAAA+AA...A, where a is a number. For example 2+22+222+2222+22222 (a total of 5 numbers are added at this time), several numbers are added by keyboard control. Import Java.text.decimalformat;import Java.util.scanner;public class Test {public static void main (string[] args) {  Scanner in = new Scanner (system.in); int x = In.nextint (); int y = x;int count = In.nextint (); int result = 0;for (int i = 1; I < count; i++) {x = x * ten + y; SYSTEM.OUT.PRINTLN (x); result = result + x;} System.out.println (result + y);}} 5. There is a fractional sequence: 2/1,3/2,5/3,8/5,13/8,21/13 The first 20 items of this sequence and import Java.text.decimalformat;import Java.util.Scanner; public class Test {public static void main (string[] args) {Double x = 2;double y = 1;double result = 0;for (int i = 0; I & Lt 5; i++) {System.out.println (x/y); result = result + X/y;x = x + y;y = XY;} SYSTEM.OUT.PRINTLN (result);}} 6. Display character problems as required: Write a program that receives the number of user input, and then displays all the characters represented from 0 to that number. Ask the user if they would like to continue the same process again 7. Write a program, according to the input of a class student's results, calculate the average grade of the class students, the number of classes require user input, according to the number of people entered, the results of the students to calculate the average score of the class, and display the results, and finally asked whether users OK to end program exit




1: Write a program for the resulting output, which requires an integer that is entered by the user,
to output a pattern that consists of a number
For example:
55555
4444
333

1


1

333
4444
55555
2. Print a solid diamond pattern
   *
  * * *
 *****
*******
 *****
  * *
   *


3. Print a hollow diamond pattern
  question: with * Print a hollow diamond pattern like this:


                   *
                  * *
                 *   *
                *     *
                 *   *
                  * *
    &NB Sp              *
                   
4: Ask for 1!+2!+3!+4!+5!+6!+7!+8!+9!+10!


1,import Java.text.decimalformat;import Java.util.scanner;public class Test {public static void main (string[] args) {     Scanner in= New Scanner (system.in); int x=in.nextint ();    for (int i=x;i>0;i--) {for (int j=i;j>0;j--) {System.out.print (i);     } System.out.println ();     } for (int i=1;i<=x;i++) {for (int j=i;j>=1;j--) {System.out.print (i);     } System.out.println (); }}}2,import Java.text.decimalformat;import Java.util.scanner;public class Test {public static void main (string[] args) { for (int i = 1, i <= 4; i++) {for (int x = i; x <= 4; + +) {System.out.print ("");} for (int j = 1; J <= I * 2-1; j + +) {System.out.print ("*");} System.out.println ();} for (int i = 3, I >= 1; i--) {for (int x=i;x<=3;x++) {System.out.print ("");} for (int j = i * 2-1; J >= 1; j--) {System.out.print ("*");} System.out.println ();}}} 3,import Java.text.decimalformat;import Java.util.scanner;public class Test {public static void main (String[] (args) {int x = 1;int count = 0;for (int i = 1; i <=; i++) {for (int j = 1; J <= I; j + +) {x *= J;} Count + = x;} System.out.println (count);}}



Basic flow of java-day01-microcomputer and program

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.