我正在尝试打印出用户输入的数字,但我不知道(例如:当用户输入2和3时,它应该显示2 + 3 = 5而不是5单独)也如何找到平均值数字也是。 这是我的代码:
public static void main(String[] args) {
Scanner keyboard= new Scanner (System.in);
System.out.print("Please enter your first number: ");
int firstnumber= keyboard.nextInt();
System.out.print("Please enter the second number: ");
int secondnumber= keyboard.nextInt();
int sum= firstnumber+secondnumber;
System.out.println(sum);
int minus= firstnumber-secondnumber;
System.out.println(minus);
int multiply= firstnumber*secondnumber;
System.out.println(multiply);
int divide= firstnumber/secondnumber;
System.out.println(divide);
int moddivide= firstnumber%secondnumber;
System.out.println(moddivide);
**int average= sum/2;**
System.out.println(average);
keyboard.close();
}
}
你可以看到(明星)(明星).....(明星)(明星)它不应该是2,因为它看起来并不像一个专业人士......
答案 0 :(得分:1)
您可以在此处使用格式化输出:
System.out.printf("%d + %d = %d\n",firstnumber,secondnumber,sum);//<-- prints `3 + 4 = 7`
格式:
%d, integers
%s, string
%b, boolean
%f, floating
以小数计算平均值:
double average = sum / 2.0;
System.out.printf("Avg of %d & %d = %f\n",firstnumber,secondnumber,average);
答案 1 :(得分:0)
这是您正在寻找的代码:
System.out.println(firstnumber + " + " + secondnumber + " = " + sum);
平均代码:
double average = (double) sum / 2;
System.out.println(average);