Array Ausgabe

Lacritz

Mitglied
Heyho,
ich versuche mich gerade an Programmieren mit Arrays - nun komm ich aber auf einen Fehler aus dem ich nicht schlau werde.. Wie genau deklariere ich die Variablen die in meine Methode (mit einem Array) "eingespeist" werden sollen

Mein Versuch bisher:
Java:
import java.util.Arrays;

import javax.swing.JOptionPane;
public final class fillArray {
	static int[] testFeld = new int[5];
	static int a , x , y , z;
	static String testString;
	static int[] fillArray(int size, int min, int step){
		int [] testFeld={1,2,3,4,5};
		if (size<min){
			testFeld [1]=min+(step*1);
			testFeld [2]=min+(step*2);
			testFeld [3]=min+(step*3);
			testFeld [4]=min+(step*4);
			testFeld [5]=min+(step*5);
		return testFeld;
		}
		else{
			testFeld [1]=min+(step*1);
			testFeld [2]=min+(step*2);
			testFeld [3]=min+(step*3);
			testFeld [4]=min+(step*4);
			testFeld [5]=min+(step*5);
		return testFeld;
		}
	}
	

	public static void main(String[] args) {
		 x = Integer.parseInt(JOptionPane.showInputDialog("size="));
		 y = Integer.parseInt(JOptionPane.showInputDialog("min="));
		 z = Integer.parseInt(JOptionPane.showInputDialog("step="));
		System.out.print(Arrays.toString(testFeld[x,y,z]));
	

	}

}

MfG Lacritz
 
Indizes bei Arrays beginnen bei 0, nicht bei 1. 😉
Die Version hier läuft und liefert ein Ergebnis.

Java:
public class fillArray
{

    static int[] testFeld = new int[5];
    static int a, x, y, z;
    static String testString;

    static int[] fillArray( int size, int min, int step )
    {
        int[] testFeld =
        {
            1, 2, 3, 4, 5
        };
        if ( size < min )
        {
            testFeld[0] = min + (step * 1);
            testFeld[1] = min + (step * 2);
            testFeld[2] = min + (step * 3);
            testFeld[3] = min + (step * 4);
            testFeld[4] = min + (step * 5);
            return testFeld;
        }
        else
        {
            testFeld[0] = min + (step * 1);
            testFeld[1] = min + (step * 2);
            testFeld[2] = min + (step * 3);
            testFeld[3] = min + (step * 4);
            testFeld[4] = min + (step * 5);
            return testFeld;
        }
    }

    public static void main( String[] args )
    {
        x = Integer.parseInt( JOptionPane.showInputDialog( "size=" ) );
        y = Integer.parseInt( JOptionPane.showInputDialog( "min=" ) );
        z = Integer.parseInt( JOptionPane.showInputDialog( "step=" ) );
        testFeld = fillArray( x, y, z );
        System.out.print( Arrays.toString( testFeld ) );
    }
}
 

Zurück
Oben