امثلة على استخدام if else في جافا java


امثلة وتطبيقات على العبارات الشرطية باستخدام if..else بلغة جافا java

اكتب برنامج باستخدام لغة جافا يطلب من الطالب ادخال علامته ومن ثم يطبع النتيجة ان كان الطالب ناجح ام راسب:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
//write a program asks student to enter his mark, then checks if student passed of failed and print result 

Scanner input=new Scanner(System.in);
System.out.println("enter your mark:");
double mark=input.nextDouble();
if(mark >=60)
{
System.out.println("passed");
}
else
{
System.out.println("failed");
}

}
}

 

 برنامج الصراف الالي, يطلب من العميل كلمة المرور ومن ثم يتحقق منها ويظهر النتيجة:

write java program for atm machine to ask user to enter his passkey, then check if passkey is correct or wrong:

/*write java program for atm machine to ask user to enter his passkey, then check if passkey is correct or wrong*/

import java.util.Scanner;

public class Main
{
 public static void main(String[] args) {
	/*Scanner input=new Scanner(System.in);
	int correctPasskey=3223;
	System.out.println("welcome client, please enter your passkey");
	int passkey=input.nextInt();
	if(passkey==correctPasskey)
	System.out.println("correct Passkey");
	else 
	System.out.println("wrong Passkey");
}

 

 

برنامج يطلب من الطالب ادخال علامته ومن ثم يطبع الدرجة الموافقة للعلامة:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("enter the mark:");
int mark=in.nextInt();
            
if(mark>100)
  System.out.println("invalid entry");
else if(mark>=90)
  System.out.println("A");
else if(mark>=80)
  System.out.print("B");
else if(mark>=70)
  System.out.print("C");
else if(mark>=60)
  System.out.println("D");
else
  System.out.println("F");

}
}

 

برنامج يطلب من المستخدم ادخال رقم اليوم ويطبع اسم اليوم الموافق له:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
//write java program that ask user to enter the number of week day and prints the //equiavalent week day name
        Scanner in=new Scanner(System.in);
        System.out.println("enter the day number:");
        int daynumber=in.nextInt();
        if(daynumber==1)
            System.out.println("saturday");
        else if(daynumber==2)
            System.out.println("sunday");
        else if(daynumber==3)
            System.out.println("monday");
        else if(daynumber==4)
            System.out.println("tuesday");
        else if(daynumber==5)
            System.out.println("wednsday");
        else if(daynumber==6)
            System.out.println("thursday");
        else if(daynumber==7)
            System.out.println("friday");
        else System.out.println("invalid entry");
}
}

 

برنامج يقوم بحل معادلات تربيعية:

/*
(Algebra: solve quadratic equations) The two roots of a quadratic equation
ax^2 + bx + c = 0 can be obtained using the following formula:

b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the
equation has two real roots. If it is zero, the equation has one root. If it is negative,
the equation has no real roots.
Write a program that prompts the user to enter values for a, b, and c and displays
the result based on the discriminant. If the discriminant is positive, display two
roots. If the discriminant is 0, display one root. Otherwise, display “The equation
has no real roots”.
Note that you can use Math.pow(x, 0.5) to compute 2x.
*/
import java.util.Scanner;

public class Exercise_03_01 {
 public static void main(String[] args) {
       
// Create a Scanner object
  Scanner input = new Scanner(System.in);

  // Prompt the user to enter values for a, b and c.
  System.out.print("Enter a, b, c: ");
  double a = input.nextDouble();
  double b = input.nextDouble();
  double c = input.nextDouble();

  // Compute the discriminant of the quadriatic equation.
  double discriminant = Math.pow(b, 2) - 4 * a * c;

  // Compute the real roots of the quadriatic equation if any.
  System.out.print("The equation has ");
  if (discriminant > 0)
  {
   double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);  
   double root2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);  
   System.out.println("two roots " + root1 + " and " + root2);
  }
  else if (discriminant == 0)
  {
   double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
   System.out.println("one root " + root1);
  }
  else
   System.out.println("no real roots");
 }
}

 

ترتيب 3 اعداد تصاعديا من الاصغر الى الاكبر

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
/*(Sort three integers) Write a program that prompts the user to enter three integers
and display the integers in non-decreasing order.*/

Scanner input = new Scanner(System.in);

		// Prompt the user to enter three integers
		System.out.print("Enter three integers: ");
		int number1 = input.nextInt();
		int number2 = input.nextInt();
		int number3 = input.nextInt();

		// Sort numbers
		int temp;
		if (number2 < number1 || number3 < number1)
		{
			if (number2 < number1)
			{
				temp = number1;
				number1 = number2;
				number2 = temp; 
			}
			if (number3 < number1)
			{
				temp = number1;
				number1 = number3;
				number3 = temp;
			}
		}
		if (number3 < number2)
		{
			temp = number2;
			number2 = number3;
			number3 = temp;
		}

		// Display numbers in accending order
		System.out.println(number1 + " " + number2 + " " + number3);
}
}

 

برنامج يقوم بتوليد رقم عشوائي بين 1 و 12 ومن ثم يطبع اسم الشهر الموافق للرقم:

 

/*(Random month) Write a program that randomly generates an integer between 1
and 12 and displays the English month name January, February, …, December for
the number 1, 2, …, 12, accordingly.*/
int month=1+(int)(Math.random()*(12-1+1));
if(month==1)
	System.out.println("january");
else if(month==2)
	System.out.println("february");
else if(month==3)
	System.out.println("march");
else if(month==4)
	System.out.println("april");
else if(month==5)
	System.out.println("may");
else if(month==6)
	System.out.println("june");
else if(month==7)
	System.out.println("july");
else if(month==8)
	System.out.println("august");
else if(month==9)
	System.out.println("september");
else if(month==10)
	System.out.println("october");
else if(month==11)
	System.out.println("november");
else if(month==12)
	System.out.println("december");

 

 لعبة اختيار الوجه/الصورة للعملة المعدنية:

 

/*(Game: heads or tails) Write a program that lets the user guess whether the flip of
a coin results in heads or tails. The program randomly generates an integer 0 or 1,
which represents head or tail. The program prompts the user to enter a guess and
reports whether the guess is correct or incorrect.*/
		
Scanner s = new Scanner(System.in);
int randomNumber, guess;
		  
System.out.print("Enter a guess(0 or 1):");
guess = s.nextInt();
		  
if(guess == 0 || guess == 1) {
        randomNumber = (int)(Math.random() * 2);
		   
   if(guess == randomNumber) {
	System.out.print("You won.");
   } 
   else {
	System.out.print("You lose.");
   }
} 
else 
{
	System.out.print("The number you entered is not valid.");
}

 


المرفقات:
هل كان المحتوى مفيد؟

التعليقات

لاضافة سؤال أو تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

امثلة على الحلقات for loops باستخدام جافا
امثلة conditional statements باستخدام Switch case | جافا java
محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...