2016-11-18 36 views
0

对于AP计算机科学,我创建了一个程序,你需要一个字,让一个盒子出来的:的BoxWord程序要么面临一个错误,或者它只是不能正常运行

亚军:

import static java.lang.System.*; 

public class BoxWordRunner 
{ 
    public static void main(String args[]) 
    { 
     BoxWord bw = new BoxWord(); 
     //bw.setWord(); 
     bw.toString(4); 
     System.out.println(bw); 
    } 
} 

其他程序:

import static java.lang.System.*; 

class BoxWord 
{ 
    private String word; 

    public BoxWord() 
    { 
     word=""; 
    } 

    public BoxWord(String s) 
    { 
     word = s; 
    } 

    public void setWord(String w) 
    { 
     word = w; 
    } 

    public String toString(int num) 
    { 
     int x=0; 
     int y=0; 
     int z=0; 
     String output = ""; 

     for(x=0;x<=num;x++) 
      for(y=x;y<=num-x;y++) 
       output += "*"; 
     for(z=num-x;z<=x;z--) 
      output = output + "#"; 
     output += "\n"; 
     return output + "\n"; 
    } 
} 

没有与跑步者的问题,尤其是这一行:

bw.setWord(); 

注释或删除它会导致程序运行,但不执行任何操作。 如果你把一个实际的字符串放在()中,就像“test”一样,也可以做同样的问题。留在一个错误的结果如图所示:

BoxWordRunner.java:13: error: method setWord in class BoxWord cannot be applied to given types; 
     bw.setWord(); 
     ^
    required: String 
    found: no arguments 
    reason: actual and formal argument lists differ in length 
1 error 
+0

我注意到在格式化你的代码时,你的''''''循环使用'z'变量不会为你的'x'或'y'循环的每次迭代执行。从空白和'z'循环来看,我认为你需要围绕你的''''''''''''''循环的'x'循环中的大括号{}来实现你想要的。 – dave

+0

另外你可能想'System.out.println(bw.toString(4));' –

回答

1

你需要一个字符串参数添加到您的bw.setWord();方法调用,如bw.setWord("hello");

你的toString方法令人困惑,没有括号包装你的for循环体。

相关问题