Chapter 2 basic Java syntax

 

     catalogue

1, Keywords

1. Keyword overview

  • A word given a specific meaning by the java language.

2. Keyword features

  • The letters that make up the keyword are all lowercase.
  • keyword

 

 

3. Keyword precautions

  • goto and const exist as reserved words and are not currently used.
  • Integration tools like IDEA have special color marks for keywords, which is very intuitive.

2, Identifier

1. Identifier overview

  • Is the character sequence used when naming classes, interfaces, methods, variables, etc.

2. Composition rules

  • English upper and lower case letters
  • Numeric character
  • $and_

3. Precautions

  • Cannot start with a number
  • Case sensitive
  • Cannot be a keyword in Java

4. Common naming conventions in Java

4.1 a package is actually a folder. Under different folders, you can create a package with the same name. The package name is composed of lowercase letters.

  • Single level directory
    • Example: kyrieli
  • Multilevel directory
    • Example: com.shuxue.kyrieli

4.2 class or interface name

  • One word
    • If it is composed of one word, the first letter is capitalized    
    • Example: Cat
  • Multiple words
    • If it is composed of multiple words, each word is capitalized
    • Example: HelloWorld

4.3 method or variable name

  • One word
    • If it is composed of one word, all characters are lowercase
    • Example: speak
  • Multiple words
    • If it is composed of multiple words, the letters of the first word are all lowercase, and the first letter is capitalized from the second word
    • Example: speakChinese

4.4 constants

  • One word
    • If it is composed of one word, each letter is capitalized
    • Example: SLEEP
  • Multiple words
    • If it is composed of multiple words, each word is capitalized, and the words are underlined
    • Example: STUDENT_SLEEP

3, Notes

1. General notes

  • Text used to explain the program

2. Annotation classification format in Java

  • Single-Line Comments
    • Format: / / annotation text
  • multiline comment
    • Format: / * note text*/
  • Documentation Comments
    • Format: / * * note text*/

3. Suggestions and requirements for notes

  • Annotation is a good programming habit that a programmer must have.
  • Beginners can form the habit of writing programs: write comments before writing code.
  • Sort out your thoughts through comments and reflect them in code.
  • Because code is just a form of thought.

4. Examples of notes

  • Add notes to the HelloWorld case and write it out
  • Requirements: write a program and output HelloWorld on the console
  • analysis:
    • To write a java program, first define the class.
    • In order for a program to be called by the jvm, the main method must be defined.
    • If a program wants to output results, it must use output statements.
  • realization:
    • The class keyword is used to define the class, followed by the class name.
    • The basic format of the main method.
    • Basic format of output statement.
package com.shujia.java.sx.day02;
/*
        Requirement: add comments on the HelloWorld code and output the HelloWorld on the console
        analysis:
            1,To write a Java program, you must define a class
            2,Outputting the data on the console shows that our program can run independently, and the main method must be defined
            3,To output data to the console, you must call the Java output statement
        realization:
            1,Java Language provides a keyword, the class keyword, followed by the name is our class name
            2,main The format of the method is fixed
                    public static void main(String[] args) {
                    }
            3,Java The output statement in is fixed
            System.out.println("HelloWorld");
            "HelloWorld"This content can be changed
 */
//This is our HelloWorld case
class HelloWorld {
    /*
       In order that the program can run independently, the main method is defined
       main Method is the entrance to the program
       Automatically invoked by the JVM
     */
    public static void main(String[] args) {
        //To display the data on the console, call the Java output statement
        System.out.println("Hello World");
        System.out.println("floret");
        System.out.println("Xiao Ming");
        System.out.println("Xiao Hong");
    }
}

5. Benefits of notes

  • Explain the program and improve the readability of the program.
  • Can help us troubleshoot.

4, Constants, hexadecimal and hexadecimal conversions

1. Constant overview

  • Its value cannot be changed during program execution

2. Constant classification in Java

  • literal constant
  • Custom constant
  • string constant      Contents enclosed in double quotation marks
  • integer constant          All integers
    • 12,23
  • Decimal constant          All decimals
    • 12.34,56.78
  • character constants          Content enclosed in single quotation marks
    • 'a','A','0'
  • Boolean Literals          It is unique, only true and false
  • Null constant          null

3. Hexadecimal

  • Java provides four representations for integer constants
    • Binary
    • octal number system
    • decimal system
    • hexadecimal
  • Binary overview
    • Hexadecimal: it is the carry system, which is a carry method stipulated by people. For any kind of base system -- X base system, it means that the number operation at a certain position is carried into one digit every X. Binary is every two into one, octal is every eight into one, decimal is every decimal one, hexadecimal is every sixteen into one.
  • Data composition of different hexadecimals
    • Binary
      • It consists of 0,1. Start with 0b.
    • octal number system
      • It consists of 0,1,... 7. Starts with 0.
    • decimal system
      • It consists of 0,1,... 9. Integers are decimal by default.
    • hexadecimal
      • From 0,1,... 9, a, B, C, D, e, f (case sensitive), starting with 0x.
  • Binary conversion

  • give an example
    • Convert 52 to binary, octal, hexadecimal

  •   Fast conversion between decimal and binary
    • 8421 yards

  4. Signed data representation

  • In the computer, there are three representations of signed numbers: original code, inverse code and complement code. All data operations are performed using complements.
  • Original code
    • It is the binary fixed-point representation, that is, the highest bit is the sign bit, "0" represents positive, "1" represents negative, and the other bits represent the size of the value.
  • Inverse code
    • The inverse code of a positive number is the same as its original code; The inverse code of a negative number reverses its original code bit by bit, except for the sign bit.
  • Complement
    • The complement of a positive number is the same as its original code; The complement of a negative number is to add 1 to the last bit of its inverse code.

5, Variable

1. Variable overview

  • In the process of program execution, the amount whose value can change within a certain range.
  • Understanding: like the unknowns in mathematics.

2. Variable definition format

  • Data type variable name = initialization value.
  • Note: the format is fixed. Remember the format to keep unchanged.

6, Data type and data conversion

  • Java language is a strongly typed language. It defines specific data types for each data and allocates different sizes of memory space in memory

 

  • Write a case to demonstrate the variable definition of each different data type  
  • Precautions for using variables:
    • Scope
      • The scope of the variable is defined by the level of braces in which the variable is defined and the scope of which braces. Two variables with the same name cannot be defined in the same scope.
    • initialize value
      • No initialization value can be used directly.
    • It is recommended to define only one variable on one line
      • Multiple can be defined, but not recommended
  • +Is an operator. We should be able to understand it and add data
  • boolean type cannot be converted to other data types
  • Default conversion
    • byte,short,char—int—long—float—double
    • Byte, short and char complement each other. They participate in the operation and are first converted to int type.
  • Force conversion
    • Target type variable name = (target type) (converted data).
    • Forced rotation is not recommended because accuracy may be lost
  • matters needing attention:
    • When adding variables, we will first look at the problem of type (if there is promotion, we will promote). Finally, when assigning results, we will also consider the problem of type.
    • For constant addition, first perform the addition operation, and then check whether the result is in the range of the received data type. If the class is not in the range, it will report an error, and if it is in the range, it will not report an error
    • Interview questions
      • byte b1=3,b2=4,b; b=b1+b2; b=3+4; Which sentence failed to compile? Why?
public class DataTypeDemo5 {
    public static void main(String[] args) {
        //Three byte type variables are defined, namely B1, B2 and B
        //Where b1 and b2 are given values, and b is not given values
        byte b1=3,b2=4,b;
//        b = b1 + b2;  // There is type promotion here, so there is a problem
        b = 3 + 4;//Constant, first calculate the result, and then check whether the result is in the range class of the received data type. If it is, no error will be reported

    }
}
  •   give an example
    • Please write the following program results                                                                          
      • System.out.println("hello"+'a'+1);                          helloa1     
      • System.out.println('a'+1+"hello");                          a1hello
      • System.out.println("5+5="+5+5);                           5+5=55
      • System.out.println(5+5+"=5+5");                          10=5+5

7, Operator

1. Arithmetic operator

2. Assignment operator

3. Comparison operator

4. Logical operators

5. Bitwise operator

6. Ternary operator

1. Arithmetic operator

 

  •  + Several functions of:
    • addition
    • Positive number
    • String connector
  • There is a problem to pay attention to when dividing:
    • Divide integers to get only integers
    • To get decimals, you can * 1.0
  • /The difference between% and%: / is seeking quotient and% is seeking remainder.
  • ++And -- Applications
    • The effect is the same when used alone
    • Participate in the operation, and the effect is different before and after the operand

2. Assignment operator

  • Symbol:
    • = , +=, -=, *=, /=, %=
    • =It is the basic assignment operator, and the others are the extended assignment operators

3. Relational operators

 

  •   Note 1: the results of comparison operators are boolean, that is, they are either true or false.
  •   Note 2: the comparison operator "= =" cannot be written as "=".

 

3. Logical operators

 

  • Logical operators are used to connect Boolean expressions. They cannot be written as 3 < x < 6 in Java, but should be written as x > 3 & x < 6.
  • The difference between "&" and "& &":
    • Single & time, whether the left is true or false, the right is calculated;
    • When double &, if the left is true, the right participates in the operation. If the left is false, the right does not participate in the operation.      The difference between "|" and "|" is the same. When double or, the left is true and the right does not participate in the operation.
  • The difference between XOR (^) and or (|) is that when both left and right are true, the result is false.

4. Bitwise operator

 

 

5. Ternary operator

  • format
    • (relational expression)? Expression 1: expression 2;
    • If the condition is true, the result of the operation is expression 1;
    • If the condition is false, the result of the operation is expression 2;
  • Example
    • Gets the largest of the two numbers.
    • int x=3,y=4,z;
    • z = (x>y)? x:y;// The Z variable stores the large number of two numbers.

8, Statement

1. Process control statement

  • In the process of a program execution, the execution order of each statement has a direct impact on the result of the program. In other words, the process of the program has a direct impact on the running results. Therefore, we must know the execution process of each statement. Moreover, many times we need to control the execution order of statements to realize the functions we want to complete.
  • Process control statement classification
    • Sequential structure
    • Select structure
    • Cyclic structure

  1.1 sequence structure

  • Sequence structure overview
    • It is the simplest and most basic process control in the program. There is no specific syntax structure. It is executed in sequence according to the sequence of codes. Most codes in the program are executed in this way.
    • In general: those written in the front shall be executed first, and those written in the back shall be executed later.
  • Sequence structure diagram

 

  1.2 selection of structure

  • Select structure

     

    • Also known as branch structure.
    • The selection structure has specific syntax rules, and the code needs to perform specific logical operations for judgment. There are two results of logical operations, so the selection is generated, and different codes are executed according to different choices.
    • The Java language provides two choice structure statements
      • if statement
      • switch Statements
  • Select structure (if statement)
    • The if statement has three formats
    • The first format of if statement:
      • If (relational expression){              Statement body     }
    • Execution process
      • First, judge the relational expression to see whether the result is true or false
      • If true, execute the statement body
      • If false, the statement body is not executed  
      • if figure I

     

 

 

  •   matters needing attention
    • Whether the relational expression is simple or complex, the result must be of boolean type
    • If the statement body controlled by the if statement is one statement, the braces can be omitted; if it is multiple statements, they cannot be omitted. It is recommended never to omit.
    • Generally speaking: there is no semicolon if there is a left brace, and there is no left brace if there is a semicolon.
  • The second format of if statement:
    • If (relational expression){              Statement body 1;     } else {              Statement body 2;     }
  • Execution process
    • First, judge the relational expression to see whether the result is true or false.
    • If true, execute statement body 1.
    • If false, execute statement body 2.
    • if figure II

 

  •   We explained the ternary operator earlier. It also gives two results after comparison. Therefore, this situation is very similar to the second format of if statement. In some cases, they should be convertible to each other.
  • The second format of if statement and ternary operator
    • if statements can be used to improve the operation of ternary operators, and vice versa
    • When is it not established?
      • It is not true when the statement body controlled by the if statement is an output statement. Because the ternary operator is an operator, a result must be returned.     The output statement cannot be used as a return result.
  • The third format of if statement:
    • if (relational expression 1){              Statement body 1;     } else   if (relational expression 2){              Statement body 2;     }     …      else {              Statement body n+1;     }
  • Execution process
    • First, judge the relationship expression 1 to see whether the result is true or false
    • If true, execute statement body 1
    • If it is false, continue to judge relationship expression 2 to see whether the result is true or false
    • If true, execute the statement body 2. If false, continue to judge the relational expression... To see whether the result is true or false
    • If no relational expression is true, the statement body n+1 is executed.
    • if Figure 3

 

  •   Examples of if statements
    • Gets the maximum of the three data
