ဟန်မျိုးထွေး
ဘာမှမလုပ်ဖြစ်ခြင်း...
အလုပ်တွေများနေလို့ Post ကောင်း ကောင်းမတင်နိုင် သေးတာပါ... နောက် တစ်ပတ်မှာ စာမေးပွဲရှိတယ်။ ပြီးတော့ အလုပ်တွေရှိတယ်။ စာပြန်ကြည့်ဖြစ်မှပဲ ဆက်ရေးတော့မယ်... အားလုံးကို တောင်းပန်ပါတယ်....
In
Labels:
တွေးမိတွေးရာ
by Han Myo Htwe
Object-Oriented Programming Problem 4
အရင်ဆုံး သိရမဲ့ ကြောင်းအရာလေးတွေကို ပြန်ပြီးပြောမယ်နော်... ပထမဆုံး for loop, while loop, arrays တွေဘယ်လို လုပ်ရမလဲ? စတာတွေကိုအရင် ပြန်ပြီးကြည့်လိုက်ပါ... ပြီးရင်တော့ နောက်တစ်ခန်းဖြစ်တဲ့ Problem 4 ကိုဆက်ပါမယ်။
ဒီအခန်းမှာတော့ အသစ်ဆိုလို့ Random Number လုပ်တာ၊ Exception ဆိုတာဘာလဲ? try and catch ကိုဘယ်လိုနေရာတွေမှာ သုံးလည်းဆိုတာတွေ ပါတယ်။ ကျန်တာတွေကတော့ အရင်အခန်းတုန်းက အကြောင်းအရာတွေကိုပဲ ပြန်သုံးတယ်...
Problem Statement ကတော့
ဒီအခန်းမှာတော့ အသစ်ဆိုလို့ Random Number လုပ်တာ၊ Exception ဆိုတာဘာလဲ? try and catch ကိုဘယ်လိုနေရာတွေမှာ သုံးလည်းဆိုတာတွေ ပါတယ်။ ကျန်တာတွေကတော့ အရင်အခန်းတုန်းက အကြောင်းအရာတွေကိုပဲ ပြန်သုံးတယ်...
Problem Statement ကတော့
Let’s build a game using Java!
Game Play
The computer generates 5 random numbers, each number within 1 to 50 (inclusive). The numbers are hidden from the player.
§ The player has three tries to guess three numbers out of the list of five numbers generated by the computer.
§ One (1) point is given for every correct number guessed.
Validation
The game should check that only valid numbers are entered. That is:
§ The player cannot enter characters, e.g. “twelve”.
§ Only numbers between 1 and 50 (inclusive) are acceptable.
Enhancement
The game should reject numbers which has already been guessed whether correct or not. For example, if the user guesses three (3), he should not be allowed to guess three (3) again in the second and third tries.
Game Extension
Extend your program to give a player N rounds of playing the game, where N is a number entered when first starting the game. (Note: save this version of the game under a different name)
ကြည့်လိုက်တာနဲ့ ဘာလုပ်ရမလဲ ဆိုတာ ရှင်းနေပါတယ်။ မရှင်းဘူးဆိုရင်လည်း ပြောပြပါ့မယ်... အရင်ဆုံးကတော့ Random number ကိုလုပ်မယ်.. ပြီးရင် Arrays တစ်ခုကိုလုပ်မယ်၊ Arrays မှာ ၅ခုထားမယ်၊ ဆိုလိုတာက random number ၅ခုလုပ်မယ်ပေါ့။ ကုဒ်ကတော့ အောက်မှာပါ...
private int[] arrayRandomNumber = new int[5];
private void generateNumbers() {
Random generator = new Random();
for (int i = 0; i < arrayRandomNumber.length; i++) {
arrayRandomNumber[i] = generator.nextInt(50) + 1;
System.out.println(arrayRandomNumber[i]);
}
}
ဒါက method တစ်ခုလုပ်တာပါ။ ပြီးတော့မှ ပြန်ပြီးယူသုံးမှာပါ။ ဘာလို့လုပ်လဲဆိုတာရယ် လုပ်ရင်ဘာတွေပိုကောင်းလည်းဆိုတာ နောက်အခန်းမှာ အသေးစိတ်ပြောပါမယ်။
မှတ်ရန်။
import java.util.Random;
ကို အရင်ဆုံး ရေးရမယ်။ random လုပ်ဖို့ import လုပ်တာပါ။
အပေါ်ကကုဒ်ကို ပြန်သုံးချင်ရင် generateNumbers(); ဆိုပြီးသုံးရင်ရပါပြီ။ နောက်တစ်ခုကတော့ ကစားမဲ့လူက ၃ ခါစမ်းခွင့် ရှိတဲ့အတွက် for loop နဲ့ ပြီးတော့ while loop နဲ့ ရေးမယ်...
for (int num = 0; num < 3; num++) {
boolean valid = true;
while (valid) {
int guessNumber = Keyboard.readInt("GUESS SECRET NUMBER "
+ (num + 1) + " > ");
ပြီးရင် if နဲ့ conditions တွေလုပ်မယ်၊ for loop နဲ့ numbers တွေတူလား အတူလား စစ်မယ်။ တူတယ်ဆိုရင် score ကို ၁ ပေါင်းပေးမယ်။ ဒီမှာ if ,else ၂ခုနဲ့လုပ်ထားတယ်၊ ပထမတစ်ခုက number ကို ၅၀ နဲ့ ၁ အတွင်းရှိမရှိစစ်တာ။ နောက်တစ်ခုကတော့ number က random number နဲ့ တူ မတူ စစ်တာ။ တူရင် ၁ပေါင်းမယ်ပေါ့။ else ကတော့ ၁ ကနေ ၅၀ အတွင်း မဖြစ်ရင် ပြန်ရေးခိုင်းဖို့ပါ။ အောက် ကကုဒ်ကို ကြည့်လိုက်ရင် ရှင်းသွားမယ် ထင်တယ်။
if (guessNumber >= 1 && guessNumber <= 50) {
for (int i = 0; i < arrayRandomNumber.length; i++) {
if (guessNumber == arrayRandomNumber[i]) {
score++;
}
valid = false;
}
} else {
System.out.println(" Please enter number (1 to 50) ");
}
နောက်ဆုံးအပိုင်းကတော့ ရှုံးလား နိုင်လား စစ်တာ ပါ။ if and else နဲ့ပါပဲ။ score ၁ ရ ရင် နိုင်တယ်။ မရရင် ရှုံးတယ်ပေါ့။
if (score <= 0) {
System.out.println("You Lose! Try Again...");
} else {
System.out.println(" You are **Winner** ");
System.out.println(" Your Score is " + score);
}
ပြီးရင်အားလုံးပြီးသွားပါပြီ။ ပေါ်မှာပြောထားတဲ့ Exception, try and catch အကြောင်းလေး မပြောလိုက်ရဘူး။ Exceptions တွေကတော့ integer နေရာမှာ String ရေးလို့ မရတာ၊ zero နဲ့ စားလို့ မရတာ စတဲ့ errors တွေ ကို ပြောတာပါ။ အဲ့လို error တွေမဖြစ်ချင်ရင် try and catch ကို သုံးပေါ့။ ကျောင်းကပေးထားတဲ့ keyboard.java မှာ အများကြီးသုံးထားတယ်။ လေ့လာချင်ရင် keyboard.java ကို ဖွင့်ပြီး ကုဒ်တွေကို လေ့လာ ကြည့်ပါ... နောက် အခန်းမှာ class, methods, field, main စတာတွေ ပါလိမ့်မယ် ကြိုပြီးလေ့လာလို့ ရအောင် class diagram လေး ထည့်ပေးလိုက်တယ်။
နောက် အခန်းမှပဲ အသေးစိတ် ပြောတော့မယ်။ ဆက်ပါအုန်းမယ်.....
In
Labels:
Java
by Han Myo Htwe
Object-Oriented Programming Problem 3
မအားတာနဲ့ တော်တော်တောင် ကြာသွားတယ်.. ပြီးတော့ Problem 3 ကို အရမ်းကြီးနားမလည်လို့ နဲနဲ စာပြန်ဖတ်ပြီးမှ ဒီ Post ကိုတင်နိုင်တယ်.. ဖတ်ပြီးသွားတော့ နားလည်သလိုလိုတော့ရှိသွားတယ်.. ကဲ စပြီးရေးတော့မယ်နော်.. ဘာပြဿနာလဲ မပြောခင် ဘာအကြောင်းအရာတွေ ပါလဲဆိုတာ အရင်ပြောမယ်။ ပထမဆုံးကတော့ while loop ၊ ပြီးတော့ Arrays ၊ နောက်ဆုံးတစ်ခုကတော့ For loop ။ အရင်တုန်းက Python သင်ဖူးရင် နားလည်ဖို့ ပိုလပိုလွယ်တယ်။ သဘောတရားက တူတူပဲလေ..
Problem Statement ကတော့ အောက်မှာဖတ်ကြည့်လိုက်တော့...
double[] spending = new double[12];
double total = 0;
double balance = 0;
for (int i = 0; i < 12; i++) {
balance = balance + 1000;
spending[i] = Keyboard.readInt("How much did you spent for Month "
+ (i + 1) + " : ");
while (balance < spending[i]) {
System.out
.println("*** Your are spending more than your balance ***");
spending[i] = Keyboard
.readInt("How much did you spent for Month " + (i + 1)
+ " : ");
}
balance = balance - spending[i];
total = total + spending[i];
}
for (int i = 0; i < 12; i++) {
System.out.println("Spending for Month" + (i + 1) + " is "
+ spending[i]);
System.out.println("Total saving is " +balance);
System.out.println("Total Spending >>" + total);
System.out.println("Average Spending >> " + (total / 12));
}
System.out.println("End of Program");
}
double[] spending = new double[12];
ဆိုတာက Arrays တစ်ခုလုပ်လိုက်တာ။ ဘယ်လောက်လုပ်မှာလဲဆိုတော့ ၁၂ခုထဲ့ထာမယ်ဆိုတော့ [12]။
for (int i = 0; i < 12; i++){}
ကတော့ သိရမှာ ၃ခုရှိတယ်။ int i=0; ဆိုတာကတော့ ဘယ်ကစမှာလဲ ကိုပြောတာ။ i<12; ဆိုတာကတော့ ဘယ်မှာဆုံးမယ်ဆိုတာကို ပြောတာ။ နောက်ဆုံးတစ်ခုကတော့ i++ or i=i+1; ကတော့ ဘယ်လောက်တိုးပြီးသွားမလဲဆိုတာကိုပြောတာပါ။ ဒီ loop မှာဆိုရင် arrays မှာ ၁၂ index ရှိပါတယ်။ ၀ ကနေစပြီးတော့ ၁၁ အထိပေါ့။
balance = balance + 1000;
balance ကို ၁၀၀၀ ရှိတယ်လို့ပြောချင်တာ
Problem Statement ကတော့ အောက်မှာဖတ်ကြည့်လိုက်တော့...
Andrew works part-time as a sales promoter and earns $1000 every month. Besides keeping records of his salary slips, he also kept his expense receipts last year. As he plans to better control his expenditure this year, he would like to know how much he spent and saved in 2009.
Help Andrew by building a simple application in Java to achieve his goal. The application should do the following:
(1) In the beginning of each month, the application puts $1000 into Andrew's bank balance.
(2) The application should ask Andrew to enter how much he spent in total for that month.
(3) When all the needed data is entered, the application:
a. Displays the expenditure for each month.
b. Calculates the total amount of money that Andrew spent for the year.
c. Calculates the monthly average amount of money that Andrew spent.
d. Calculates the balance of money left in his savings.
* Note - at any point in time, Andrew should not be able to spend more money than what he has saved so far. If Andrew enters a value larger than what he currently has in the bank, he should be asked to enter again.
Advanced Features
(4) The application should also take into account the interest rate of the bank (e.g. 1% per month). For this problem, we will assume that the bank pays compound monthly interest for Andrew’s savings.
ဖတ်ပြီးပြီဆိုရင်တော့ ဘာလုပ်ရမလဲ သိပြီထင်တယ်...
ကုဒ်စရေးတော့မယ် အောက်ဆုံးမှဘဲ ရေးထားတဲ့ ကုဒ်တွေကိုပြန်ရှင်းတော့မယ်။
public void start() { double[] spending = new double[12];
double total = 0;
double balance = 0;
for (int i = 0; i < 12; i++) {
balance = balance + 1000;
spending[i] = Keyboard.readInt("How much did you spent for Month "
+ (i + 1) + " : ");
while (balance < spending[i]) {
System.out
.println("*** Your are spending more than your balance ***");
spending[i] = Keyboard
.readInt("How much did you spent for Month " + (i + 1)
+ " : ");
}
balance = balance - spending[i];
total = total + spending[i];
}
for (int i = 0; i < 12; i++) {
System.out.println("Spending for Month" + (i + 1) + " is "
+ spending[i]);
System.out.println("Total saving is " +balance);
System.out.println("Total Spending >>" + total);
System.out.println("Average Spending >> " + (total / 12));
}
System.out.println("End of Program");
}
double[] spending = new double[12];
ဆိုတာက Arrays တစ်ခုလုပ်လိုက်တာ။ ဘယ်လောက်လုပ်မှာလဲဆိုတော့ ၁၂ခုထဲ့ထာမယ်ဆိုတော့ [12]။
for (int i = 0; i < 12; i++){}
ကတော့ သိရမှာ ၃ခုရှိတယ်။ int i=0; ဆိုတာကတော့ ဘယ်ကစမှာလဲ ကိုပြောတာ။ i<12; ဆိုတာကတော့ ဘယ်မှာဆုံးမယ်ဆိုတာကို ပြောတာ။ နောက်ဆုံးတစ်ခုကတော့ i++ or i=i+1; ကတော့ ဘယ်လောက်တိုးပြီးသွားမလဲဆိုတာကိုပြောတာပါ။ ဒီ loop မှာဆိုရင် arrays မှာ ၁၂ index ရှိပါတယ်။ ၀ ကနေစပြီးတော့ ၁၁ အထိပေါ့။
balance = balance + 1000;
balance ကို ၁၀၀၀ ရှိတယ်လို့ပြောချင်တာ
spending[i] = Keyboard.readInt("How much did you spent for Month " + (i + 1) + " : ");
ဒါကတော့ spending arrays ကို input လုပ်တာပါ။ (i+1)ဆိုတာကတော့ ပထမ ဆုံးကို ၁ ဖြစ်အောင်လုပ်လိုက်တာပါ။ ပုံမှန်ဆိုရင်တော့ i က 0 ဖြစ်နေတာပါ။
while (balance < spending[i]) {}
while နဲ့ for ကသဘောတရားကတူတူပါပဲ။ အစတွေ၊ အဆုံးတွေ၊ စတဲ့ condition တွေမသိဘူးဆိုရင် while ကိုပိုသုံးပါတယ်။
နောက်ဆုံးသိရမှာကတော့ ဒီအခန်းဟာ တော်တော်အရေးပါတဲ့ အခန်းဖြစ်ပါတယ်။ for loop, while loop, arrays စတာတွေမသိဘူး နားမလည်ဘူးဆိုရင်တော့ နောက်ပိုင်းအခန်းတွေဆိုပိုဆိုးသွားနိုင်ပါတယ်။ အဲ့တော့ နောက်တစ်ခေါက်လောက် ပြန်ကြည့်ရင်ပိုကောင်းမယ်။ ကျွန်တော်က ရှင်းတာမကောင်းလို့သေသေချာချာ မရှင်းပြနိုင်တာ ခွင့်လွတ်ပေးပါလို့ တောင်းပန်ပါတယ်။ မှားတာရှိရင်လည်း ပြောခဲ့ကြပါ။
ဆက်ပါအုံးမယ်... နောက်တစ်ပတ်တော့ လေ့ကျင့်ခန်းလေးတွေပါ လုပ်ကြည့်မယ်နော်...
In
Labels:
Java
by Han Myo Htwe
Object-Oriented Programming Problem 2
Problem 2 ကို တော့ အပိုင်းမခွဲတော့ဘူး တစ်ခုတည်းနဲ့ ပြီးအောင်ရေလိုက်တော့မယ်။ အရင်ဆုံး သိရမဲ့ဟာတွေကတော့...
Problem 1 တုန်းက ပြောခဲ့တဲ့ Data Types၊ Declare လုပ်တာ စတဲ့ ရေးရမဲ့ ပုံစံတွေကို ပြန်သိဖို့တော့ လိုမယ်။
အခု Problem 2 ကတော့ အရမ်းကြီးမခက်ဘူး။ Python မှာသင်ဖူးတဲ့ {if, else if, else} လိုမျိုးတွေရေးရမယ်။ တစ်နည်းပြောရရင် true or false conditions တွေကို ရေရတာပဲ။ true ဖြစ်ရင် ဘာ ဆက်လုပ်မယ်၊ false ရင်ဘာလုပ်မယ်၊ စတာတွေကို ရေးရမှာပါ။ python နဲ့မတူတာက java မှာ if ကို { } နဲ့ လုပ်တယ်။ ရှင်းအောင်ပြောရရင်..
if {
else if {
}
else {
}
}
else {
}
ဒါကမှ Problem 2 ပါ...
Fit Lions Sports Club is pleased with the Sports Training Calculator that you have developed. Thus they would to add some addition features to the existing Sports Training calculator.
The Sports Training Calculator should use the following table to determine if the athlete is within the guidelines for their selected sport:
VO2Max | |||
Sport | Age | Males | Females |
Basketball | 18-30 | 40-60 | 43-60 |
Bicycling | 18-26 | 62-74 | 47-57 |
Canoeing | 18-26 | 55-67 | 47-67 |
Gymnastics | 18-22 | 52-58 | 36-50 |
Swimming | 10-25 | 50-70 | 40-60 |
Each team needs to complete at least 2 different sports. Can your team help to make the modifications?
အရင်တုန်းကရေးထားတဲ့ ကုဒ်ကို ပြန်သုံးရမယ်နော်.. ဒီမှာပိုသွားတာက if conditions တွေပါလာတာဘဲ။ worksheet ကိုအကုန်လုံးပြန်မရေးတော့ဘူးဗျာ.. အရမ်းများတယ်။ အားလုံးလည်းသိပြီးသားလို့ထင်တယ်။ ချုံ့ပြီးပြောရရင် ၊ ကုဒ်သုံးခုပေးထားတယ်...
Version A, B, C မှာ ဘယ်ဟာက ကောင်းဆုံးလဲ၊ ကျွန်တော်ကတော့ C လို့ ဖြေလိုက်တယ်၊
ပြီးတော့ Multiple Branching အကြောင်းလုပ်ရတယ်။ အဲ့ဒါက if, else if, else ကိုပြောတယ်ဘဲ။
နောက်တစ်ခုက Multiple Conditions အဲ့ဒါကတော့ {or , and} ကိုပြောတာ၊ အသေးစိတ်ကိုတော့ နောက်မှာဆက်ပြောမယ်။ ပြီးတော့ Flowchat တစ်ခုပေးထားတယ် အဲ့ဒါကိုကြည့်ပြီး ကုဒ်ရေးရင်ရပြီ။ flowchart ပေးထားရင် ကုဒ်ရေးရတာ အရမ်းလွယ်သွားပြီလေ...
အပေါ်မှာပြထားတာ ရှင်းရှင်းလေးပဲ.. အရင်ဆုံး true တွေကို if နဲ့ စရေးမယ်။ age, gender, VO2 တွေက မှန်တယ်ဆိုရင် .. Age and VO2 are OK! လို့ ပြလိုက်မယ်။ age and gender က မှန်တယ်၊ VO2 ကမှားမယ်ဆိုရင် VO2 is not within limit လို့ ပြမယ်။ အဲ့လိုပဲ male and female ကိုလည်း ခွဲလိုက်မယ်။ နောင်ဆုံးကတော့ Age ကို မှန်လားမှာလားလို့ခွဲမယ်.. ကျွန်တော်က နောက်ကနေ ပြန်ရေးထားသလို ဖြစ်နေတယ်ထင်တယ်... စာရှင်းပြတာလုံးဝမကောင်းတာကို သည်းခံ ပေးကြပါနော်...
ဒါကတော့ အပေါ်က flowchart အတိုင်းပြန်ရေးထားတာ။ ဒါက game ကိုရွေးရတဲ့အပိုင်း မပါသေးဘူး။ 6P မှာတော့ ပြောသွားတယ်။ မမှတ်မိတော့ဘူး 6P ရမှပဲ ပြောတော့မယ်။ ကျွန်တော်ကတော့ ကိုယ့်နည်းကိုယ့်ဟန်နဲ့ ရေးထားတယ်။ ဘယ်လိုလဲဆိုတော့ နောက်ထက် if condition ထက်ထဲ့လိုက်တယ်၊ မရေးပြတော့ဘူး...
ဒါကတော့ သုံးထားတဲ့ formula ပါ။ ဘာလို့ return ကိုသုံးထားလဲဆိုတာတော့ ပြောဖို့မလိုတော့ဘူးထင်တယ်...
Notes :
return ဆိုတာ formula ကို ပြန်သုံးချင်လို့။ loop လုပ်တဲ့အခါ formula ကို တစ်ခါဘဲရေးပြီး ပြန်သုံးတာ။
if {
} ပြီးရင် ; မလိုဘူး
Int ဆိုရင် = or == သုံးလို့ရတယ်
String ကိုဆိုရင်တော့ .equals(), compareTo(), and compare() ကိုသုံးတယ်..read more
equal() ကို capital or small letter မခွဲချင်ဘူးဆိုရင် equalsIgnoreCase ကိုသုံးတယ်။
နောက်ဆုံးပြောရရင်တော့ ဒီ Problem မှာ {if, else if, else} ကိုကောင်းကောင်းသုံးတတ်ရမယ်။ {&& and ||} ကို နားလည်ရမယ်။ ဂွင်း{ } တွေကိုနေရာမှန်အောင်ထားတတ်ရမယ်။ ကျွန်တော်ကတော့ တခါတည်း ဂွင်းစရော"{" ဂွင်းပိတ်ရော"}" တန်းရေးတယ်။ ပြီးရင် Ctrl + Shift + F လုပ်ပြီး ညီအောင်ပြန်စီတယ်။ ဒါမှကြည့်လို့ကောင်းပြီး ရှင်းနေမယ်... condition ကို ";" မလိုဘူး၊ ( ) ဆိုရပြီ။ statement ပြီးရင်တော့ ";"ရေးပေးရတယ်...
ဆက်ပါအုံးမည်....
public class UsingMethod { public void versionA() {
double area1 = 3.142 * 3.0 * 3.0;
double area2 = 3.142 * 3.5 * 3.5;
double area3 = 3.142 * 4.0 * 4.0;
System.out.println("Area of circle 1 is " + area1);
System.out.println("Area of circle 2 is " + area2);
System.out.println("Area of circle 3 is " + area3);
}
public void versionB() {
double area1 = areaOfCirle(3.0);
double area2 = areaOfCirle(3.5);
double area3 = areaOfCirle(4.0);
System.out.println("Area of circle 1 is " + area1);
System.out.println("Area of circle 2 is " + area2);
System.out.println("Area of circle 3 is " + area3);
}
public void versionC() {
System.out.println("Area of circle 1 is " + areaOfCirle(3.0));
System.out.println("Area of circle 2 is " + areaOfCirle(3.5));
System.out.println("Area of circle 3 is " + areaOfCirle(4.0));
}
private double areaOfCirle(double radius) {
double area = 3.142 * radius * radius;
return area;
}
}
Version A, B, C မှာ ဘယ်ဟာက ကောင်းဆုံးလဲ၊ ကျွန်တော်ကတော့ C လို့ ဖြေလိုက်တယ်၊
ပြီးတော့ Multiple Branching အကြောင်းလုပ်ရတယ်။ အဲ့ဒါက if, else if, else ကိုပြောတယ်ဘဲ။
နောက်တစ်ခုက Multiple Conditions အဲ့ဒါကတော့ {or , and} ကိုပြောတာ၊ အသေးစိတ်ကိုတော့ နောက်မှာဆက်ပြောမယ်။ ပြီးတော့ Flowchat တစ်ခုပေးထားတယ် အဲ့ဒါကိုကြည့်ပြီး ကုဒ်ရေးရင်ရပြီ။ flowchart ပေးထားရင် ကုဒ်ရေးရတာ အရမ်းလွယ်သွားပြီလေ...
အပေါ်မှာပြထားတာ ရှင်းရှင်းလေးပဲ.. အရင်ဆုံး true တွေကို if နဲ့ စရေးမယ်။ age, gender, VO2 တွေက မှန်တယ်ဆိုရင် .. Age and VO2 are OK! လို့ ပြလိုက်မယ်။ age and gender က မှန်တယ်၊ VO2 ကမှားမယ်ဆိုရင် VO2 is not within limit လို့ ပြမယ်။ အဲ့လိုပဲ male and female ကိုလည်း ခွဲလိုက်မယ်။ နောင်ဆုံးကတော့ Age ကို မှန်လားမှာလားလို့ခွဲမယ်.. ကျွန်တော်က နောက်ကနေ ပြန်ရေးထားသလို ဖြစ်နေတယ်ထင်တယ်... စာရှင်းပြတာလုံးဝမကောင်းတာကို သည်းခံ ပေးကြပါနော်...
if (age >= 18 && age <= 22) { if (gender.equalsIgnoreCase("male")) { if (v02Max >= 52 && v02Max <= 58) { System.out.println("Your Age and VO2 are Ok!"); } else { System.out.println("VO2 is not within limit"); } } else if (gender.equalsIgnoreCase("female")) { if (v02Max >= 36 && v02Max <= 50) { System.out .println("Your Age and VO2 are Ok!"); } else { System.out .println("You are not qualified to play Game"); } } } else { System.out.println("You are Under Age!"); }
ဒါကတော့ အပေါ်က flowchart အတိုင်းပြန်ရေးထားတာ။ ဒါက game ကိုရွေးရတဲ့အပိုင်း မပါသေးဘူး။ 6P မှာတော့ ပြောသွားတယ်။ မမှတ်မိတော့ဘူး 6P ရမှပဲ ပြောတော့မယ်။ ကျွန်တော်ကတော့ ကိုယ့်နည်းကိုယ့်ဟန်နဲ့ ရေးထားတယ်။ ဘယ်လိုလဲဆိုတော့ နောက်ထက် if condition ထက်ထဲ့လိုက်တယ်၊ မရေးပြတော့ဘူး...
// Using Method (formulae)
private double calcV02Max(int restingHeartRate, int maxHeartRate) {
return 15.0 * maxHeartRate / restingHeartRate;
}
private double calcTrgHeartRate(int maxHeartRate) {
return 0.8 * maxHeartRate;
}
private int calcMaxHeartRate(int age) {
return 220 - age;
}
ဒါကတော့ သုံးထားတဲ့ formula ပါ။ ဘာလို့ return ကိုသုံးထားလဲဆိုတာတော့ ပြောဖို့မလိုတော့ဘူးထင်တယ်...
Notes :
return ဆိုတာ formula ကို ပြန်သုံးချင်လို့။ loop လုပ်တဲ့အခါ formula ကို တစ်ခါဘဲရေးပြီး ပြန်သုံးတာ။
if {
} ပြီးရင် ; မလိုဘူး
if ( <conditions> ) {
statements;
…
}
နောက်တစ်ခုက Int ဆိုရင် = or == သုံးလို့ရတယ်
String ကိုဆိုရင်တော့ .equals(), compareTo(), and compare() ကိုသုံးတယ်..read more
equal() ကို capital or small letter မခွဲချင်ဘူးဆိုရင် equalsIgnoreCase ကိုသုံးတယ်။
နောက်ဆုံးပြောရရင်တော့ ဒီ Problem မှာ {if, else if, else} ကိုကောင်းကောင်းသုံးတတ်ရမယ်။ {&& and ||} ကို နားလည်ရမယ်။ ဂွင်း{ } တွေကိုနေရာမှန်အောင်ထားတတ်ရမယ်။ ကျွန်တော်ကတော့ တခါတည်း ဂွင်းစရော"{" ဂွင်းပိတ်ရော"}" တန်းရေးတယ်။ ပြီးရင် Ctrl + Shift + F လုပ်ပြီး ညီအောင်ပြန်စီတယ်။ ဒါမှကြည့်လို့ကောင်းပြီး ရှင်းနေမယ်... condition ကို ";" မလိုဘူး၊ ( ) ဆိုရပြီ။ statement ပြီးရင်တော့ ";"ရေးပေးရတယ်...
ဆက်ပါအုံးမည်....
In
Labels:
Java
by Han Myo Htwe
Subscribe to:
Comments (Atom)
