输入两个数 输出一个最大的数
时间: 2024-05-09 18:19:47 浏览: 16
请问您需要使用哪种编程语言实现呢?以下是 Python 和 Java 两种语言的示例代码。
Python:
```
a = int(input("请输入第一个数:"))
b = int(input("请输入第二个数:"))
if a > b:
print("最大的数是:", a)
else:
print("最大的数是:", b)
```
Java:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数:");
int a = scanner.nextInt();
System.out.print("请输入第二个数:");
int b = scanner.nextInt();
if (a > b) {
System.out.println("最大的数是:" + a);
} else {
System.out.println("最大的数是:" + b);
}
}
}
```
相关问题
输入两个数 输出最大的
好的,您可以使用以下 Python 代码实现:
```python
a = float(input("请输入第一个数:"))
b = float(input("请输入第二个数:"))
if a > b:
print("最大的数是:", a)
else:
print("最大的数是:", b)
```
这段代码会先让用户输入两个数字,然后判断哪个数字更大,并输出结果。
java输入两个数输出最大公约数
答案:可以使用辗转相除法求出两个数的最大公约数。具体步骤如下:
1. 用较大数除以较小数,得到余数
2. 把较小数作为被除数,余数作为除数,再次进行相除
3. 重复上述过程,直到余数为0为止
4. 此时,较小数即为最大公约数
示例代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int gcd = getGcd(a, b);
System.out.println(gcd);
}
private static int getGcd(int a, int b) {
if (b == 0) {
return a;
}
return getGcd(b, a % b);
}
}
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)