2011-03-22 112 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()的返回值,如圖所示,總是返回一個空字符串。有評論說更多的代碼在這裏 - 大概代碼不存在了嗎?

你能解釋一下你期望打印什麼嗎?