为什么这是一个错误?

时间:2014-08-27 00:27:32

标签: java syntax

我在这行“System.out.println((num1 / dem1)(num2 / dem2))”中收到错误;“

错误说:

此行有多个标记      - 作业的左侧必须是变量      - 令牌“)”上的语法错误,AssignmentOperator之后的预期      这个标记

package project;
import java.util.Scanner;

 public class summerproject {
 public static void main(String[] args)  
    {
     Scanner in = new Scanner( System.in );
     {
    //INPUT NUMBERS
     System.out.println("Enter Numerator 1. ");
     int num1 = in.nextInt(); 
     System.out.println("Enter Denominator 1. ");
     int dem1 = in.nextInt();
     System.out.println("Enter Numerator 2. ");
     int num2 = in.nextInt();
     System.out.println("Enter Denominator 2. ");
     int den2 = in.nextInt();
     }
     System.out.println("Press 1 to multiply");
     int mult = in.nextInt();


     if (mult == 1)
     {
         System.out.println((num1/dem1)(num2/dem2));   
     }
    }//ARGS BRACKET      
 }//END BRACKET

2 个答案:

答案 0 :(得分:2)

此行无效:

System.out.println((num1/dem1)(num2/dem2));

(num1/dem1)(num2/dem2)需要在它们之间设置运算符。例如,要将两个表达式相乘,请使用*运算符:

(num1/dem1)*(num2/dem2)

Java的行为与数学不同,串联意味着乘法。相反,您必须明确地将操作数相乘。


此外,您宣布了

int den2 = in.nextInt();

这应该是

int dem2 = in.nextInt().

在适当调整大括号并进行上述修正后,您最终应该:

public class summerproject {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        //INPUT NUMBERS
        System.out.println("Enter Numerator 1. ");
        int num1 = in.nextInt();
        System.out.println("Enter Denominator 1. ");
        int dem1 = in.nextInt();
        System.out.println("Enter Numerator 2. ");
        int num2 = in.nextInt();
        System.out.println("Enter Denominator 2. ");
        int dem2 = in.nextInt();

        System.out.println("Press 1 to multiply");
        int mult = in.nextInt();

        if (mult == 1) {
            System.out.println((num1 / dem1) * (num2 / dem2));
        }
    }
}

示例运行:

Enter Numerator 1. 
20
Enter Denominator 1. 
4
Enter Numerator 2. 
30
Enter Denominator 2. 
5
Press 1 to multiply
1
30

答案 1 :(得分:1)

您需要在操作过程中使用操作员

您的代码似乎表明您想要成倍增加,因此请尝试下面的代码......

System.out.println((num1/dem1)*(num2/dem2));