2017-11-08 9 views
-1

入力引数でエラーが何か分かりません。コンパイルエラーメッセージはあまり役に立ちません。 instantiated-されていないjava: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

+5

変更を[0、1、2、3]'へ '新しいint型[] {0、1、2、 3} ' – Eran

+0

javaで配列を作成するためにさらに詳しい説明と異なる方法が必要な場合は、次のリンクを参照してください:https://www.javatpoint.com/array-in-java –

答えて

1

あなたのプログラムにはいくつかの問題があります。私は、Java言語の基礎を学ぶことを強くお勧めします。インターネットには多くの無料のリソースがあります。ここにあなたが始めることができるJava Tutorialsウェブサイトがあります。 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)); 
    } 

} 
0

1.arr>数字はこれを使用すると、あなたが入力に配列をしたい2.Ifはnullポインタ例外

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  
     } 

    } 
関連する問題