[ Android ] Xây dựng ứng dụng nhập vào số nguyên n; tính giai thừa của n, in kết quả.


Vi dụ: Xây dựng ứng dụng nhập vào số nguyên n; tính n!; in kết quả.

>> Thực hiện: 

- Create Android Project: TinhGiaiThua




- Thiết lập và code:

+ Res\layout\main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    
       

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/n" />

        <EditText
            android:id="@+id/txtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.12" 
            android:hint="Input"
            android:inputType="text|textAutoCorrect|textCapWords|number"
            />

           <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.12"
            android:text="Tinh" />
        
    <TextView
        android:id="@+id/kq"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/kq" />
    
    

</LinearLayout>

+ Res\value\string.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Nhap so can tinh</string>
    <string name="app_name">Tính giai thừa</string>
    <string name="kq">Kết quả:</string><string name="n">N:</string>
    

</resources>

+ rsc\...

package txt.Begin.TinhGiaiThua;


import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;
public class TinhGiaiThuaActivity extends Activity {     // Phuong thuc tinh giai thua public int GiaiThua(int a){ int gt=1; for(int i=1;i<=a;i++) gt=gt*i; return gt; } // onCreate     public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);        setContentView(R.layout.main);               final Button tinh=(Button) findViewById(R.id.button1);        final TextView ketqua=(TextView) findViewById(R.id.kq);                     // xu ly nut button        tinh.setOnClickListener(new Button.OnClickListener(){        public void onClick(View v) {        String so=((EditText) findViewById(R.id.txtn)).getText().toString();        int n=Integer.parseInt(so);        try
{        int s=GiaiThua(n);        ketqua.setText("");        ketqua.setText(ketqua.getText()+""+(s));         } catch (Exception ex)
{
ketqua.setText(ex.toString());
}        }                });                   }}


>> Kết quả:




*******

Một số tài liệu và khoá học bổ ích dành cho bạn: 

# Giáo trình: Lập Trình Android [Click để xem]

# Khoá học online:  Lập trình Android toàn tập [Click để xem]

-----------------------------------------

Categories

AI (13) AI programming (1) ASP (1) Android (32) App Honeygain (4) Assembly (17) Biểu diễn thuật toán (1) Bubble-Sort (1) Bài giảng (2) Bài giảng lập trình C và Cpp (21) Bài viết hay (108) Bản đồ tư duy (1) C Plus Plus (103) C/C++ (16) CDSL phân tán (1) CSS (2) Cơ sở dữ liệu (11) Danh ngôn lập trình (1) Datamining (4) Genetic Algorithm (1) Giáo trình (2) Giải thuật tiến hóa - thuật toán di truyền (2) Google App Engine (2) Góc học tập (34) HTML (1) Hướng dẫn kiếm tiền online tại nhà (6) Hướng dẫn sử dụng Emu8086 (1) Học lập trình (131) Học lập trình C và CPP qua ví dụ (17) Java (54) Java Căn bản (6) JavaScript (5) Kỹ năng đọc hiệu quả (1) Kỹ thuật lập trình (16) Kỹ thuật đồ họa máy tính (10) Lý thuyết Cơ sở dữ liệu (2) Lý thuyết đồ thị (11) Lập trình Cơ sở dữ liệu (2) Lập trình Python (2) Lập trình căn bản (8) Lập trình hướng đối tượng với Java (7) Lập trình mobile (9) Lập trình mạng (6) Lập trình nhúng (1) Lập trình trí tuệ nhân tạo (2) ML (1) MMO (6) MS Access (1) Machine learning (2) Mạng máy tính (1) Mẹo tìm kiếm trên Google (1) Nghiên cứu khoa học (3) Ngôn ngữ lập trình (2) Những cuốn sách hay mà bạn nên đọc khi còn trẻ (1) Pascal (3) Phương pháp tính toán tối ưu (2) Phương pháp tối ưu (2) Quản lý dự án CNTT (1) SEO (1) SQL (5) Swift (9) Sách hay (4) Thiết kế Web (2) Thuật toán (51) Thuật toán Sắp Xếp -Sort (9) Thuật toán Tìm kiếm - Search (5) Thuật toán di truyền (4) Thực hành Android (2) Tin học văn phòng (5) Tiện ích máy tính (3) Toán rời rạc (13) Treo máy kiếm tiền (3) Trí tuệ nhân tạo (18) Tài liệu tham khảo (4) Tìm hiểu Blockchain (2) Tự học Android (3) Tự học Android qua ví dụ (1) Tự học JavaScript (1) Tự học lập trình (9) Tự học lập trình Android (17) Tự học lập trình C và CPP (14) Tự học lập trình java qua các ví dụ (8) XML (1) blockchain (2) bài giảng quản lý dự án CNTT (1) bài tập java (3) bài tập lập trình (4) cấu trúc dữ liệu giải thuật (15) hướng dẫn viết báo (1) học lập trình Java (11) học máy (5) hợp ngữ (8) lập trình viên (3) phưng pháp đơn hình (2) thuật toán AI (2) tài liệu CNTT miễn phí (3) tính toán tối ưu (1) tự học lập trình iOS (8) tự học lập trình python (1) ví dụ Assembly (1) Đại số gia tử và ứng dụng (1) Đồ họa (4)