我试图通过使用for循环制作整个黄色方块,但它不起作用,我无法弄清楚。有人能帮我吗? [英] I'm trying to make entire yellow square by using for loops but it didn't work and i couldn't figure out. can some one help me?

查看:83
本文介绍了我试图通过使用for循环制作整个黄色方块,但它不起作用,我无法弄清楚。有人能帮我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int h = grid.getHt();
int w = grid.getWd();
for (int row = 0; row < h; ++row)
{
    for (int col = 0; col < w; ++ col)
    {
        grid.setColor (row, col, Color.YELLOW);

    }
}
for (int row = 4; row < ((4)+1); ++row)
{
    for (int col = 0; col < w; ++col)
    {
        grid.setColor (row, col, Color.YELLOW);
    }
}
for (int row = ((4)-1); row < ((4)+1); ++row)
{
    for (int col = 0; col < w; ++col)
    {
        grid.setColor (row, col, Color.YELLOW);
    }
}
for (int row = 4; row < h; ++row)
{
    for (int col = 0; col < w-(row+(h-1)); ++col)
    {
        grid.setColor(row, col, Color.YELLOW);
    }
}
for (int row = 0; row < h/2; ++row)
{
    for (int col = 0; col < row+2; ++col)
    {
        grid.setColor (row, col, Color.YELLOW);
    }
}





我尝试了什么: < br $>


i知道必须使用for循环但是我不知道要做这个复杂的问题



What I have tried:

i know have to use for loop but I have no idea to do this complicated problem

推荐答案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



要填充一个矩形,你只需要这个(假设你调用正确的API函数)

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

To fill a rectangle, you need no more than this (assuming you call the right API functions)
int h = grid.getHt();
int w = grid.getWd();
for (int row = 0; row < h; ++row)
{
    for (int col = 0; col < w; ++ col)
    {
        grid.setColor (row, col, Color.YELLOW);
    }
}



使用调试器,您需要检查 h 的值和 w 符合您的预期。


With the debugger, you need to check the values of h and w against your expectations.


这篇关于我试图通过使用for循环制作整个黄色方块,但它不起作用,我无法弄清楚。有人能帮我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