Home  >  Article  >  Java  >  java process control

java process control

巴扎黑
巴扎黑Original
2017-06-23 16:26:11957browse

Determine whether a certain year is a leap year

package com;

import java.util.Scanner;

public class Msj {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Please enter a year:"); //To The console outputs a prompt message

long year;

try {

year = scan.nextLong();

if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { // It is a leap year

System.out.print(year + "It is a leap year!");

} else { // Not a leap year

System.out.print(year + "Not a leap year!");

}

} catch (Exception e) {

System.out.println("The year you entered is not a valid year!");

}

}

}

Result:

Verify the legitimacy of login information

package com;

import java.util.Scanner;

public class Msj {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);//Create scanner
System.out.println("Please enter the login user Name: ");
String username = scan.nextLine();//Receive the user's login name
System.out.println("Please enter the login password: ");
String password = scan. nextLine();//Receive the user’s login password
if (!username.equals("mr")) {// Determine the legality of the username
System.out.println("The username is illegal. ");
    } else if (!password.equals("mrsoft")) {// Determine the validity of the password
        System.out.println("The login password is incorrect.");
      } else { // If the above two conditions are judged, the login verification will be passed by default.
          System.out.println("Congratulations, your login information has passed the verification.");
                                                                                                   
##Result

Assign department to new employee

package com;

import java.util.Scanner;

public class Msj {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the new employee’s name Name: ");
String name = scan.nextLine();//Receive employee name
System.out.println("Please enter the programming language for the new employee:");
String language = scan.nextLine();//Receive the programming language for employee application
//Determine the department to which the employee is assigned based on the programming language
switch (language.hashCode()) {
case 3254818://java's ha Hash code
              case 2301506:// Java hash code
          case 2269730:// JAVA hash code
          System.out.println("Employee"+name+" is assigned to the Java program development department . ");
break;
case 3104:// c# hash code
case 2112:// C# hash code
System.out.println("Employee"+name+" Assigned to the C# project maintenance group. "); case 9745901: // ASP.NET hash code
System.out.println("Employee "+name+" is assigned to the Asp.net program testing department.");
break;
default:
              System.out.println("This company does not need program developers in " + language + " language. ");
}
}
}



Use switch statement to calculate discount based on consumption amount

package com;

import java.util.Scanner;

public class Msj {
public static void main(String[] args) {
float money = 1170; // Amount
String rebate = ""; // Discount
if (money > 200) {
int grade = (int) money / 200; // Grade
switch (grade) { // Calculate discount ratio based on grade
case 1:
rebate = "Nine Five Fold ";
Break;
Case 2:
Rebate =" 10 % off ";
Break;
Case 3:
Rebate =" 850 % ";
Break ;
                  case 4:                                 rebate = "83% off"; rebate = "20% off";
                break;
                    case 6:
Rebate = "70 %";
Break;
Case 7:
Rebate = "Seven Five Fum";
Break 30% off";
                                                                                                                                              break; case 10:
rebate = "35% off";
                                                                                                                                                  using       using       using           using       using using       through’s ’ through ’ s ’ through ’ s ’ through ‐ to ‐ ′‐‐‐‐‐ and } to "Your accumulated consumption amount is: " + money);// Output consumption amount
System.out.println("You will enjoy " + rebate + " discount! "); // Output discount ratio
}
}

Result:




Determine the season of the user input month

package com;

import java.util.Scanner;

public class Msj {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); // Create a scanner
// Prompt the user to enter the month
System.out.println("Please enter a month, I can tell you which season it belongs to.");
int month = scan.nextInt(); // Receive user input
switch (month) { // Determine which season the month belongs to
            case 12:
                        case 1:  
                case 2:
                                            ’         ’s ’ s ’         ‐ ‐ ‐ ‐‐‐ case 12: ​ ​                                                                                                                      ;
case 3:
case 4:
case 5:
System.out.print("The month you entered belongs to spring");
break;
case 6:
          case 7:
                                                                                                                  use using   using       using         use using ’ s ’ through     through ’s ’ s ‐   ‐   ‐ and                                                                                case 10:
case 11:
          System.out.print("The month you entered belongs to autumn");
              break;
                                    System.out.print(""                                                                                         Month? ");
}
}
}

Result



##Use the while loop statement and the increment operator to loop Traverse the array

package com;

import java.util.Scanner;

