Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

تحديات برمجيةالتحدي الثاني - حل التمرين الخامس بلغة جافا

المطلوب

أكتب برنامج يطلب من المستخدم إدخال رقمين, ثم يعرض له قائمة خيارات يمكن تطبيقها على هذين الرقمين.
المستخدم سيكون عليه إدخال رقم الخيار فقط حتى يتم تنفيذه.

  • الخيار 0 لإيقاف البرنامج.
  • الخيار 1 لطباعة ناتج جمع العددين.
  • الخيار 2 لطباعة ناتج طرح العددين.
  • الخيار 3 لطباعة ناتج ضرب العددين.
  • الخيار 4 لطباعة ناتج قسمة العددين.

الحل بلغة جافا

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double a, b;
int option = -1;
System.out.print("Enter first number: ");
a = input.nextDouble();
System.out.print("Enter second number: ");
b = input.nextDouble();
System.out.println(
"\nWrite down the operation number then press enter\n"
+ "0: To end the program.\n"
+ "1: To print the sum.\n"
+ "2: To print the subtraction.\n"
+ "3: To print the multiplication.\n"
+ "4: To print the division.");
while (option != 0)
{
System.out.println("-------------------------");
System.out.print("Enter option: ");
option = input.nextInt();
switch (option)
{
case 0:
System.out.println("Program end.");
break;
case 1:
System.out.println(a + " + " + b + " = " + (a + b));
break;
case 2:
System.out.println(a + " - " + b + " = " + (a - b));
break;
case 3:
System.out.println(a + " * " + b + " = " + (a * b));
break;
case 4:
System.out.println(a + " / " + b + " = " + (a / b));
break;
}
}
}
}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); double a, b; int option = -1; System.out.print("Enter first number: "); a = input.nextDouble(); System.out.print("Enter second number: "); b = input.nextDouble(); System.out.println( "\nWrite down the operation number then press enter\n" + "0: To end the program.\n" + "1: To print the sum.\n" + "2: To print the subtraction.\n" + "3: To print the multiplication.\n" + "4: To print the division."); while (option != 0) { System.out.println("-------------------------"); System.out.print("Enter option: "); option = input.nextInt(); switch (option) { case 0: System.out.println("Program end."); break; case 1: System.out.println(a + " + " + b + " = " + (a + b)); break; case 2: System.out.println(a + " - " + b + " = " + (a - b)); break; case 3: System.out.println(a + " * " + b + " = " + (a * b)); break; case 4: System.out.println(a + " / " + b + " = " + (a / b)); break; } } } }

إذا قام المستخدم بإدخال الرقمين 1 و 4. ثم قام بتجربة كل الخيارات الموجودة في القائمة, فستكون النتيجة كالتالي.
قمنا بتعليم المعلومات التي أدخلها المستخدم باللون الأصفر و النتائج التي ظهرت له بعد إدخالها باللون الأزرق.

Enter first number:  1 
Enter second number:  4 

Write down the operation number then press enter
0: To end the program.
1: To print the sum.
2: To print the subtraction.
3: To print the multiplication.
4: To print the division.
-------------------------
Enter option:  1 
1 + 4 = 5
-------------------------
Enter option:  2 
1 - 4 = -3
-------------------------
Enter option:  3 
1 * 4 = 4
-------------------------
Enter option:  4 
1 / 4 = 0.25
-------------------------
Enter option:  0 
Program end.