简体   繁体   English

如何阻止我的代码多次打印“升序”?

[英]How to stop my code from printing "Ascending" more than once?

My code is:我的代码是:

Scanner sc=new Scanner(System.in);
    System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
    String input=sc.nextLine();
    for(int i=0;i<input.length(); i++) {
    String[] b=input.split(" ");
    if(Integer.valueOf(b[i]) < Integer.valueOf(b[i+1])) { 
        System.out.println("Acsending"); 
    }
    else { // When condition is false 
            System.out.println("Mixed"); 
        }
    }

But when my input is 1 2 3 4 5 6 , the output is: Ascending Ascending Ascending Ascending Ascending And when my input is 1 4 2 5 2 , the output is, Ascending Mixed Ascending Mixed How do I make the code print only if the input is mixed or ascending?但是当我的输入是1 2 3 4 5 6 ,输出是: Ascending Ascending Ascending Ascending Ascending而当我的输入是1 4 2 5 2 ,输出是, Ascending Mixed Ascending Mixed如何使代码打印仅当输入是混合还是升序?

the guy who answer before me (Elliott Frisch) is PRO I have no Idea what this line does and what those :: Does在我之前回答的人(Elliott Frisch)是专业人士,我不知道这条线是做什么的,还有那些 :: 是什么

Arrays.stream(input.split("\\s+")).mapToInt(Integer::parseInt).toArray();

I'm a beginner myself, this is how i did it我自己是初学者,这就是我做到的

    public static void main(String[] args) 
    {
    Scanner sc =new Scanner(System.in);
    System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
    String input=sc.nextLine();
    String[] b=input.split(" "); 

    sc.close(); //Always Close Scanner after user  :)
    boolean isMixed = false; // Flag

    for(int i=0; i < input.length()/2; i++) //input.length() is equal to number + Spaces we don't want spaces
    {
        //Integer.parseInt(b[i]) converting string to Integer
        if(Integer.parseInt(b[i]) < Integer.parseInt(b[i+1]))
        {
            isMixed = false; 
        } 
        else 
        {
            isMixed = true;
        }

    }

    if(isMixed)
    {
         System.out.println("Mixed"); 
    } else
    {
        System.out.println("Ascending"); 
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM