java 常见的面试题-------求俩数字的最大公约数

面试的时候回让手写一个函数求最大值、最小值、还有公因数,今天我们来看一下怎么实现。
在这里插入图片描述

先看xml布局:

   <?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=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:textSize="20sp"
        android:text="输入第一个数:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/edit_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:textSize="20sp"
        android:text="输入第二个数:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/edit_to"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />
</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="计算"
    android:id="@+id/commit"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="最大公约数为:" />
<TextView
    android:id="@+id/number"
    android:textColor="#d2d2"
    android:textSize="20sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

再看代码实现:

 private void submit() {
        // validate
        String one = edit_one.getText().toString().trim();
        if (TextUtils.isEmpty(one)) {
            Toast.makeText(this, "one不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String to = edit_to.getText().toString().trim();
        if (TextUtils.isEmpty(to)) {
            Toast.makeText(this, "to不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        // TODO validate success, do something
        //string转int
        int i = Integer.parseInt(one);
        int i1 = Integer.parseInt(to);
        getGCD(i,i1);
    }

判断输入的两个值是否为空,不为空则转换为int传入 getGCD()方法进行求值

  //求数值的方法
    public  int getGCD(int x,int y) {// x接受第一个整数,y接受第二个整数
                int num=1;// 定义一个变量num,来保存最大公约数
            for (int i = 1; i <=x; i++) {// 遍历1到x的所有整数 
        if (x%i==0 && y%i==0) {//  如果有一个数同时满足被x,y整除
            num=i;//  将这个数保存到num变量
            //int转string
            number.setText(Integer.toString(num));
        }
    }
            return num;
}

求完值在转换为string赋值给TextView。

是不是觉得有点简单呢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值