Sag mir, was ich vermisse?Zeigt ganze Zahlen vom ersten bis zum letzten C-Step-Schritt an. Wenn die Ausgabe zu Endlosschleifen führen kann, eine Ausnahme auslösen IllegalArgumentException
Input Arguments
1, 8, 2
8, 1, -2
1, 8, -2
Result
1 3 5 7
8 6 4 2
IllegalArgumentException
Mein Code:
package com.Star;
public class Main {
public static void main(String[] args) {
rangeWithStepPrinter(1,8,5);
rangeWithStepPrinter(8,1,-2);
rangeWithStepPrinter(1,8,-2);
}
public static void rangeWithStepPrinter(int first, int last, int i) {
if (first < last) {
for (i = first; i <= last; i += +2) {
System.out.print(i + " ");
}
}
if (first > last) {
for (i = first; i >= last; i += -2) {
System.out.print(i + " ");
}
}
System.out.println();
}
}
Sagen Sie uns, was das Problem ist. Sie könnten eine beliebige Anzahl von Dingen vermissen, wie sollen wir es ohne eine detaillierte Erklärung des Problems wissen und wo Sie Probleme haben? –
[Nichts in diesem Code löst eine "IllegalArgumentException" aus (http://ideone.com/HbF7LV). Beachten Sie, dass Sie den Parameter "i" ignorieren. –
Ich bekomme 1 3 5 7 für die letzte –