我一直在試圖找出這個問題的答案有什麼問題。真的需要幫助!任何幫助表示讚賞!由於JAVA - For循環和If else語句
問題
- 您可以輸入兩個整數(A和B),並
- ,程序會從數字打印到數字B如果
a<b
。 - 如果
a>b
,程序將從b打印到a。 如果
b is the same as a
,程序會要求用戶輸入另一個數字b,直到它不等於a。Scanner sc = new Scanner(System.in); System.out.println("Enter a: "); int a = sc.nextInt(); System.out.println("Enter b: "); int b = sc.nextInt(); if(a > b) { for(int i = b; b >= a; b--) { System.out.println(b); } } else if (a < b) { for(int i = a; a <= b; a++) { System.out.println(i); } } else { System.out.println("Enter another number b: "); int numberb = sc.nextInt(); }
}
'while(true)'和'break'不是'很好的做法','do/while'是爲:) – azro
@azro重構,謝謝,你剛剛刪除了一行不必要的代碼。現在讓我們考慮一種用單行lambda替換整個事物的方法;-) –