এই নিবন্ধে আমরা বুঝবো কিভাবে তিনটি বুলিয়ান (Boolean) ভেরিয়েবলের মধ্যে দুটি true কিনা চেক করতে হয়। বুলিয়ান ভেরিয়েবল হলো এমন ডাটা টাইপ যা শুধু true বা false মান ধারণ করতে পারে।
নিচে এর একটি প্রদর্শন দেওয়া হলো:
ইনপুট
মনে করুন আমাদের ইনপুট হলো:
Input : true, true, false
আউটপুট
প্রত্যাশিত আউটপুট হবে:
Result : Two of the three variables are true
অ্যালগরিদম
ধাপ ১ - শুরু ধাপ ২ - চারটি বুলিয়ান মান ঘোষণা করুন: my_input_1, my_input_2, my_input_3 এবং my_result ধাপ ৩ - ব্যবহারকারীর কাছ থেকে মান পড়ুন / মান সংজ্ঞায়িত করুন ধাপ ৪ - একটি if-else শর্ত ব্যবহার করে তিনটি মানের মধ্যে প্রতিবার দুটি করে AND অপারেটর দিয়ে তুলনা করুন ধাপ ৫ - ফলাফল প্রদর্শন করুন ধাপ ৬ - শেষ
উদাহরণ ১
এখানে ব্যবহারকারী প্রম্পট অনুযায়ী ইনপুট দিচ্ছেন। আপনি আমাদের কোডিং গ্রাউন্ড টুলে এই উদাহরণটি লাইভ চেষ্টা করতে পারেন
।
import java.util.Scanner;
public class BooleanValues {
public static void main(String[] args) {
boolean my_input_1, my_input_2, my_input_3, my_result;
System.out.println("The required packages have been imported");
System.out.println("A scanner object has been defined ");
Scanner my_scanner = new Scanner(System.in);
System.out.print("Enter the first boolean value: ");
my_input_1 = my_scanner.nextBoolean();
System.out.print("Enter the second boolean value: ");
my_input_2 = my_scanner.nextBoolean();
System.out.print("Enter the third boolean value: ");
my_input_3 = my_scanner.nextBoolean();
if(my_input_1) {
my_result = my_input_2 || my_input_3;
} else {
my_result = my_input_2 && my_input_3;
}
if(my_result) {
System.out.println("Two of the three variables are true");
} else {
System.out.println("Two of the three variables are false");
}
}
}
আউটপুট
The required packages have been imported A scanner object has been defined Enter the first boolean value: true Enter the second boolean value: true Enter the third boolean value: false Two of the three variables are true
উদাহরণ ২
এখানে মানগুলো পূর্বে সংজ্ঞায়িত করা হয়েছে, এবং কনসোলে অ্যাক্সেস করে প্রদর্শন করা হয়েছে।
public class BooleanValues {
public static void main(String[] args) {
boolean my_input_1, my_input_2, my_input_3, my_result;
my_input_1 = true;
my_input_2 = true;
my_input_3 = false;
System.out.println("The three boolean values are defined as " + my_input_1 + " , " + my_input_2 + " and " + my_input_3);
if(my_input_1) {
my_result = my_input_2 || my_input_3;
} else {
my_result = my_input_2 && my_input_3;
}
if(my_result) {
System.out.println("Two of the three variables are true");
} else {
System.out.println("Two of the three variables are false");
}
}
}
আউটপুট
The three boolean values are defined as true , true and false Two of the three variables are true