package com.shujia.java.sx.day04;
/*
        Determine the maximum of the three numbers
            if Nesting of statements
 */
import java.util.Scanner;
public class IfDemo7 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the first number a: ");
        int a = sc.nextInt();
        System.out.println("Please enter the second number b: ");
        int b = sc.nextInt();
        System.out.println("Please enter the third number c: ");
        int c = sc.nextInt();
        //Don't consider equality first
        if (a > b) {
            if (a > c) {
                System.out.println("The maximum is a: " + a);
            } else {
                System.out.println("The maximum is c: " + c);
            }
        } else if (b > c) {
            System.out.println("The maximum is b: " + b);
        } else {
            System.out.println("The maximum is c: " + c);
        }
    }
}
  •   Select structure (switch statement)
  • switch statement format:
    • Switch (expression){            case value 1:              Statement body 1;              break;              case value 2:              Statement body 2;              break;             …              default:                   Statement body n+1;              break;     }
  • Format interpretation
    • Switch indicates that this is a switch statement
      • Value of expression: byte,short,int,char
      • After JDK5, it can be enumeration
      • After JDK7, it can be String
    • case is followed by the value to compare with the expression
    • The statement body part can be one or more statements
    • break means interrupt and end. You can end the switch statement
    • The default statement indicates that when all conditions do not match, the content of the location will be executed, which is similar to the else of the if statement.
  • Execution process
    • First, calculate the value of the expression
    • Secondly, compare with case in turn. Once there is a corresponding value, the corresponding statement will be executed, and the break will end in the process of execution.
    • Finally, if all case s do not match the value of the expression, the body of the default statement is executed, and the program ends.
    • switch statement diagram

  •   matters needing attention
    • A case can only be followed by a constant, not a variable, and the values behind multiple cases cannot be the same
    • Can default be omitted?
      • Can be omitted. Generally not recommended. Unless the judgment value is fixed. (single choice question)
      • Can break be omitted?
        • Can be omitted, generally not recommended. Otherwise, the result may not be what you want
      • Must the position of default be at the end?
        • Can appear anywhere in a switch statement.
      • End condition of switch statement
        • break encountered
        • Execute to the end of the program
  • switch statement exercise
    • Use the switch statement to input the month and output the corresponding season
package com.shujia.java.sx.day04;
/*
        Use the switch statement to input the month and output the corresponding season
        Spring: March, April and may summer: June, July and August autumn: September, October and November winter: December, January and February
 */
import java.util.Scanner;
public class SwitchTest1 {
    public static void main(String[] args) {
        //Create Scanner object
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the month you want to know the season: (1)-12): ");
        int month = sc.nextInt();
        switch (month){
            case 1:
                System.out.println("winter");
                break;
            case 2:
                System.out.println("winter");
                break;
            case 3:
                System.out.println("spring");
                break;
            case 4:
                System.out.println("spring");
                break;
            case 5:
                System.out.println("spring");
                break;
            case 6:
                System.out.println("summer");
                break;
            case 7:
                System.out.println("summer");
                break;
            case 8:
                System.out.println("summer");
                break;
            case 9:
                System.out.println("autumn");
                break;
            case 10:
                System.out.println("autumn");
                break;
            case 11:
                System.out.println("autumn");
                break;
            case 12:
                System.out.println("winter");
                break;
            default:
                System.out.println("The month entered is incorrect, please re-enter!");
                break;
        }
    }
}
  •   be careful:
    • When making judgment, we have two choices, if statement and switch statement. Then, how do we choose which statement to use?
    • if statement usage scenario
      • Judgment that the result is boolean type
      • Judgment for a range
      • Judgment on several constant values
    • Use scenario of switch statement:
      • Judgment on several constant values

