简体   繁体   中英

Always getting the same answer in java

Can anyone explain to me why I get only 40 as answer when I run the following code?

View problem in code coach

    import java.util.*;
    public class Program
    {
        public static void main(String[] args) {
            Scanner var = new Scanner (System.in);
            String n = var.nextLine();
            int p = var.nextInt();
            String[] a = var.nextLine().split(" ");
            List<String> list = new ArrayList<String>();
            for(int i=0;i<a.length;i++){
                list.add(a[i]);
            }
            list.add(n);
            Collections.sort(list);
            int y = list.indexOf(n)+1;
            if(y>p){
                System.out.print(((y/p)*20)+(y%p)*20);
            }
            else{
                System.out.print("20");
            }
        }
    }

nextInt() cannot read newline character. so when you use nextLine() it reads that new line character. This answer might help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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