Getting start with Java Part II
Calculator Program မွာတုန္းက ေျပာထားတဲ႔ အတိုင္း ေပါင္းထားတဲ႔ အေျဖမထြက္ မခ်င္း user input မမွန္မခ်င္း
ကြ်န္ေတာ္က user ဆီက input ကိုထပ္ကာထပ္ကာ ေတာင္းေစခ်င္ရင္ ေတာ့ ဒီလိုပံုစံနဲ႔ ေရးလို႔ရပါတယ္။
public class Calculator { public static void sum() { try{ java.util.Scanner scan = new java.util.Scanner(System.in); System.out.print("please enter first number"); int first=scan.nextInt(); System.out.print("please enter second number"); int second= scan.nextInt(); System.out.println(first+second); }catch(Exception ae) { sum(); } } public static void main(String args[]) { sum(); } }ဒီေနရာမွာ ကြ်န္ေတာ္တို႔ ေတာင္းထားတဲ႔ input value ကို user က ရိုက္ထည့္တာ မမွန္ခဲ႔ရင္ catch မွာ exception
မိပါတယ္ အဲ ဒီအခ်ိန္မွာပဲ sum() method ကို ျပန္ေခၚလို႔ရပါတယ္ ဒီလိုဆို ထပ္ခါထပ္ခါ user input မမွန္မခ်င္း
input value ေတာင္းေနမွာပါ။ခုတစ္ေခါက္ ကြ်န္ေတာ္တို႔ စာေမးပြဲ အေအာင္အရံႈးစစ္တဲ႔ program ေလးတစ္ပုဒ္ေလာက္ေရးၾကည့္ရေအာင္။ ခုကြ်န္တာ္ေရးမယ့္အတိုင္း
ေရးဖို႔ ဆိုရင္ေတာ့ ပထမဆံုး စာေမးပြဲအေအာင္အရံႈး စစ္ဖို႔ အတြက္ Calculator program တုန္းက သံုးထားၿပီးျဖစ္တဲ႔
- User Input ဘယ္လိုေတာင္းရလဲ ဆိုတာရယ္။
- Variable ကိုဘာလို႔ သံုးလဲဆိုတဲ႔ သေဘာတရားရတယ္
- Try Catch block ကို ဘာေၾကာင့္သံုးရတယ္ ဘယ္လိုသံုးရတယ္ ကိုသိရမယ့္ အျပင္
ခုထပ္ၿပီးသိရမွာက (if condition) ျဖစ္ပါတယ္။ if ... else အလုပ္လုပ္ပံုကို အရင္ ေလ႔လာၿပီး ေရးၾကည္႔ႏိုင္ပါၿပီ။
ျမန္မာလို စဥ္းစားလွ်င္
တကယ္လို႔(အမွတ္က ေလးဆယ္ႏွင့္ညီၿပီးၾကီးလွ်င္)
{
စာေမးပြဲေအာင္သည္။
}
မဟုတ္လွ်င္
{
စာေမးပြဲက်သည္။
}
|
program လိုေရးလွ်င္
if(mark >= 40)
{
Pass the exam.
}
else
{
Fail the exam
}
|
တစ္ခါတစ္ရံတြင္ ႏွစ္ခုထက္ မကေသာ အေျခအေနမ်ားအတြက္ if ... else if ... else အစရွိသျဖင့္ ေရးႏိုင္ပါသည္။
eg.
if(mark>100) { System.out.println("Invalid Marks"); } else if(mark<0) { System.out.println("Invalid Marks"); } else if(mark>=40) { System.out.println("Pass the exam"); } else { System.out.println("Fail the exam"); }Calculator Example အတိုင္း Exam program တစ္ကို ကို ေရးၾကည့္ႏိုင္ပါသည္။ Calculator Program ကို
အေျခခံၿပီး ေရးမည္ဆိုလွ်င္public class Calculator { public static void sum() { try{ java.util.Scanner scan = new java.util.Scanner(System.in); System.out.print("please enter Your Mark"); int first=scan.nextInt(); if(first>=40) { System.out.println("Pass the exam"); }else { System.out.println("Fail the exam"); } }catch(Exception ae) { System.out.println("Invalid marks"); } } public static void main(String args[]) { sum(); } }ယခု program ကိုၾကည့္ၿပီး if else condition ကိုသံုးကာ မိမိကိုယ္တိုင္ ဖန္တီးထားေသာ exam program
တစ္ပုဒ္ကို ေရးႏိုင္ပါသည္။
Facebook comments:



အစ်ကိုရေ့ဆက်ရေးပါအုံးဗျ ကျွန်တော်အခုမှနည်းနည်းရှင်းသွားတာ ကျေးဇူးဘဲ
အစ်ကို့ကိုဘယ်လိုဆက်သွယ်လို့ရမလဲ အီးမေးပေးပါလား မသိတာလေးတွေမေးချင်လို့
ခုလို အားပေးတာ ကျေးဇူးတင်ပါတယ်။ ကျွန်တော်လဲ ဆက်ရေးဖို့ ရှိပါတယ် ခုလောလောဆယ် မအားသေးလို့ မရေးဖြစ်သေးတာပါ။ ခုလဲ ကျွန်တော်ဆက်ရေးမှာပါ။ ကျွန်တော်အတတ်နိုင်ဆုံး အားလပ်တဲ့ အချိန်တိုင်း ဆက်ရေးသွားမှာပါ။ မေးမြန်းဖို့ ကတော့ ကျွန်တော်တို့ ဆိုက်ရဲ ့တာဝန်ရှိသူတွေနဲ့ တိုင်ပင်လိုက်ပါဦးမယ် ခင်ဗျာ။ ကျေးဇူးတင်ပါတယ်။