1.3 circulation structure

  • A loop statement can repeatedly execute a piece of code when the loop conditions are met. This repeatedly executed code is called a loop body statement. When the loop body is repeatedly executed, the loop judgment condition needs to be modified to false at an appropriate time to end the loop, otherwise the loop will be executed all the time and form a dead loop.
  • Composition of circular statements
    • Initialization statement:
      • One or more statements that complete some initialization operations.
    • Judgment condition statement:
      • This is a boolean expression that determines whether to execute the loop body.
    • Control condition statement:
      • This part is executed after the end of one cycle and before the execution of the next cycle judgment condition. It is used to control the variables in the cycle condition to make the cycle end at an appropriate time.
  • Loop structure (for loop statement)
    • for loop statement format:
      • For (initialization statement; judgment condition statement; control condition statement){           Loop body statement;     }
      • Execution process
        • A: Execute initialization statement
        • B: Execute the judgment condition statement to see whether the result is true or false
          • If false, the loop ends.
          • If true, continue.
        • C: Execute loop body statement
        • D: Execute control condition statement
        • E: Go back to B and continue
    • for loop structure statement diagram

 

  •   matters needing attention
    • The result of the judgment condition statement is a boolean type.
    • If the loop statement is a statement, the braces can be omitted; if it is multiple statements, the braces cannot be omitted. It is recommended never to omit.
    • Generally speaking: there is no semicolon if there is a left brace, and there is no left brace if there is a semicolon.
  • Loop structure (while loop statement)
    • while loop statement format:
      • Basic format     While (judgment condition statement){           Loop body statement;    }
      • Extended format     Initialization statement;     While (judgment condition statement){           Loop body statement;           Control condition statement;     }
    • while loop statement format diagram

 

  • Loop structure (difference between for loop and while loop)
    • for loop statements and while loop statements can be converted equivalently, but there are still some differences
      • Usage difference: the variable controlled by the control condition statement can no longer be accessed after the end of the for loop, and can continue to be used after the end of the while loop. If you want to continue to use it, use while, otherwise it is recommended to use for. The reason is that the variable disappears from memory after the end of the for loop, which can improve the efficiency of memory use.
      • Scene difference:
        • The for loop is suitable for operating against a range judgment
        • The while loop is suitable for operations with unclear judgment times
    • Examples of while loop statements
      • The highest mountain in China is Mount Everest: 8848m. Now I have a large enough paper with a thickness of 0.01m. How many times can I fold it to ensure that the thickness is not lower than the height of Mount Everest?
package com.shujia.java.sx.day04;
/*
        The highest mountain in China is Mount Everest: 8848m. Now I have a large enough paper with a thickness of 0.01m.
        How many times can I fold it to ensure that the thickness is not lower than the height of Mount Everest?
        4*100/2*100 = 2
        analysis:
            1,The height is 8848, that is, the maximum value is 884800. There needs to be a variable to receive this value
            2,The initial value is 1
            3,Each fold is twice the thickness of the previous one * 2
            4,How many times does it stack? But we know that as of the conditional thickness > = 884800, jump out of the loop
            5,Because we don't know how many times to fold, we don't choose the for loop and choose the while loop
 */
public class WhileTest2 {
    public static void main(String[] args) {
        int mountainHigh = 884800;
        int count = 0;
        int thickness = 1;
        while (thickness<884800){
            thickness*=2;
            count++;
            System.out.println("This is the second:"+count+"Secondary folding, thickness:"+thickness);
        }
        System.out.println("---------------------------------------");
        System.out.println("Total folded:"+count+"second");
    }
}
  • Loop structure (do...while loop statement)
    • do... while loop statement format:
      • Basic format     do {           Loop body statement;    } While (judgment condition statement);
      • Extended format     Initialization statement;     do {           Loop body statement;           Control condition statement;     } While (judgment condition statement);
    • do...while loop statement diagram:

  •   Differences and precautions of cycle structure
    • In fact, the three circular statements can perform the same function, that is, they can be converted equivalently, but there are still others:
      • The do... while loop executes the loop body at least once.
      • for loop and while loop will execute the loop body only when the condition is true
    • matters needing attention:
      • The writer gives priority to the for loop, then the while loop, and finally the do... While loop.
      • The following code is an endless loop while(true){} for(;) {}
  • Loop structure (nested use of loops)
    • Requirement: Please output a star (*) pattern with 4 rows and 5 columns.
package com.shujia.java.sx.day05;
import java.util.Scanner;
/*
        *  *  *  *  *
        *  *  *  *  *
        *  *  *  *  *
        *  *  *  *  *
        Methods: first look at the number of spaces and stars in the graph from left to right, and observe the change of their number, so as to use the double for loop to solve the problem.
 */
