Android Studioni o'rnatish, kalkulyator dasturini yaratish Android Studionni Windows ga o’rnatish


Download 0.73 Mb.
bet4/4
Sana14.05.2023
Hajmi0.73 Mb.
#1459849
1   2   3   4
Bog'liq
jPVq5HI04MVm3 8ydjGNOY3HZZ3Ac6dX

Kalkulyator tartibini yaratish

Kalkulyator sxemasi uzoq. Buning sababi shundaki, biz kalkulyatordagi har bir tugmani aniq belgilashimiz va ehtiyotkorlik bilan joylashtirishimiz kerak. Quyidagi parcha faollik_asosiy tartib faylining qisqartirilgan versiyasidir
...
Kalkulyatorning ichki qismlari
Kalkulyatorimiz ikkita qiymatga ega: valueOne va valueTwo. Ushbu qiymatlar ustida ishlash uchun raqamlar mavjud. Ikkala qiymat ham double turiga ega, shuning uchun ular o'nli yoki kasrsiz raqamlarni o'z ichiga olishi mumkin. Quyida aniqroq bo'ladigan sabablarga ko'ra valueOne turini Double NaN (raqam emas) turidagi maxsus qiymatga o'rnatamiz.
xususiy double valueOne = Double.NaN; xususiy juft qiymatIkki;
Ushbu oddiy kalkulyator faqat qo'shish, ayirish, ko'paytirish yoki bo'lish amallarini bajara oladi. Shunday qilib, biz ushbu operatsiyalarni ifodalash uchun to'rtta statik belgilarni va biz bajaradigan keyingi operatsiyani o'z ichiga olgan CURRENT_ACTION o'zgaruvchisini aniqlaymiz.
xususiy statik yakuniy belgi ADDITION = '+'; xususiy statik yakuniy belgi SUBTRACTION = '-'; xususiy statik yakuniy belgi MULTIPLICATION = '*'; xususiy statik yakuniy belgi DIVISION = '/'; shaxsiy belgi CURRENT_ACTION;
Nihoyat, kalkulyatorimiz chiqishini formatlash uchun DecimalFormat sinfidan foydalanamiz. O'nlik format konstruktori o'ntagacha kasrlarni ko'rsatishga imkon beradi.
decimalFormat = yangi DecimalFormat("#.##########");
Tugmachalarni bosish (raqamlar)
Foydalanuvchi har safar raqamni (yoki nuqtani) bosganida, biz bu raqamni tahrir matniga qo'shishni xohlaymiz. Quyidagi kod misolida nol (0) raqami uchun buni qanday qilish ko'rsatilgan.
binding.buttonZero.setOnClickListener(yangi View.OnClickListener() {@Override public void onClick(ViewView) { binding.editText.setText(binding.editText.getText()+"0");}});
Tugmani bosish bilan ishlash (operatorlar)

Operator bosish bilan ishlash biroz boshqacha. Avval kutilayotgan hisob-kitoblarni amalga oshirmoqchimiz. Shuning uchun biz computeCalculation usulini aniqlaymiz. ComputeCalculation-da, valueOne haqiqiy son bo'lsa, biz editText-dan valueTwo-ni o'qiymiz va joriy navbatdagi operatsiyani bajaramiz. Boshqa tomondan, valueOne NaN bo'lsa, valueOne tahrir matniga raqam tayinlaydi.
private void co mputeCalculation() { if(!double.isNaN(valueOne)) { valueTwo = Double.parseDouble(binding.editText.getText().toString()); binding.editText.setText(null); agar (CURRENT_ACTION == ADDITION) valueOne = this.valueOne + valueTwo; boshqacha bo'lsa (CURRENT_ACTION == AYRISH) valueOne = this.valueOne - valueTwo; Aks holda (CURRENT_ACTION == MULTIPLICATION) valueOne = this.valueOne * valueTwo; boshqacha bo'lsa (CURRENT_ACTION == BO'LISH) valueOne = this.valueOne/valueTwo; } else { try { valueOne = Double.parseDouble(binding.editText.getText().toString()); } catch (e istisno) {}}}
Har bir bayonot uchun biz avval computeCalculation() ni chaqiramiz, so'ngra tanlangan bayonotda CURRENT_ACTION ni o'rnatamiz va '=' tenglik operatori uchun computeCalculation() deb nomlaymiz, so'ngra valueOne va CURRENT_ACTION tarkibini tozalaymiz.
binding.buttonAdd.setOnClickListener(yangi View.OnClickListener(){@Override public void onClick(View view){computeCalculation(); CURRENT_ACTION = QO'SHISH; binding.infoTextView.setText(decimalFormat.format(valueOne); binding)+" .editText.setText(null);}}); binding.buttonSubtract.setOnClickListener(yangi View.OnClickListener() {@Override public void onClick(View View) { computeCalculation(); CURRENT_ACTION = SUBTRACTION; binding.infoTextView.setText(decimalFormat.format(valueOne.set)); );}}); binding.buttonMultiply.setOnClickListener(yangi View.OnClickListener(){@Override public void onClick(View view){computeCalculation();CURRENT_ACTION = MULTIPLICATION;binding.infoTextView.setText binding(decimalFormatne) )) .editText.setText(null);}});binding.buttonDivide. setOnClickListener(new View.OnClickListener() {@Override public void onClick(View View) { computeCalculation(); CURRENT_ACTION = SECTION; binding.infoTextView.setText(decimalFormat.format(valueOne) + "/"); binding.setTextT (nol); }}); binding.buttonEqual.setOnClickListener(yangi View.OnClickListener() {@Override public void onClick(View view) {computeCalculation(); binding.infoTextView.setText(binding.infoTextView.getText().toString()+decatimalFormat)(valid) + "=" + decimalFormat.format(valueOne)); valueOne = Double.NaN; CURRENT_ACTION = '0';}}); editText.setText(null); }}); binding.buttonEqual.setOnClickListener(yangi View.OnClickListener() {@Override public void onClick(View view) {computeCalculation(); binding.infoTextView.setText(binding.infoTextView.getText().toString()+decatimalFormat)(valid) + "=" + decimalFormat.format(valueOne)); valueOne = Double.NaN; CURRENT_ACTION = '0';}}); editText.setText(null); }}); binding.buttonEqual.setOnClickListener(yangi View.OnClickListener() {@Override public void onClick(View view) {computeCalculation(); binding.infoTextView.setText(binding.infoTextView.getText().toString()+decatimalFormat)(valid) + "=" + decimalFormat.format(valueOne)); valueOne = Double.NaN; CURRENT_ACTION = '0';}});
Tabriklaymiz! Siz oddiy android kalkulyator ilovasini tugatdingiz.
Download 0.73 Mb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling