//布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.number.MainActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/num" android:hint="输入阶乘数" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/start" android:text="开始阶乘" /> <TextView android:id="@+id/textname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/textname1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/textname2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/textname3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/textname4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/textname5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/textname6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </LinearLayout> </ScrollView> </LinearLayout> //主要代码package com.bwie.number; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.math.BigInteger; import java.util.ArrayList; import java.util.Random; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText editText = findViewById(R.id.num); Button button = findViewById(R.id.start); TextView textView = findViewById(R.id.textname); TextView textView1 = findViewById(R.id.textname1); final TextView textView2 = findViewById(R.id.textname2); TextView textView3 = findViewById(R.id.textname3); TextView textView4 = findViewById(R.id.textname4); TextView textView5 = findViewById(R.id.textname5); TextView textView6 = findViewById(R.id.textname6); TextView textView7 = findViewById(R.id.textname7); TextView textView8 = findViewById(R.id.textname8); textView.setText("0.杨辉三角\n" + get(1, 5)); textView1.setText("1.杨辉三角\n" + get(2, 8)); textView3.setText("3.递归10的阶乘:\n " + digui(10)); textView4.setText("4.12个月后兔子对数:\n " + feibo(12)); textView5.setText("5.无序的二维数组\n" + erwei(4, 0)); textView6.setText("6.行有序的二维数组\n" + erwei(4, 1)); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String num=editText.getText().toString(); //textView2.setText("2.大数阶乘:\n" + digui(new BigInteger(num))); textView2.setText("2.大数阶乘:\n"+jiecheng(Integer.parseInt(num))); } }); } //二维数组 public String erwei(int num, int type) { String a = ""; int[][] arr = new int[num][num]; for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { Random rand = new Random(); int ran = rand.nextInt(num * num) + 1; arr[i][j] = feibo(ran); } } switch (type) { case 0://无序 for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { a += arr[i][j] + " "; } a += "\n"; } break; case 1://行有序 for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { int min = arr[i][j]; for (int m = j; m < num; m++) { if (arr[i][m] < min) { min = arr[i][m]; arr[i][m] = arr[i][j]; arr[i][j] = min; } } } } for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { a += arr[i][j] + " "; } a += "\n"; } break; } return a; } //递归 public int digui(int length) { int num = length; if (num == 1) { return 1; } else { return num * digui(num - 1); } } //递归2 public BigInteger digui(BigInteger length) { BigInteger num = length; BigInteger big1 = BigInteger.valueOf(1); if (num.compareTo(big1) == 0) { return big1; } else { return num.multiply(digui(num.subtract(big1))); } } //大数阶乘 private BigInteger jiecheng(int n) { BigInteger a=BigInteger.valueOf(n); for (int i=1;i<=n;i++){ a = a.multiply(BigInteger.valueOf(i)); } return a; } //斐波那契 public int feibo(int moth) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 1; i <= moth; i++) { if (i == 1 || i == 2) { list.add(i - 1, 1); } else { list.add(i - 1, list.get(i - 2) + list.get(i - 3)); } } return list.get(moth - 1); } //杨辉三角 public String get(int type, int count) { String arr = ""; int num[][] = new int[count][count];//这个数组有几层 for (int i = 0; i < count; i++)//主要是对数组进行赋值 { for (int j = 0; j <= i; j++)//每一层的个数都是小于等于层数的,m代表层数,n代表着第几个数 { if (j == 0 || i == j)//每一层的开头都是1,i==n的时候也是1,必须要这个,凡事都得有个开头 { num[i][j] = 1; } else num[i][j] = num[i - 1][j - 1] + num[i - 1][j];//这个就是递推的方法了,例如3=1+2,3的坐标就是3[3,1]=1[2,0]+2[2,1]; } } for (int i = 0; i < count; i++)//主要是输出数组 { if (type == 1) { for (int l = i; l < count; l++)//这个主要是打空格,好看一点,去掉就是直角三角形了 { arr = arr + " "; } } for (int j = count - i; j <= count; j++)//这个就是打印数组了,每层循环几次就几个 { if (num[i][count - j] < 8 || i == 2) { arr = arr + num[i][count - j] + " "; } else { arr = arr + num[i][count - j] + " "; } } arr = arr + "\n"; } return arr; } }