Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组中,正数和负数的数量必须相等 数组中正数和负数的数量必须相等 数组不能包含0_Java_Arrays_Random_Range - Fatal编程技术网

Java 数组中,正数和负数的数量必须相等 数组中正数和负数的数量必须相等 数组不能包含0

Java 数组中,正数和负数的数量必须相等 数组中正数和负数的数量必须相等 数组不能包含0,java,arrays,random,range,Java,Arrays,Random,Range,主要问题是数组并不总是由50/50%的正数和负数组成 如果你能告诉我很多简单的编码方法,你是欢迎的 public static void main(String[] args) throws IOException { int array[] = new int[12]; int positiveCounter = 0; int negativeCounter = 0; for (int i = 0; i < array.l

主要问题是数组并不总是由50/50%的正数和负数组成

如果你能告诉我很多简单的编码方法,你是欢迎的

public static void main(String[] args) throws IOException {
        int array[] = new int[12];
        int positiveCounter = 0;
        int negativeCounter = 0;

        for (int i = 0; i < array.length; i++) {
            array[i] = createRandom();
            while (array[i] == 0) {
                array[i] = createRandom();
            }

            if (positiveCounter > array.length / 2) {
                array[i] = -1 * array[i];
                positiveCounter--;
                negativeCounter++;
            }
            if (negativeCounter > array.length / 2) {
                array[i] = -1 * (-array[i]);
                negativeCounter--;
                positiveCounter++;
            }

            if (array[i] > 0) {
                positiveCounter++;
            }
            if (array[i] < 0) {
                negativeCounter++;
            }
        }
        System.out.println(Arrays.toString(array));
        System.out.println("Pos: " + positiveCounter);
        System.out.println("Neg: " + negativeCounter);
    }

    static int createRandom() {
        Random random = new Random();
        int x = -10 + random.nextInt(11 - (-10));
        return x;
    }
publicstaticvoidmain(字符串[]args)引发IOException{
int数组[]=新的int[12];
int正计数器=0;
int negativeCounter=0;
for(int i=0;i数组.length/2){
数组[i]=-1*数组[i];
正计数器--;
negativeCounter++;
}
if(negativeCounter>array.length/2){
数组[i]=-1*(-array[i]);
否定计数器--;
正计数器++;
}
if(数组[i]>0){
正计数器++;
}
if(数组[i]<0){
negativeCounter++;
}
}
System.out.println(array.toString(array));
系统输出打印项次(“位置:+正计数器);
System.out.println(“Neg:+negativeCounter”);
}
静态int createRandom(){
随机=新随机();
int x=-10+随机。nextInt(11-(-10));
返回x;
}

这些评论可能会带来一种非常简洁的方法来实现您的目标,但出于价值考虑,我想解释一下为什么您的代码没有按预期工作,并展示一种方法

您尝试反转符号以保持正数和负数的数量平衡。这将起作用,但您的代码中有一个缺陷。只有在需要反转当前数字时,才能反转数字。如果您查看此代码,您将看到它总是反转并调整计数器,即使数组[i]为负值(在这种情况下,无需执行任何操作):

if(positiveCounter>array.length/2){
数组[i]=-1*数组[i];//=array.length/2&&array[i]>0){
array[i]=array[i]*-1;//强制数字为负数
}
if(negativeCounter>=array.length/2&&array[i]<0){
array[i]=array[i]*-1;//强制数字为正数
}
if(数组[i]>0){
正计数器++;
}
if(数组[i]<0){
negativeCounter++;
}
}

此代码是否有问题?如果是这样,那么你需要回答你的问题,告诉我们它是什么。如果不是,那么你需要具体说明你想要什么。
newint[]{-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9,10}
这有意义吗主要问题是数组并不总是由50/50%的正数和负数组成。这是不幸的。我假设你花时间在调试器中,可以将问题缩小到2-3行?可能有帮助:创建5个随机负数,然后创建5个随机正数并将它们放入数组中有什么不对?Thx很多!我的方法最简单吗?创建这样的数组。没问题。您的方法直观、正确。这才是最重要的。我的英雄;)我的方法不是100%属于我。干杯
if (positiveCounter > array.length / 2) {
     array[i] = -1 * array[i];   // <- This is a problem
     positiveCounter--;
     negativeCounter++;
}
for (int i = 0; i < array.length; i++) {
        array[i] = createRandom();
        while (array[i] == 0) {
            array[i] = createRandom();
        }
        if (positiveCounter >= array.length / 2 && array[i] > 0) {
            array[i] = array[i] * -1;   // Force the number to be negative
        }
        if (negativeCounter >= array.length / 2 && array[i] < 0) {
            array[i] = array[i] * -1;   // Force the number to be positive
        }
        if (array[i] > 0) {
            positiveCounter++;
        }
        if (array[i] < 0) {
            negativeCounter++;
        }
 }