public class Msj {

public static void main(String[] args) {

// Create a bird array

String [] Aves = New String [] {"Egret", "Yellow 鹂", "Parrot", "Crow", "Magpie",

"Bird" ", "Lark" };

int index = 0;//Create index variable

System.out.println("There are many birds in my garden, including: ");

while ( index < aves.length) {// Traverse the array
        System.out.println(aves[index++]);//Increase the index value
                                                                            ’ ’s ’ s ’ s ’ ‐ ‐ ‐ ‐ ‐ to #Result:




Use for loop to output Yang Hui triangle

package com;

import java.util.Scanner;

public class Msj {
public static void main(String[] args) {
int triangle[][]=new int[10][];//Create a two-dimensional array
// Traverse the first level of the two-dimensional array
        for (int i = 0; i < triangle.length; i++) {
          triangle[i]=new int[i+1];// Initialize the second level The size of the array
// Traversing the second layer of the array
For (int j = 0; j & lt; = i; j ++) {
// Assign the array elements on both sides to 1
if(i==0||j==0||j==i){
                                                    triangle[i][j]=1; [i][j]=triangle[i-1][j]+triangle[i-1][j-1];
              }
          System.out.print(triangle[i][j]+ “\t”);



##Use nested loops to output the multiplication table on the console

package com;

import java.util.Scanner;

public class Msj {

public static void main(String[] args) { for(int i=1;i<=9;i++){// The loop control variable traverses from 1 to 9

            for(int j=1;j<=i;j++){// The second-level loop control variable is equal to the first-level maximum index

                                                                                                                                                                  use using         use using ’ using ’ s ’ using ’ s using using ’ s using ’ s through ’ s ’       ‐ ‐ ‐ ‐ ‐ ‐ ‐ 1 out.print(j+"*"+i+"="+i*j+"\t");

                                                       

                  System.out.println();// Line break in the outer loop

                                   ## }

}

Result:




Use a while loop to calculate 1+1/2!+1/3!…1/20!

package com;

import java.math.BigDecimal;
import java.util.Scanner;

public class Msj {

public static void main( String[] args) {

BigDecimal sum = new BigDecimal(0.0); // and BigDecimal factorial = new BigDecimal(1.0); // Calculation result of factorial term

int i = 1; // Loop increment

while (i <= 20) {

sum = sum.add(factorial); // Accumulate the sum of factorials

++i; // Add 1 to i

factorial = factorial.multiply(new BigDecimal(1.0 / i)); // Calculate the factorial term

} }
System.out.println("1+1/2!+1/3!···1/ The calculation result of 20! is equal to: \n" + sum); // Output the calculation result

}

}

Result:




Use a for loop to output a hollow diamond

package com;

import java.math.BigDecimal;
import java.util.Scanner;

public class Msj {
public static void main(String[] args) {
printHollowRhombus(6);
}
public static void printHollowRhombus(int size) {
if ( size % 2 == 0) {
                                                                        ## j = size / 2 + 1; j > i + 1; j--) {
              System.out.print(" ");// Output the blank space in the upper left corner
        }
            for ( int j = 0; j < 2 * i + 1; j++) {
                      if (j == 0 || j == 2 * i) {
                        System.out.print("* "); // Output the edge of the upper half of the rhombus
            } else {
              System.out.print(" "); // Output the upper half of the rhombus hollow
                                        using         using using ’     using using     through through out through out through ’'s' out through ‐ through ‐ to ‐‐ ‐‐‐‐‐ and ‐ to System.out. Println (""); // Change
}
for (int i = size /2+1; i & lt; size; i ++) {
for (int j = 0; j & lt; i - size/ 2; j ++) {
System.out.print (""); // Output the lower left corner of the diamond -shaped left corner
}
for (int j = 0; j & lt; 2 * size - 1- 0 Lower half edge
                                                                                                                                                              Lower half edge System.out.println("" ); //Newline
}
}
}

Result:



##Termination loop body

package com;

import java.math.BigDecimal;
import java.util.Scanner;

public class Msj {
public static void main(String[] args) {
System.out.println("\n-------------Interrupt single-level loop Example-------------");
// Create an array
String[] array = new String[] { "Egret", "Red-crowned Crane", "Oriole", "Parrot", "Crow", "Magpie",
"Eagle", "Cuckoo", "Eagle", "Graybird", "Eagle", "Lark" };
System.out .println("Before you find the first eagle, tell me what birds there are.");
for (String string : array) { // foreach traverses the array
if (string.equals("Eagle ")) // If an eagle is encountered
                                                                                                                                                                                    Break
System.out.println("\n\n-------------Example of interrupting double-layer loop-------------");
// Create the results of the results
int [] [] [] myscores = new int [] [] {{67, 78, 63, 22, 66}, {55, 68, 78, 95, 44}, {95 , 97, 92, 93, 81 } };
System.out.println("Baby’s test scores this time:\nMath\tChinese\tEnglish\tArt\tHistory");
                                                             for (int[] is : myScores) { // Traverse the score table
          for (int i : is) {
            System.out.print(i + "\t"); // Output the scores
if (i < 60) { // If you encounter a failure in the middle, immediately interrupt all output
                System.out.println("\nWait, " + i + "What is the score? Why did this fail? ");
                    break No1;




Loop body filter:

package com;

import java.math.BigDecimal;

import java.util.Scanner ;

public class Msj {

public static void main(String[] args) {               // Create array

            String[] array = new String[] { "Egret", " "Red-crowned crane", "oriole", "parrot", "crow", "magpie",

"Eagle", "cuckoo", "eagle", "gray bird", "eagle", "lark" } ;

System.out.println("There are many birds in my garden, but a few eagles have come recently. Please help me catch them.");

int eagleCount = 0;

for (String string : array) {// foreach traverses the array

if (string.equals("Eagle")) {//If an eagle is encountered
System.out.println("An eagle was found, Has caught in the cage. ");

Eaglecount ++;

Continue; // Interrupt loop
}
System.out.println (" Search bird, found: "+string);/// / Otherwise, output the array element
} }
  System.out.println("A total of caught: " + eagleCount + "Eagle.");
}
}

Result:





The above is the detailed content of java process control. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn