Ich probiere ein Java Programm zu schreiben, welches zwei ganzen Zahlen
a und b von der Kommandozeile einließt(wobei a<b) und die Summe aller
Zahlen von a bis b ausrechnet.Ich muss keine schleife in der main
Methode verwenden, das Programm muss nach dem ersten Durchlauf
terminieren. Ich habe das geschrieben:
Es ist nicht korekt. Kann jemand mir helfen?
public class Beispiel2
{
void int az(int a, int b,int sum){
//int i;
for(int i=0; i>=a; sum+=++i)
{ if(i<=b)
System.out.println(sum);
}
//return sum;
}
public static void main(String[] args)
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int sum=0;
void int az(int a, int b, int sum);
}
}
Ohje... da ging ja einiges schief. Hoffe, das war jetzt nicht deine
Hausaufgabe?!
public class Beispiel2 {
static int az(int a, int b) {
int sum = 0;
for (int i = a; i <= b; i++)
{
sum = sum + i;
System.out.println("i: " + i + " Teilsumme: " + sum);
}
return sum;
}
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int summe = az(a, b);
System.out.println("Summe: " + summe);
}
}
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.