Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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 有没有比我的方法更好的方法?_Java_For Loop - Fatal编程技术网

Java 有没有比我的方法更好的方法?

Java 有没有比我的方法更好的方法?,java,for-loop,Java,For Loop,我在一次工作面试中被问到以下问题。我被要求使用*字符进行一种形式的“填充打印”。以下是我作为答案提供的代码(java): 编辑: 如下所示:用户输入3: x x x x x x * * * x x * * * x x * * * x x x x x x> public class asterisk { public static void main (String args[]){ int input,ast; Scanner scan =

我在一次工作面试中被问到以下问题。我被要求使用
*
字符进行一种形式的“填充打印”。以下是我作为答案提供的代码(java):

编辑:

如下所示:用户输入3:

x x x x x
x * * * x
x * * * x
x * * * x 
x x x x x>


public class asterisk {

    public static void main (String args[]){
        int input,ast;
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter number: ");
        input = scan.nextInt();


        if(input>0) {
            topBottom(input);
            for(int x=1; x<=input; x++){
                System.out.print("x ");
                for( ast=1; ast<=input; ast++) {
                    System.out.print("* ");

                }
                System.out.print("x ");
                System.out.println();
            }
            topBottom(input);
        } else {
            System.out.print("x ");
        }    
    }

    public static void topBottom(int input) {           
        for(int top = 1; top<=input+2; top++ ) {
            System.out.print("x ");
        }
        System.out.println();
    }
x
x***x
x***x
x***x
x>
公共类星号{
公共静态void main(字符串参数[]){
int输入,ast;
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入编号:”);
输入=scan.nextInt();
如果(输入>0){
上下(输入);

对于(intx=1;x,您的代码很好,但有一些建议。 按照惯例,方法应该从动词开始。使用<代码> TopStudio//Case>函数是有问题的。我发现代码比任何东西都更让人困惑。考虑可读性和效率。

这样的方法更容易阅读,并且不包括额外的方法

对于n+2行中的n+2个字符

for(int i=0; i<input+2; i++) {
  for(int j=0; j<input+2; j++) {
对于所有其他行,打印第一个和最后一个字符的
X
,否则打印
*

    else {
      if(j == 0 || j == input+1) {
        System.out.print("X ");
      } else {
        System.out.print("* ");
      }
    }
最终结果:

for(int i=0; i<input+2; i++) {
  for(int j=0; j<input+2; j++) {
    if(i == 0 || i == input+1) {
      System.out.print("X ");
    } else {
      if(j == 0 || j == input+1) {
        System.out.print("X ");
      } else {
        System.out.print("* ");
      }
    }
  }
  System.out.println();
}

for(int i=0;i微小更改,@Michael code),以打印下一行并打印内部循环中的字符

      //  y = column
      for(int y=0; y < input; y++){     
        //  x = row
        for(int x=0; x< input; x++){
          nextChar = (x == 0 || y == 0 || (x+1) == input 
            || (y+1) == input) ? BORDER : FILLING;
            System.out.print(nextChar);
        }
        System.out.println();
      }
//y=column
对于(int y=0;y
本练习的目标不是让您的代码变得“高效”算法。这只是看看你是否理解循环。试着让它可读,使用简短、命名良好的方法,保持间距一致,正确缩进代码。此外,在Java中循环通常从0开始。变量在最后一刻在最窄的范围内声明和初始化。对于初学者:public final static StringCROSS=“x”公共最终静态字符串星号=“*”;-然后使用System.print(星号);或CROSS@MichaelD.-如果要输入代码,请“回答”不要评论…难以阅读的无格式代码..代码看起来不错。像Michael D建议的那样让它更具可读性。嗯,这看起来很有趣。看起来与我的答案有点不同,但我看到了逻辑,我也会尝试研究这一个。非常感谢@JordoniasNice解释。另外,你可以将2个如果折叠成一个:if((i==0 | | i==input+1)| | |(j==0 | | j==input+1))通过将nextChar行更改为:nextChar=(x==0 | | y==0),可以省略System.out.println();边框:((x+1)==input |(y+1)=input)?BORDER.“\n”:填充;
      //  y = column
      for(int y=0; y < input; y++){     
        //  x = row
        for(int x=0; x< input; x++){
          nextChar = (x == 0 || y == 0 || (x+1) == input 
            || (y+1) == input) ? BORDER : FILLING;
            System.out.print(nextChar);
        }
        System.out.println();
      }