簡體   English   中英

升序和Java降序

[英]Ascending order and Descending Java

嗨,這是我的問題,我似乎無法正確打印輸出,我想我的代碼中存在邏輯錯誤,當我輸入一個升序編號然后是一個降序時,它不會顯示。 我也是編程的新手。 碼:

 import java.util.Scanner;
    public class tester {
        public static void main(String[] args) {
            int n, i, k, j;
            int asc = 0, 
            Scanner x = new Scanner(System.in);

            do {

                System.out.print("How many numbers to process : ");
                k = x.nextInt(); 

                if(k<=1) {
                    System.out.println("Enter a number greater than 1");
                }
            } while(k<=1);

            System.out.printf("Please enter %d numbers: ",k);
            n = x.nextInt();  

            for(i=0; i<n-1; i++) { 
                j = x.nextInt();
                if( j < n) { 
                    asc++;              // is this right?
                } else {
                    asc--;
                }
            }
            if (asc==k) {
                System.out.print("Not Growing Up."); 
            } 
            if (asc!=k) { 
                System.out.print("Growing Up.");
            }
        }   
   }

這是輸出

 Example outputs (what i'm trying to get)
 How many numbers to process : 4
 Please enter 4 numbers : 1 2 3 4
 Growing up.

 How many numbers to process : 4
 Please enter 4 numbers : 4 3 2 1
 Not Growing up.

這是我的問題:

 How many numbers to process : 4
 Please enter 4 numbers : 1 2 1 3
 Growing up.         // it should be not growing up.

無需遍歷所有數字。 您可以只檢查以前的數字是否較低(如果增長)。 如果沒有,請打印並返回。 檢查我的示例代碼。

更換

n = x.nextInt(); 
for (i=0; i<n-1; i++) { 
    j = x.nextInt();
    if( j < n) { 
        asc++;              // is this right?
    } else {
        asc--;
    }
}
if (asc==k) {
    System.out.print("Not Growing Up."); 
} 
if (asc!=k) { 
    System.out.print("Growing Up.");
}

int prev = x.nextInt();
for (i=0; i<k-1; i++) { 
    j = x.nextInt();
    if (j < prev) { System.out.print("Not Growing Up."); return; }
    prev = j;
}
System.out.print("Growing Up.");
    String numbers = "1 2 3 4";  // Let's take this input for example
    int temp = 0;               // This use to compare previous integer
    boolean isAsc = false;     // This store whether the digits is growing up or not

    StringTokenizer st = new StringTokenizer(numbers); // Declare StringTokenizer
     while (st.hasMoreTokens()) {

       int next = Integer.parseInt(st.nextToken());  // Put the first integer in next (1)

       if(next > temp){    // if (1) > 0

         temp = next;     // Assign 1 to temp, next time digit 2 will compare with digit 1         
         isAsc = true;   // Assign the ascending to true

       } else

         isAsc = false;
     }

    if(isAsc)
      System.out.print("Growing up.");
    else
      System.out.print("Not growing up.");
  }

您可以將用戶輸入像我聲明的變量numbers一樣存儲為字符串,並將它們分成每個標記以進行比較。

import java.lang.reflect.Array;
import java.util.*;

public class A1 {
public static void main(String[] args) {
    int a[]={2,5,0,1};
    Arrays.sort(a);
    int b= a.length;
    for(int i=0;i<a.length;i++)
    {

        System.out.println(+a[i]+"\t"+a[b-1]);
        b--;



    }


}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM