androidstudio随机数加减乘除
时间: 2023-06-30 11:20:50 浏览: 70
您可以使用Java中的Random类来生成随机数,然后使用算术运算符来执行加减乘除操作。以下是一个示例代码:
```java
import java.util.Random;
public class RandomNumberGenerator {
public static void main(String[] args) {
Random random = new Random();
int num1 = random.nextInt(100); // 生成0-99之间的随机整数
int num2 = random.nextInt(100);
int result1 = num1 + num2;
int result2 = num1 - num2;
int result3 = num1 * num2;
int result4 = num1 / num2;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
System.out.println("num1 + num2 = " + result1);
System.out.println("num1 - num2 = " + result2);
System.out.println("num1 * num2 = " + result3);
System.out.println("num1 / num2 = " + result4);
}
}
```
这段代码将生成两个随机数,然后执行加减乘除操作,并打印结果。注意,如果除数为0,则会抛出异常。您可以使用异常处理来避免这种情况。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)