Java 不打印相同的输出

Java 不打印相同的输出,java,debugging,fitness,hill-climbing,Java,Debugging,Fitness,Hill Climbing,只是不打印与上面一行相同的输出,我不明白为什么会发生这种情况,我注意到它从末尾向后打印最后N个数字,无论我在参数中输入什么,它都会再次打印该数量 这是主要的 public class main { public static void main(String args[]) { ScalesSolution s1 = new ScalesSolution(11); s1.println(); ScalesSolution s2 = new ScalesSoluti

只是不打印与上面一行相同的输出,我不明白为什么会发生这种情况,我注意到它从末尾向后打印最后N个数字,无论我在参数中输入什么,它都会再次打印该数量

这是主要的

public class main {
    public static void main(String args[]) {
    ScalesSolution s1 = new ScalesSolution(11);
    s1.println();
    ScalesSolution s2 = new ScalesSolution(s1.GetSol());
    s2.println();
}
}
这是ScaleSolution类

import java.util.ArrayList;
import java.util.Random;

public class ScalesSolution {
private String scasol;

public void print() {
    System.out.print(scasol);
}

// Display the string with a new line
public void println() {
    print();
    System.out.println();
}



public String GetSol()
{
    return scasol;
}
}
这是另一节课

import java.util.*;
import java.io.*;

public class randomOther {
// Shared random object
static private Random rand;

// Create a uniformly distributed random integer between aa and bb inclusive
static public int UI(int aa, int bb) {
    int a = Math.min(aa, bb);
    int b = Math.max(aa, bb);
    if (rand == null) {
        rand = new Random();
        rand.setSeed(System.nanoTime());
    }
    int d = b - a + 1;
    int x = rand.nextInt(d) + a;
    return (x);
}

// Create a uniformly distributed random double between a and b inclusive
static public double UR(double a, double b) {
    if (rand == null) {
        rand = new Random();
        rand.setSeed(System.nanoTime());
    }
    return ((b - a) * rand.nextDouble() + a);
}
static public ArrayList<Double> ReadNumberFile(String filename) {
    ArrayList<Double> res = new ArrayList<Double>();
    Reader r;
    try {
        r = new BufferedReader(new FileReader(filename));
        StreamTokenizer stok = new StreamTokenizer(r);
        stok.parseNumbers();
        stok.nextToken();
        while (stok.ttype != StreamTokenizer.TT_EOF) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER) {
                res.add(stok.nval);
            }
            stok.nextToken();
        }
    } catch (Exception E) {
        System.out.println("+++ReadFile: " + E.getMessage());
    }
    return (res);
}
}

我认为这两个输出应该是相同的,我发现这里有一个问题,不确定为什么它们不

我发现您在
随机二进制字符串(int n)
中使用
System.out.print
的方式造成了混乱。它正在打印并附加到字符串
s
。尽量避免这样。替换
系统输出打印(s+=“0”)
系统输出打印(s+=“1”)带有
s+=“0”
s+='1'RandomBinaryString
中的code>将修复您的输出

在代码中使用以下代码段:

private static String RandomBinaryString(int n) {
    String s = new String();

    // Code goes here
    // Create a random binary string of just ones and zeros of length n
    for (int i = 0; i < n; i++) {
        int y = randomOther.UI(0, 1);
        if (y == 0) {
            s += '0';// this line here was changed
        } else {
            s += '1';// and this line here was changed too
        }
    }

    return (s);
}
私有静态字符串RandomBinaryString(int n){
字符串s=新字符串();
//代码在这里
//创建一个长度为n的1和0的随机二进制字符串
对于(int i=0;i


希望这有帮助

请提供一个仍然重现错误的较小示例。比如说,删除一些函数。此外,我看到您在代码中使用了
Random
,并使用当前时间对其进行种子设定,请同时消除随机性(例如,通过使用常量数据对其进行种子设定,或者更好的是,通过完全消除对Random的依赖并使用硬编码常量)。我不确定如何编辑它:查阅StackOverflow的教程。如果你不知道问题是什么,你希望其他人怎么做?你在期待什么?@DevilsHnd嗨,先生,我知道问题出在它们应该是一样的时候,输出是不同的,我不知道为什么它们是不同的尽管这是我的问题,我希望这里的人知道发生了什么,可以帮助我的银行。。。。哇,非常感谢,这很有趣,因为我刚刚忘记从我的前一天删除这个,我忘了重新删除它。-。很高兴它有帮助!
private static String RandomBinaryString(int n) {
    String s = new String();

    // Code goes here
    // Create a random binary string of just ones and zeros of length n
    for (int i = 0; i < n; i++) {
        int y = randomOther.UI(0, 1);
        if (y == 0) {
            s += '0';// this line here was changed
        } else {
            s += '1';// and this line here was changed too
        }
    }

    return (s);
}