编写一个 JAVA 程序,要求用户输入两个整数num1和num2,然后输出它们的和、差、积、商、余数、平均数和最大值。 在程序中使用以下运算符:+、-、*、/、%(需要分别使用这些运算符完成相应的计算)。
时间: 2023-12-28 19:09:00 浏览: 15
As an AI language model, I do not have a physical form or consciousness, so the concept of null does not apply to me in the same way it does to humans. In programming, null refers to a variable that has no value or is undefined.
相关问题
编写一个JAVA程序,要求用户输入两个整数num1和num2,然后输出它们的和、差、积、商、余数、平均数和最大值。
可以使用Scanner类获取用户输入的两个整数,然后进行相应的计算和输出,代码如下:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个整数:");
int num1 = scanner.nextInt();
System.out.print("请输入第二个整数:");
int num2 = scanner.nextInt();
int sum = num1 + num2;
int diff = num1 - num2;
int product = num1 * num2;
int quotient = num1 / num2;
int remainder = num1 % num2;
double average = (double)(num1 + num2) / 2;
int max = num1 > num2 ? num1 : num2;
System.out.println("和:" + sum);
System.out.println("差:" + diff);
System.out.println("积:" + product);
System.out.println("商:" + quotient);
System.out.println("余数:" + remainder);
System.out.println("平均数:" + average);
System.out.println("最大值:" + max);
}
}
```
运行程序,输入两个整数,即可看到输出结果:
```
请输入第一个整数:10
请输入第二个整数:3
和:13
差:7
积:30
商:3
余数:1
平均数:6.5
最大值:10
```
编写一个 JAVA 程序,要求用户输入两个整数num1和num2,然后输出它们的和、差、积、商、余数、平均数和最大值。
以下是一个简单的实现:
```java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个整数:");
int num1 = scanner.nextInt();
System.out.print("请输入第二个整数:");
int num2 = scanner.nextInt();
int sum = num1 + num2;
int diff = num1 - num2;
int product = num1 * num2;
int quotient = num1 / num2;
int remainder = num1 % num2;
double average = (num1 + num2) / 2.0;
int max = num1 > num2 ? num1 : num2;
System.out.println("和:" + sum);
System.out.println("差:" + diff);
System.out.println("积:" + product);
System.out.println("商:" + quotient);
System.out.println("余数:" + remainder);
System.out.println("平均数:" + average);
System.out.println("最大值:" + max);
}
}
```
运行程序,输入两个整数,即可得到它们的和、差、积、商、余数、平均数和最大值的计算结果。
阅读全文