2017-11-02 8 views
-4

ちょっと私が手に、次のエラーint型カントBEのint []私はそれを理解しない:私はJavaのintエラーがあることカントのint []

エラーが int型の上にある[] cijferStudent =新しいint型[7]。そしてwhileループで。誰かが私を助けることができれば、私はそれを感謝します。あなたの助けのための

おかげであなたを:)

package oefenopdracht6a; 

import java.util.Scanner; 

/** 
* 
* @author eazyman 
*/ 
public class Oefenopdracht6a { 

    public static Scanner input = new Scanner(System.in); 
    public static int aantalCijfers = 0; 

    public static void main(String[] args) { 

     aantalCijfersGeroepen(); 
    } 

    public static void aantalCijfersGeroepen() { 
     System.out.println("Hoeveel cijfers wilt u invoeren?"); 
     aantalCijfers = input.nextInt(); 

     for (int i = 0; i >= aantalCijfers;) { 
      System.out.println("Aantal cijfers moet groter zijn dan 0!"); 
      aantalCijfersGeroepen(); 
     } 

     int i = 0; 
     int[] cijferStudent = new int[7]; 
     while (i < aantalCijfers) { 
      System.out.println("Cijfer student " + i); 
      cijferStudent = input.nextInt(); 
      i++; 
     } 

     System.out.println(cijferStudent); 

    } 

} 
+0

はい、あなたは 'cijferStudent = input.nextInt();に影響を与えようとしましたが、nextIntはintを返し、' ciferStudent'はint []です。私はあなたが 'ciferStudent [i]' –

+0

'cijferStudent'が配列であると思います。そのような配列に値を代入することはできません。 –

+0

'input.nextInt()'は整数の配列として定義された単一の整数 'cijferStudent'を返します。したがって、それらは異なる型です。つまり、' cijferStudent [i] = input.nextInt()入力トークンを 'cijferStudent'のフィールドに代入したいかもしれません。 – Youka

答えて

1
cijferStudent = input.nextInt(); 

は、input.nextInt()がintを返しますので、誤差はあるが、cijferStudentintの配列です

cijferStudent[i] = input.nextInt(); 

に変更する必要がありますintint[]に割り当てることはできません。ただし、配列内の特定の場所にintを割り当てることができます(この場合は、i番目の場所が推測されます)。

1

インデックスが欠落しているだけでなく、ユーザーが入力したすべての負の値に対して、 aantalCijfersGeroepenが呼び出され、正の数に戻ってくるので、N回入力するように求められます。分割された質問と処理。

public static void main(String[] args) { 
    aantalCijfersGeroepen(); 
    uitgaveAantalCijfers(); 
} 

public static void aantalCijfersGeroepen() { 
    System.out.println("Hoeveel cijfers wilt u invoeren?"); 
    aantalCijfers = input.nextInt(); 

    while (aantalCijfers <= 0) { 
     System.out.println("Aantal cijfers moet groter zijn dan 0!"); 
     aantalCijfersGeroepen(); 
    } 
} 

public static void uitgaveAantalCijfers() { 
    int[] cijferStudent = new int[7]; 
    for (int i = 0; i < aantalCijfers; ++i) { 
     System.out.println("Cijfer student " + i); 
     cijferStudent[i] = input.nextInt(); 
     //System.out.println(" " + cijferStudent[i]); 
    } 
    System.out.println(Arrays.toString(cijferStudent)); 
} 

また、アレイの印刷がどちらか、あまりにもループで行わ や便利な機能Arrays.toStringを使用する必要があります。

関連する問題