Код в для l oop не выполняется - PullRequest
0 голосов
/ 25 марта 2020

Я создал какое-то время l oop, вычислил площадь и сравнил цену этой области с данной ценой, и если данная цена ниже, то наш результат - это расчетная область. Мне нужно напечатать весь результат в конце, когда пока l oop перерыв. Поэтому я сохраняю результат в массиве, чтобы напечатать его, когда l oop не работает, но for в конце не выполняется.

    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    String[] result = new String[t];

    while (t != 0) {
        int j = 0;
        int n = sc.nextInt();
        int b = sc.nextInt();
        int w[] = new int[n];
        int h[] = new int[n];
        int p[] = new int[n];
        int area[] = new int[n];

        for (int i = 0; i < n; i++) {
            w[i] = sc.nextInt();
            h[i] = sc.nextInt();
            p[i] = sc.nextInt();
        }

        for (int i = 0; i < n; i++) {
            area[i] = w[i] * h[i];
            System.out.println("Area: " + area[i]);
        }

        int count = 0;

        for (int i = 0; i < n; i++) {
            if (b >= p[i]) {
                count++;
            }
        }

        System.out.println("count: " + count);

        if (count == 0) {
            result[j] = "no tablet";
        } else {
            int check[] = new int[count];
            for (int i = 0; i < count; i++) {
                if (b >= p[i]) {
                    check[i] = area[i];
                }
            }
            Arrays.sort(check);
            result[j] = "" + check[count - 1];
        }
        j++;
        t--;
    }

    for (int i = 0; i < t; i++) {
        System.out.println("Result: " + result[i]);
    }

Ответы [ 2 ]

0 голосов
/ 25 марта 2020

Вот правильный код. Я удалил ошибку и выдал приглашение пользователю ввести значения. ваш последний для l oop не был запущен, потому что в то время как l oop ваше значение 't' становится 0, и это конец, в котором вы использовали этот t в условии для l oop, поэтому он не был запущен , Я использовал другую переменную для этого. поэтому ваша проблема решена сейчас. Вот полный правильный код.

Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();
        String[] result=new String[t];
        int x = t;

        while(t!=0) {
            int j=0;
            int n=sc.nextInt();
            int b=sc.nextInt();
            int w[]=new int[n];
            int h[]=new int[n];
            int p[]=new int[n];
            int area[]=new int[n];

            for(int i=0;i<n;i++) {
                System.out.println("Enter Width for w["+i+"]: ");
                w[i]=sc.nextInt();
                System.out.println("Enter Height for h["+i+"]: ");
                h[i]=sc.nextInt();
                System.out.println("Enter Price for w["+i+"]: ");
                p[i]=sc.nextInt();
            }

            for(int i=0;i<n;i++) {
                area[i]=w[i]*h[i];
                System.out.println("Area A["+i+"]: " +area[i]);
            }

            int count=0;

            for(int i=0;i<n;i++) {
                System.out.println("b = "+b+"  and p["+i+"] = "+p[i]);
                if(b>=p[i]) {
                    count++;
                }
            }

            System.out.println("count: "+count);

            if(count==0) {
                result[j]="no tablet";
            }else {

            int check[]=new int[count];

            for(int i=0;i<count;i++) {
                if(b>=p[i]) {
                    check[i]=area[i];
                }
            }

            Arrays.sort(check);
                        result[j]=""+check[count-1];




        }

            j++;
            t--;
        }


        for(int i=0;i<x;i++) {
            System.out.println("Result: "+result[i]);
            }
0 голосов
/ 25 марта 2020

вы должны проверить свой список больше, чем запись

, как это

 while(t>0)

и область, которую вы должны объявить снаружи, в то время как l oop

я так думаю

Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();
        String[] result=new String[t];
        List area=new List<int>();

        while(t>0) {
            int j=0;
            int n=sc.nextInt();
            int b=sc.nextInt();
            int w[]=new int[n];
            int h[]=new int[n];
            int p[]=new int[n];


            for(int i=0;i<n;i++) {
                w[i]=sc.nextInt();
                h[i]=sc.nextInt();
                p[i]=sc.nextInt();
            }

            for(int i=0;i<n;i++) {
                area.Add(w[i]*h[i]);
                System.out.println("Area: "+area[i]);
            }

            int count=0;

            for(int i=0;i<n;i++) {
                if(b>=p[i]) {
                    count++;

                }
            }

            System.out.println("count: "+count);

            if(count==0) {
                result[j]="no tablet";
            }else {

            int check[]=new int[count];

            for(int i=0;i<count;i++) {
                if(b>=p[i]) {
                    check[i]=area[i];
                }
            }

            Arrays.sort(check);
                        result[j]=""+check[count-1];




        }

            j++;
            t--;
        }


        for(int i=0;i<t;i++) {
            System.out.println("Result: "+result[i]);
            }


    }
}
...