java: 输入 int[] 时表达式的非法开始
java: illegal start of expression when entering int[]
我无法弄清楚我的输入参数有什么错误,编译错误消息没有多大帮助。
import java.util.ArrayList;
/**
* Created by cheun on 11/8/2017.
*/
public class Problem2_ProductsOfAllIntsExceptAtIndex {
static int pointer;
static int[] arr;
static int[] temp_arr;
static int input;
public static int[] myFunction(int[] arg) {
// write the body of your function here
for (int i = 0; i <= arg.length; i++) {
pointer = i;
temp_arr = arg;
temp_arr[i] = 1; // or should put it null
for (int j = 0; i <= arr.length; i++) {
input *= temp_arr[j];
}
arr[pointer] = input;
}
return arr;
}
public static void main(String[] args) {
Problem2_ProductsOfAllIntsExceptAtIndex solution = new Problem2_ProductsOfAllIntsExceptAtIndex();
// line 32 FAULTY LINE BELOW
System.out.println(myFunction([0, 1, 2, 3]));
// line 32 FAULTY LINE ABOVE
}
}
D:\java_workspace\InterviewQuestions\com.cake.interviews\src\Problem2_ProductsOfAllIntsExceptAtIndex.java
Error:(32, 39) java: illegal start of expression
Error:(32, 40) java:
')' expected Error:(32, 41) java: ';' expected
Error:(32, 43) java: not a statement
Error:(32, 44) java: ';' expected
1.arr未实例化->空指针异常
2.If 你想输入一个带有数字的数组使用这个:
System.out.println(myFunction(new int[]{0, 1, 2, 3}));
或者您可以将我的函数定义更改为
myFunction(int...arg) 并给出由逗号分隔的参数
示例:
public class Problem2_ProductsOfAllIntsExceptAtIndex {
static int pointer;
static int[] arr= new int[4];
static int[] temp_arr;
static int input;
public static int[] myFunction(int[] arg) {
// write the body of your function here
for (int i = 0; i <= arg.length; i++) {
pointer = i;
temp_arr = arg;
temp_arr[i] = 1; // or should put it null
for (int j = 0; i <= arr.length; i++) {
input *= temp_arr[j];
}
arr[pointer] = input;
}
return arr;
}
public static void main(String[] args) {
Problem2_ProductsOfAllIntsExceptAtIndex solution = new Problem2_ProductsOfAllIntsExceptAtIndex();
// line 32 FAULTY LINE BELOW
System.out.println(Arrays.toString(myFunction(new int[]{0, 1, 2, 3})));
// line 32 FAULTY LINE ABOVE
}
}
您的程序中有几处错误。我强烈建议学习 Java 语言的基础知识。互联网上有很多免费资源。这是 Java Tutorials website where you could start. And here 你可以直接转到 Java 中的数组。
话虽如此,以下可能是您想要实现的目标:
public class Problem2_ProductsOfAllIntsExceptAtIndex {
public static int[] myFunction(int[] arg) {
// making all variables global (=> static) doesn't make sense in this case
int input = 1;
int[] temp_arr;
int[] arr ;
arr = new int[arg.length];
for (int i = 0; i < arg.length; i++) {
temp_arr = Arrays.copyOf(arg, arg.length); // clone the original array; otherwise you overwrite the original one
temp_arr[i] = 1; // or should put it null
for (int j = 0; j < temp_arr.length; j++) {
input *= temp_arr[j];
}
arr[i] = input;
input = 1; // reset
}
return arr;
}
public static void main(String[] args) {
int [] arr = myFunction(new int[] { 0, 1, 2, 3 });
System.out.println(Arrays.toString(arr));
}
}
我无法弄清楚我的输入参数有什么错误,编译错误消息没有多大帮助。
import java.util.ArrayList;
/**
* Created by cheun on 11/8/2017.
*/
public class Problem2_ProductsOfAllIntsExceptAtIndex {
static int pointer;
static int[] arr;
static int[] temp_arr;
static int input;
public static int[] myFunction(int[] arg) {
// write the body of your function here
for (int i = 0; i <= arg.length; i++) {
pointer = i;
temp_arr = arg;
temp_arr[i] = 1; // or should put it null
for (int j = 0; i <= arr.length; i++) {
input *= temp_arr[j];
}
arr[pointer] = input;
}
return arr;
}
public static void main(String[] args) {
Problem2_ProductsOfAllIntsExceptAtIndex solution = new Problem2_ProductsOfAllIntsExceptAtIndex();
// line 32 FAULTY LINE BELOW
System.out.println(myFunction([0, 1, 2, 3]));
// line 32 FAULTY LINE ABOVE
}
}
D:\java_workspace\InterviewQuestions\com.cake.interviews\src\Problem2_ProductsOfAllIntsExceptAtIndex.java Error:(32, 39) java: illegal start of expression Error:(32, 40) java: ')' expected Error:(32, 41) java: ';' expected Error:(32, 43) java: not a statement Error:(32, 44) java: ';' expected
1.arr未实例化->空指针异常
2.If 你想输入一个带有数字的数组使用这个:
System.out.println(myFunction(new int[]{0, 1, 2, 3}));
或者您可以将我的函数定义更改为 myFunction(int...arg) 并给出由逗号分隔的参数
示例:
public class Problem2_ProductsOfAllIntsExceptAtIndex {
static int pointer;
static int[] arr= new int[4];
static int[] temp_arr;
static int input;
public static int[] myFunction(int[] arg) {
// write the body of your function here
for (int i = 0; i <= arg.length; i++) {
pointer = i;
temp_arr = arg;
temp_arr[i] = 1; // or should put it null
for (int j = 0; i <= arr.length; i++) {
input *= temp_arr[j];
}
arr[pointer] = input;
}
return arr;
}
public static void main(String[] args) {
Problem2_ProductsOfAllIntsExceptAtIndex solution = new Problem2_ProductsOfAllIntsExceptAtIndex();
// line 32 FAULTY LINE BELOW
System.out.println(Arrays.toString(myFunction(new int[]{0, 1, 2, 3})));
// line 32 FAULTY LINE ABOVE
}
}
您的程序中有几处错误。我强烈建议学习 Java 语言的基础知识。互联网上有很多免费资源。这是 Java Tutorials website where you could start. And here 你可以直接转到 Java 中的数组。
话虽如此,以下可能是您想要实现的目标:
public class Problem2_ProductsOfAllIntsExceptAtIndex {
public static int[] myFunction(int[] arg) {
// making all variables global (=> static) doesn't make sense in this case
int input = 1;
int[] temp_arr;
int[] arr ;
arr = new int[arg.length];
for (int i = 0; i < arg.length; i++) {
temp_arr = Arrays.copyOf(arg, arg.length); // clone the original array; otherwise you overwrite the original one
temp_arr[i] = 1; // or should put it null
for (int j = 0; j < temp_arr.length; j++) {
input *= temp_arr[j];
}
arr[i] = input;
input = 1; // reset
}
return arr;
}
public static void main(String[] args) {
int [] arr = myFunction(new int[] { 0, 1, 2, 3 });
System.out.println(Arrays.toString(arr));
}
}