输入三个整数,把这三个整数按从java 大到小的顺序输出。方法不限 点击复制后,将打开C知道体验页
时间: 2024-04-30 19:23:22 浏览: 14
以下是一种Java语言实现的方法:
```java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int a = 3, b = 1, c = 2;
int[] arr = {a, b, c};
Arrays.sort(arr);
for (int i = 2; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
```
输出结果为:
```
3 2 1
```
首先将三个整数存储在数组中,然后使用Java内置的`Arrays.sort()`方法对数组进行排序,从小到大排序后,从数组的末尾开始循环输出即可得到从大到小的顺序。
相关问题
我要用java的方法来写输入三个整数,把这三个整数按从大到小的顺序输出。方法不限
好的,以下是使用Java实现输入三个整数,将它们按从大到小的顺序输出的代码示例:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
scanner.close();
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
if (a > b) {
int temp = a;
a = b;
b = temp;
}
System.out.println(c + " " + b + " " + a);
}
}
```
你可以在控制台输入三个整数,例如 `5 3 9`,程序会将它们按从大到小的顺序输出,即 `9 5 3`。
希望这个示例能够帮助到你!
用java写一个关于Android 的检验类:1、 edittext限制两位小数,且数值不超过600;2、edittext限制输入三位整数且小于240;
可以参考下面的代码实现:
```java
public class AndroidInputValidator {
// 检验edittext输入的数值是否符合要求
public boolean validateEditText(EditText editText, int type) {
String input = editText.getText().toString().trim();
if (TextUtils.isEmpty(input)) {
return false;
}
if (type == 1) {
// 限制两位小数,且数值不超过600
double value = Double.parseDouble(input);
if (value >= 0.01 && value <= 600) {
String regex = "^\\d+(\\.\\d{1,2})?$";
return input.matches(regex);
}
} else if (type == 2) {
// 限制输入三位整数且小于240
int value = Integer.parseInt(input);
if (value >= 0 && value < 240) {
String regex = "^[0-9]{1,3}$";
return input.matches(regex);
}
}
return false;
}
}
```
使用方式:
```java
EditText editText1 = findViewById(R.id.editText1);
EditText editText2 = findViewById(R.id.editText2);
AndroidInputValidator validator = new AndroidInputValidator();
// 检验editText1是否符合要求,type为1表示限制两位小数,且数值不超过600
if (validator.validateEditText(editText1, 1)) {
// 符合要求
} else {
// 不符合要求,做相应处理
}
// 检验editText2是否符合要求,type为2表示限制输入三位整数且小于240
if (validator.validateEditText(editText2, 2)) {
// 符合要求
} else {
// 不符合要求,做相应处理
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)