gpt4 book ai didi

java - 为什么这是一个错误?

转载 作者:行者123 更新时间:2023-12-01 22:44:57 24 4
gpt4 key购买 nike

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

错误提示:

该行有多个标记 - 赋值的左边必须是变量 - 标记“)”上的语法错误,AssignmentOperator 预计在之后 这个 token

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

最佳答案

该行无效:

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

关于java - 为什么这是一个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25517141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com