Hallo
Ich habe folgendes Programm geschrieben:
Ich habe in dem Programm sehr viele Variablen, ich dachte jetzt ich könnte das Programm durch ein Array vereinfachen, also habe ich mir Grundwissen zu Arrays angeignet.
Mein Problem ist jetzt folgendes: Wie kann man denn mit Arrays rechnen?
Ich habe mein Array erstmal deklariert:
Ich möchte jetzt folgende Berechnung, mithilfe des Arrays machen:
Leider habe ich keinen Peil, wie ich das machen soll :bahnhof:
Wie kann ich denn gezielt ein Feld im Array ansprechen und zu einer bereits gespeicherten Variablen was dazu addieren?
Meinen ersten Versuch habe ich mir jetzt erstmal aus Java von Kopf bis Fuß abgekupfert, aber ich kann mir nicht vorstellen, dass es so klappt. Kann mir vllt. jemand von euch auf die Sprünge helfen?
LG
Ich habe folgendes Programm geschrieben:
Java:
import java.text.DecimalFormat;
public class AmeiseKorr
{
public static void main (String [] args)
{
DecimalFormat df = new DecimalFormat("0.000");
int input = 0, input2 = 0, ants, control, control2;
String FormatcoordA1 = "0", FormatcoordA2 = "0", FormatcoordB1 = "0", FormatcoordB2 = "0";
String Formatc_way1 = "0", Formatc_way2 = "0", Formatc_Distance = "0", Formatdistance_Ants = "0";
do
{
System.out.print("\n" + "Moechten Sie eine Ameise oder 2 Ameisen laufen lassen?");
System.out.print("\n" + "Sie koennen das Programm mit einer Eingabe <= 0 abbrechen!" );
System.out.print("\n" + "Druecken Sie die 1 fuer eine Ameise oder die 2 fuer zwei Ameisen: ");
ants = Input.readInt();
do
{
double coordX = 0, coordY = 0, coord_A = 0, coordA = 0, coordA1 = 0, coordA2 = 0;
double coord_B = 0, coordB = 0, coordB1 = 0, coordB2 = 0;
double c_wa = 0, c_way = 0, c_way1 = 0, c_way2 = 0, c_Distance = 0, distance_Ants = 0;
if ( ants == 1 )
{
System.out.print("\n" + "Anzahl von Schritten? (Abbruch mit <= 0): " );
input = Input.readInt();
}
if ( ants == 2 )
{
System.out.print("\n" + "Anzahl der Schritte von Ameise Eins (Abbruch mit <= 0): " );
input = Input.readInt();
System.out.print("\n" + "Anzahl der Schritte von Ameise Zwei (Abbruch mit <= 0): " );
input2 = Input.readInt();
}
control = input;
control2 = input2;
while ( control >= 1 )
{
coordA = Math.random() + Math.random() * (-1);
coordA1 += coordA;
coordB = Math.random() + Math.random() * (-1);// Zufallszahl zwischen -1 und 1 erstellen
coordB1 += coordB;
c_way = Math.sqrt( Math.pow( coordA, 2 ) + Math.pow( coordB, 2 ) );
c_way1 += c_way;
control--;
//System.out.println(coordA + " " + coordB);
}
while ( control2 >= 1 )
{
coord_A = Math.random() + Math.random() * (-1);
coordA2 += coord_A;
coord_B = Math.random() + Math.random() * (-1);
coordB2 += coord_B;
c_wa = Math.sqrt( Math.pow( coord_A, 2 ) + Math.pow( coord_B, 2 ) );
c_way2 += c_wa;
control2--;
//System.out.println(coord_A + " " + coord_B);
}
if ( ants == 1 && input >= 1 )
{
c_Distance = Math.sqrt( Math.pow( coordA1 - coordX, 2 ) + Math.pow( coordB1 - coordY, 2));
FormatcoordA1 = df.format(coordA1);
FormatcoordB1 = df.format(coordB1);
Formatc_way1 = df.format(c_way1);
Formatc_Distance = df.format(c_Distance);
System.out.print( "\n" + "Endposition (x,y): " + FormatcoordA1 + ", " + FormatcoordB1 );
System.out.print( "\n" + "Der zurueckgelegte Weg der Ameise betraegt: " + Formatc_way1);
System.out.print( "\n" + "Distanz von (0,0): " + Formatc_Distance + "\n");
}
if ( ants == 2 && input >= 1 && input2 >= 1 )
{
distance_Ants = Math.sqrt( Math.pow(coordA1 - coordA2, 2 ) + Math.pow(coordB1 - coordB2, 2) );
FormatcoordA1 = df.format(coordA1);
FormatcoordB1 = df.format(coordB1);
Formatc_way1 = df.format(c_way1);
FormatcoordA2 = df.format(coordA2);
FormatcoordB2 = df.format(coordB2);
Formatc_way2 = df.format(c_way2);
Formatdistance_Ants = df.format(distance_Ants);
System.out.print("\n" + "Endposition Ameise 1 (x,y): " + FormatcoordA1 + ", " + FormatcoordB1 );
System.out.print("\n" + "Weg Ameise 1: " + Formatc_way1 );
System.out.print("\n" + "Endposition Ameise 2 (x,y): " + FormatcoordA2 + ", " + FormatcoordB2 );
System.out.print("\n" + "Weg Ameise 2: " + Formatc_way2 );
System.out.print("\n" + "Distanz zwischen Ameise 1 und Ameise 2: " + Formatdistance_Ants );
}
} while ( input > 0 );
} while ( ants >= 1 );
}
}
Ich habe in dem Programm sehr viele Variablen, ich dachte jetzt ich könnte das Programm durch ein Array vereinfachen, also habe ich mir Grundwissen zu Arrays angeignet.
Mein Problem ist jetzt folgendes: Wie kann man denn mit Arrays rechnen?
Ich habe mein Array erstmal deklariert:
Java:
int x = 0, y = 0;
double[] coord1 = new double[7];
double[] coord2 = new double[7];
Ich möchte jetzt folgende Berechnung, mithilfe des Arrays machen:
Java:
while ( control >= 1 )
{
coordA = Math.random() + Math.random() * (-1);
coordA1 += coordA;
coordB = Math.random() + Math.random() * (-1);// Zufallszahl zwischen -1 und 1 erstellen
coordB1 += coordB;
c_way = Math.sqrt( Math.pow( coordA, 2 ) + Math.pow( coordB, 2 ) );
c_way1 += c_way;
control--;
//System.out.println(coordA + " " + coordB);
}
Leider habe ich keinen Peil, wie ich das machen soll :bahnhof:
Wie kann ich denn gezielt ein Feld im Array ansprechen und zu einer bereits gespeicherten Variablen was dazu addieren?
Meinen ersten Versuch habe ich mir jetzt erstmal aus Java von Kopf bis Fuß abgekupfert, aber ich kann mir nicht vorstellen, dass es so klappt. Kann mir vllt. jemand von euch auf die Sprünge helfen?
Java:
while ( x < input )
{
coord1[x] = new coord();
coord1[x].coordA = Math.random() + Math.random() * (-1);
coord1[x].coordA1 += coord1[x].coordA;
coord1[x].coordB = Math.random() + Math.random() * (-1);// Zufallszahl zwischen -1 und 1 erstellen
coord1[x].coordB1 += coord1[x].coordB;
coord1[x].c_way = Math.sqrt( Math.pow( coordA, 2 ) + Math.pow( coordB, 2 ) );
coord1[x].c_way1 += coord1[x].c_way;
LG