Java 可变长度参数赋值的语法问题
我正在尝试编写一个Java程序,该程序将计算一系列整数的乘积,这些整数使用可变长度参数列表传递给方法product。该方法必须通过几个调用进行测试,每个调用都有不同数量的参数 我已经尽我所能编写了代码,但无法让它编译无误,也无法理解我做错了什么。我怀疑我有什么不正常的事情,但就是不知道是什么,我希望得到一些建议。这是我在学校的Java开发课作业的一部分Java 可变长度参数赋值的语法问题,java,arguments,Java,Arguments,我正在尝试编写一个Java程序,该程序将计算一系列整数的乘积,这些整数使用可变长度参数列表传递给方法product。该方法必须通过几个调用进行测试,每个调用都有不同数量的参数 我已经尽我所能编写了代码,但无法让它编译无误,也无法理解我做错了什么。我怀疑我有什么不正常的事情,但就是不知道是什么,我希望得到一些建议。这是我在学校的Java开发课作业的一部分 public class VLArgs { //calculates the product public static in
public class VLArgs
{
//calculates the product
public static int product(int...numbers)
{
int product = 1;
//multiplies the integers
for (int number:numbers)
{
product *= number;
}
return product;
}
public static void main(String[] args)
{
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
//displays the values
System.out.printf(“a = %d, b = %d, c = %d, d = %d, e = %d\n”,
a, b, c, d, e);
//calls the product of the values with a different number of arguments in each call
System.out.printf(“The product of a and b is: %d\n”,product(a, b));
System.out.printf(“The product of a, b and c is: %d\n”,product(a, b, c));
System.out.printf(“The product of a, b, c and d is: %d\n”, product(a, b, c, d));
System.out.printf(“The product of a, b, c, d and e is: %d\n”, product(a, b, c, d, e));
}
}
我收到的主要错误是:
线程“main”java.lang中出现异常。错误:未解决的编译问题:
在VLArgs.main(VLArgs.java:31)
其中的所有错误都是语法错误(大约35个)。打印语句中有一个问题。它使用了一些未知的字符格式 我已经按照你的要求更新了代码。将它与您的代码匹配,您就会理解它 对于任何问题,请对此答案进行评论
public class VLArgs {
//calculates the product
public static int product(int... numbers) {
int product = 1;
//multiplies the integers
for (int number : numbers) {
product *= number;
}
return product;
}
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
//displays the values
System.out.printf("The product of a and b is: %d%n", product(a, b));
System.out.printf("The product of a, b and c is: %dn", product(a, b, c));
System.out.printf("The product of a, b, c and d is: %dn", product(a, b, c, d));
System.out.printf("The product of a, b, c, d and e is: %dn", product(a, b, c, d, e));
}
}
您的代码和逻辑似乎完全正确。一个问题似乎出现在
“
中。只要将其更改为”
,它就会工作。我知道这听起来很愚蠢,但不同的编译器对Unicode符号的解释不同。您可以看到这一点。这正是问题所在,通过在IDE中手动更改引号,它现在可以编译并运行。