我仍然没有获得正确的价值

时间:2019-09-14 18:04:52

标签: java for-loop

现在循环正在工作,但是我正在获取'tc'的初始化值。 我想要总成本的值,即这里的tc由公式- | Y-X | * max(c,b)

但是我的程序正在返回tc的初始值,即0。

在解决循环后,答案还是和初始化值相同。

     import java.util.*;
public class Main {
    static int max(int c, int b){
        int m;
        if(b<c){
             m=c;
        }
        else{
            m=b;
        }
        return m;
    }

    public static void main(String[] args) {
        int i, y, z, b, c, j, tc = 0;
        Scanner in= new Scanner(System.in);
        System.out.println("Enter the number of soldiers");
        int n = in.nextInt();
        System.out.println("Enter the coordinates of "+ n + " soldiers");
        int X[]=new int[n];
        for (i=0;i<n;i++)
        {
            X[i]= in.nextInt();
        }
        System.out.println("Enter the numbers of bomb with soldiers");
        int B[]=new int[n];
        for (i=0;i<n;i++)
        {
            B[i]= in.nextInt();
        }
        //below this
        for (i = n-1; i >= 1 ; i--)
        {
            y = X[i];
            c = B[i];
            System.out.println(i);
            for (j = i-1; j >= 0; j--)
            {
                System.out.println(j);

                z = X[i];
                b = B[i];
                int m = max(c,b);
                tc += (y - z) * m;

            }
        }
        // till here

        System.out.println(tc);

    }
}

0 个答案:

没有答案
相关问题