2011-03-22 64 views
1

下面的方法应该创建两个随机的字符串变量并打印它们。在这一点上,它们应该是相同的。Java方法不工作

public static void main(String[] args){ 
    ScalesSolution s1 = new ScalesSolution(10); 
    s1.println(); 
    ScalesSolution s2 = new ScalesSolution(s1.getSol()); 
    s2.println(); 
} 

的ScalesSolution类:

import java.util.*; 

public class ScalesSolution 
{ 
private String scasol; 
//Creates a new scales solution based on a string parameter 
//The string parameter is checked to see if it contains all zeros and ones 
//Otherwise the random binary string generator is used (n = length of parameter) 
public ScalesSolution(String s) 
{ 
    boolean ok = true; 
    int n = s.length(); 
    for(int i=0;i<n;++i) 
    { 
     char si = s.charAt(i); 
     if (si != '0' && si != '1') ok = false; 
    } 
    if (ok) 
    { 
     scasol = s; 
    } 
    else 
    { 
     scasol = RandomBinaryString(n); 
    } 
} 
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 

    return(s); 
} 
public ScalesSolution(int n) 
{ 
    scasol = RandomBinaryString(n); 
} 
//This is the fitness function for the Scales problem 
//This function returns -1 if the number of weights is less than 
//the size of the current solution 
public double ScalesFitness(ArrayList<Double> weights) 
{ 
    if (scasol.length() > weights.size()) return(-1); 
    double lhs = 0.0,rhs = 0.0; 
    int n = scasol.length(); 

    //Code goes here 
    //Check each element of scasol for a 0 (lhs) and 1 (rhs) add the weight wi 
    //to variables lhs and rhs as appropriate 

    return(Math.abs(lhs-rhs)); 
} 

public void smallChange(int n) 
{ 
    Random rand = new Random(); 
    int p = (rand.nextInt(n) - 1); 

//  Checks if p < 0. If so, then p will not have 1 subtracted from it. 
    if(p < 0){ 
     p = (rand.nextInt(n)); 
    } 

    String x = new String(); 

    x = scasol.substring(0, p); 
     if (scasol.charAt(p) == '0') 
      scasol.replace('0', '1'); 
     else if (scasol.charAt(p) == '1') 
      scasol.replace('1', '0'); 
      scasol = x; 
}//End smallChange() 

public String getSol(){ 

    return(scasol); 

}//End getSol() 

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

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


} 

输出是空白的 - 我在做什么错?

谢谢。

回答

4

现在您RandomBinaryString代码包含一个新的字符串和注释的:

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 

    return(s); 
} 

这意味着您总能获得一个空字符串回来时,你打电话RandomBinaryString。当“代码在这里”出现时,情况很可能会更好。 :)

+0

谢谢,现在全部解决。 – MusTheDataGuy 2011-03-22 18:01:01

5

你需要写这远远这里提到

String s = new String(); 
//Code goes here 
//Create a random binary string of just ones and zeros of length n 

return(s); 
+0

我认为他的观点是它永远不应该达到这个代码。 – Will 2011-03-22 14:19:50

+1

当然它达到这个代码。这段代码是他打算编写的练习的一部分。 int的构造函数将调用“private static String RandomBinaryString(int n)”方法,因为他被要求实现它,所以它什么也不做。 – 2011-03-22 14:21:08

+0

是的,但他似乎认为'新的ScalesSolution(10)'正在调用String构造函数。也许这可以指给他。 – Will 2011-03-22 14:24:14

0

的部分,什么是印刷取决于scasol的定义。从上面的代码中,scasol将包含RandomBinaryString()的返回值,如图所示,总是返回一个空字符串。有评论说更多的代码在这里 - 大概代码不存在了吗?

你能解释一下你期望打印什么吗?