phrase switch in java - ProgrammerTech
Language:

Search

phrase switch in java

  • Share this:
phrase switch in java

phrase switch in java

The switch clause of conditional probabilities in the Java language, in this lesson we will deal with the switch and it is called the conditional probabilities function. The switch statement is used when we have several possibilities that depend on checking a certain value, for example we have the average and according to the rate are the possibilities. That is, we use it if we want to test the value of a variable with six possibilities, and if this value is equal to any of them, the commands we put in this specific possibility will be executed, and every possibility we put is called a case.

For example, the result is weak if the average is less than 50 and acceptable if it is between 60 and 70 and so on.... Among these possibilities are "weak - acceptable - good - very good - excellent" and all of these possibilities are based on examining the value of one variable which is the rate.

The general form of the switch statement in Java

switch(variable)
    {
     case value:
     statement
         break;
     default:
     statement
}

The word switch means to test the value of the variable in parentheses. And between the parentheses is the variable whose value we want to test, and it allows us to test the following variables (int - byte - short - char).

The word case means status, value means value and statement means command.

The word "default" means "default" and is the same as the idea of ​​the else clause. This statement is executed only if it does not execute any case in the switch statement, so we put it at the end of the possibilities.

And the word break we don't need to put it after default because the program will stop anyway.

example 1

Write a program that asks the user to enter the class number or class number. And if according to the class number, a message will appear to the user with the number of students and the name of the class in that class.

package switch_case;
import java.util.Scanner;
public class Switch_Case {
    public static void main(String[] args) {
        Scanner n=new Scanner(System.in) ;
    int c;
    System.out.println("Enter Number Class");
    c=n.nextInt();
    switch(c)
    {
        case 1:
            System.out.println("class A Student=30");
            break;
        case 2:
            System.out.println("class B Student=35");
            break;
        case 3:
            System.out.println("class C Student=40");
            break;
        default:
            System.out.println("Error");
    }
}}

We assumed that in this school there are three classes, i.e. three classes, which are case1, case2, case3 and we created the variable c. It is the variable that will receive the values ​​from the user's keyboard and we wrote a message for the user to enter the class number. The entered value will be stored in c by input object n and after the user enters the class number it will be stored in c.

After that we check c, we build the switch function to be checked and within the parentheses we put the three possibilities with the data of each class. In the event that the user requests it and then we put a default error message, if the user enters any value other than the previously existing values, which are 1, 2 and 3, he will be printed that the input is wrong.

When the user enters the number 1, he will receive Class A data with the message.

And if he enters number 2, it will print him the data of Class B.

And if you enter the number 3, it will print the data of the third class C.

And if he enters other than these values, an error will be printed for him.

 

Example 2

Write a simple calculator program in Java using conditional probabilities.

package switch_case;
import java.util.Scanner;
public class Switch_Case {
    public static void main(String[] args) {
        Scanner n=new Scanner(System.in) ;
        int a,b;
        String op;
        System.out.println("Enter First Number");
        a=n.nextInt();
        System.out.println("Enter Last Number");
        b=n.nextInt();
        System.out.println("Enter op(*,/,+");
        n=new Scanner(System.in);
        op=n.nextInt();
        switch(op)
        {
            case "+":
                System.out.println(a+b);
                break;
                case "/"
                System.out.println(a/b);
                break;
                case "*"
                System.out.println(a*b);
                break;
                default:
                    System.out.println("Error");
        }
    }
}

We created two variables a and b and created a text variable op to receive data from the user. And then print a sentence asking the user to enter the first number and then the second number. Then he is asked what type of arithmetic operation is it multiplication, division or addition, then he performs the required operation. Then we used the probabilities of these operations and set the default in case the user entered a type of operation that does not exist and an error is printed for him.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com