栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Java中创建魔术方块?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Java中创建魔术方块?

  • 您没有保存输入的内容,因此将0x0 square传递给
    isMagicSquare()
  • isMagicSquare()
    许多方面的实现是错误的。
    • 条件
      x > side
      应该是
      x < side
    • sumD
      只需要在计算完成后检查即可。
    • 您必须先进行初始化
      sumX
      sumY
      然后再计算它们。
    • 您应该使用
      +=
      而不是
      =+
      来计算总和。

纠正:

使代码保存输入

do{    System.out.print("Enter an int. Enter -1 when done>");    current = Integer.parseInt(in.nextLine());    if (current != -1) ints.add(current); // add this line to the loop to read the input}while(current != -1);

和正确的

isMagicSquare()

private static Boolean isMagicSquare(int[][] array){    int side = array.length;    int magicNum = 0;    for(int x = 0; x < side; ++x){        magicNum += array[0][x];    }    int sumD = 0;    for(int x = 0; x < side; ++x){        int sumX = 0;        int sumY = 0;        for (int y = 0; y < side; ++y){ sumX += array[x][y]; sumY += array[y][x];        }        sumD =+ array[x][x];        if(sumX != magicNum  || sumY != magicNum){ return false;        }    }    return sumD == magicNum;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455286.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号