[英]Print the series of numbers in java by user input
我需要根據用戶輸入顯示以下系列
示例:如果用戶輸入數字5,則預期輸出將是
54321
5432
543
54
5
public static void main(String args[]) throws Exception {
int n;
boolean quit = false;
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number :");
n = sc.nextInt();
System.out.println("The Series is:");
for(int i=n;i>=1;i--){
for(int j=n; j<=i; j++){
System.out.print(j);
}
}
System.out.println("");
}
假設您可以毫無問題地獲得所需的整數,那么序列的算法可能如下所示:
int f = 5;
int k = f;
while (k > 0) {
for (int i = 0; i < k; i++) {
System.out.println(f - i);
}
k--;
System.out.println("....");
}
正在打印所需的順序:5 4 3 2 1 .... 5 4 3 2 .... 5 4 3 .... 5 4 .... 5 ....
請嘗試以下代碼:
System.out.println("The Series is:");
System.out.println("");
for(int i=1;i<=n;i++){
for(int j=n; j>=i; j--){
System.out.print(j);
}
System.out.println("");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.