public class XingXingDemo {
    public static void main(String[] args) {
        //Four rows and five columns of stars
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= 5; j++) {
                System.out.print("*" + "\t");
            }
            System.out.println();
        }
        System.out.println("--------------Lower left triangle---------------");
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*" + "\t");
            }
            System.out.println();
        }
    }
}
  •   Jump control statement
    • As we have said before, goto in Java is a reserved word and cannot be used at present. Although there is no goto statement to enhance the security of the program, it also brings a lot of inconvenience. For example, I want to end when a loop knows a certain step, and I can't do it now. In order to make up for this defect, Java provides break, continue and return to realize the jump and interrupt of control statements.
    • break interrupt
    • Continue continue
    • Return return  
  • Jump control statement (break)
    • Usage scenario of break:
      • In the select structure switch statement
      • In a loop statement
      • It is meaningless to leave the existence of the usage scenario
    • Function of break:
      • Jump out of single layer cycle
      • Jump out of multi-layer loop
        • Tabbed jump out
        • Format: tag name: Circular statement
        • Tag names should conform to Java Naming Rules
  • Jump control statement (continue)
    • Use scenario of continue:
      • In a loop statement
      • It is meaningless to leave the existence of the usage scenario
    • Function of continue:
      • Compare break with single-layer cycle, and then summarize the differences between the two
        • break   Exit current loop
        • continue   Exit this cycle
        • It can also be used with labels  
  • Jump control statement (return)
    • The return keyword is not used to jump out of the loop body. A more common function is to end a method, that is, exit a method. Jump to the method called by the upper layer. The use of this method will be explained in detail.
    • Demonstration case:
      • Ending the loop actually ends the main method
    • give an example
package com.shujia.java.sx.day04;
/*
        Select structure: switch
        Statement format: 20
             switch((expression){
	            case Value 1:
			        Statement body 1;
			        break;
		        case Value 2:
			        Statement body 2;
			        break;
		        ...
		        default: 
			        Statement body n+1;
			        break;
             }
         switch It represents a switch statement. What types can the result value of the expression be?
         Value of expression: byte,short,int,char
         JDK5 You can then pass in the enumeration
         JDK7 It can be followed by String type (interview question)

         case Followed by the value to be compared with the expression. If it matches the corresponding case value,
                Then execute the corresponding statement body, which can be one statement or multiple statements.
         break It means interrupt and end. Its function is to end the switch statement
         default This statement indicates that all situations do not match, and the statement body content is executed,
                Similar to the last else of if else
        Execution process:
            1,First, evaluate the value of the expression
            2,Compare the calculated value with the value behind the case from top to bottom. If a matching value is found,
              Start executing the corresponding statement body
            3,When the corresponding statement body is executed, the next break statement will be executed. Executing the break statement represents
              switch End of statement selection structure (jump out of switch selection)
            4,When the corresponding value of case is searched from top to bottom, if no matching value is found, the default will be executed
                And then jump out of the loop
            matters needing attention:
                1,case Label values cannot have duplicate values.
                2,break The label can be omitted, but it is generally not recommended to omit, otherwise the result may not be what you want
                    When all break s are omitted, the value of the expression is matched with the value of case, from the matched one
                    case From the beginning of the statement body to the end or to a break
                3,default The position where the statement appears. Generally, we put it at the end to represent the statement body that is executed only when all case s do not match
                    But even if you put it first and don't write break, some case statements that don't write break will be executed.

                    Just remember that the default statement body will be executed only when all case s do not match.

                4,switch What are the conditions for the end of a statement?
                    1,Encountered a break
                    2,Program execution to end
 */
import java.util.Scanner;
public class SwitchDemo1 {
    public static void main(String[] args) {
        //Use the keyboard to enter how much money you have?
        Scanner sc = new Scanner(System.in);
        //French fries 10, hamburger 19, taco youth 38, coke 8, spicy wings 12
        //Suppose I bring just enough money to buy one of them each time
        System.out.println("How much money do you have with you");
        int money = sc.nextInt();
        switch (money){
            default:
                System.out.println("Sorry, you don't have enough money. Come back with enough money next time!");
                break;
            case 10:
                System.out.println("Buy French fries, welcome next time!");
            case 19:
                System.out.println("Buy hamburgers, welcome next time!");
            case 38:
                System.out.println("Buy taco youth, welcome to visit next time!");
            case 8:
                System.out.println("Buy Coke, welcome next time!");
            case 12:
                System.out.println("Buy spicy wings, welcome to come next time!");
        }
    }
}

Tags: Java

Posted on Sun, 03 Oct 2021 20:18:59 -0400 by scheols