您好我刚开始学习java并且遇到一些非常基本的计算问题。这个简单的程序要求用户输入两个值,然后当它运行时我希望它显示“20 + 5 = 25”。但是,我在行"+" num2 "=" num1
的这一部分上反复收到“语法错误,删除此令牌”消息。有什么问题?
package ner.cs1451;
import java.util.Scanner;
public class Project01a {
/*This program asks the user to input two values
*Then computes the following: sum, difference, product, quotient, remainder, and the average.*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = in.nextInt();
System.out.print("Enter second number: ");
int num2 = in.nextInt();
int avg = (num1 + num2)/2;
System.out.println(num1 "+" num2 "=" num1 + num2);
System.out.println(num1 "-" num2 "=" num1 - num2);
System.out.println(num1 "*" num2 "=" num1 * num2);
System.out.println(num1 "/" num2 "=" num1 / num2);
System.out.println(num1 "%" num2 "=" num1 % num2);
System.out.println("The average of your two numbers is: "+avg);
答案 0 :(得分:1)
您在尝试连接的所有值之间缺少连接运算符+
。 E.g。
System.out.println(num1 + "+" + num2 + "=" + (num1 + num2));
您还希望用括号括起加法,以便在数字之间进行正常加法,而不是连接。
答案 1 :(得分:1)
将字符串连接起来:
System.out.println(num1 + "+" + num2 + "=" + (num1 + num2));