使用 while 循环绘制 java 中的点

Using while loops to plot points in java

大家好,我正在做一项作业,我必须使用 while 循环在一个范围内绘制几个不同的点,以便仅绘制输入的数字在可接受范围内的点。由于我是为 class 这样做的,所以我们使用 Bluej 和预制测试来确定问题是否已得到正确解决,但测试只会失败,而不会让我深入了解我做错了什么。我们分配的工作范围是 0 - 290,每当用户输入超出该范围的点时,循环应该打印 "Done",我每次尝试都会成功。我试过弄乱一些数字,看看是否有什么帮助,但我总是失败。我真的很感激任何对正在发生的事情的洞察力,谢谢。

 /**
 * Plotter.java
 * 
 */

//Put any imports below this line.
import java.util.Scanner;

/**
 * Short, one-line description of Plotter class here.
 * 
 * Optionally, include a paragraph that provides a more 
 * detailed description.
 *
 * @author Nicholas Thomas
 * @version 3/18/2018
 */
public class Plotter
{
    /** Main method of the program.
     * Method main
     *
     * @param args A parameter
     */
    public static void main(String[]args)
    {
        Plotter plotter = new Plotter();
        plotter.drawAxes();
        Scanner keyboard = new Scanner(System.in);
        plotter.plotPoints(keyboard);
    }

    /** Method dealing with x axis.
     * Method drawXAxis
     *
     */
    public void drawXAxis()
    {
        for (int x = 0; x < 300; x += 5)
        {
            new Square(x, 147);
        }
    }

    /** Method to draw ticks using x.
     * Method drawOneXTick
     *
     * @param x A parameter
     */
    public void drawOneXTick(int x)
    {
        for (int y = 137; y < 160; y += 5)
        {
            new Square(x, y);
        }
    }

    /** Method to draw all ticks.
     * Method drawAllXTicks
     *
     */
    public void drawAllXTicks()
    {
        for (int x = 7; x < 300; x+= 20)
        {
            drawOneXTick(x);
        }
    }

    /** Method dealing with y axis. 
     * Method drawYAxis
     *
     */
    public void drawYAxis()
    {
        for (int y = 0; y < 300; y += 5)
        {
            new Square(147, y);
        }
    }

    /** Method to draw one y tick.
     * Method drawOneYTick
     *
     * @param y A parameter
     */
    public void drawOneYTick(int y)
    {
        for (int x = 137; x < 160; x += 5)
        {
            new Square(x, y);
        }
    }

    /** Method to draw all y ticks.
     * Method drawAllYTicks
     *
     */
    public void drawAllYTicks()
    {
        for (int y = 7 ; y < 300; y+= 20)
        {
            drawOneYTick(y);
        }
    }

    /** Method to draw axes.
     * Method drawAxes
     *
     */
    public void drawAxes()
    {
        drawXAxis();
        drawYAxis();
        drawAllXTicks();
        drawAllYTicks();
    }

    /** Method to plot points.
     * Method plotPoints
     *
     * @param keyboard A parameter
     */
    public void plotPoints(Scanner keyboard)
    {
        int x = 0, y = 0;
        while (( x >= 0 && x <= 290) && (y >= 0 && y <= 290))
        {
            System.out.print("Enter an x and y coordinate: ");
            x = keyboard.nextInt(); 
            y = keyboard.nextInt();
            new Circle(x,y);
            System.out.print("Done");
        }
        while(( x < 0 && x > 290) && (y < 0 && y > 290))
        {
            System.out.print("Done");
        }
    }

    /** Method to plot points.
     * Method plotPointsProper
     *
     * @param keyboard A parameter
     */
    public void plotPointsProper(Scanner keyboard)
    {

    }
}

这部分特别给我带来麻烦,我只添加了第二个 while 循环以查看是否有帮助,但没有成功。

public void plotPoints(Scanner keyboard)
{
    int x = 0, y = 0;
    while (( x >= 0 && x <= 290) && (y >= 0 && y <= 290))
    {
        System.out.print("Enter an x and y coordinate: ");
        x = keyboard.nextInt(); 
        y = keyboard.nextInt();
        new Circle(x,y);
        System.out.print("Done");
    }
    while(( x < 0 && x > 290) && (y < 0 && y > 290))
    {
        System.out.print("Done");
    }
}

我得到的错误告诉我绘制了太多点,我想知道我的 x 和 y 变量的初始化是否与我的问题有关,或者它是否是我的方式'已经写了我的 while 循环?再次感谢你们的任何提示或建议。

我已经修改了您的 plotPoints 函数,使其适用于您指定的约束条件。

public void plotPoints(Scanner keyboard)
{
    int x = 0, y = 0;
    while (true)
    {
        System.out.print("Enter an x and y coordinate: ");
        x = keyboard.nextInt(); 
        y = keyboard.nextInt();
    if(( x < 0 || x > 290) || (y < 0 || y > 290)) break;
        new Circle(x,y);
    }
    System.out.print("Done");
}

在您分享的代码中,如果您输入 x>290,它仍然会调用 new Circle(x,y),循环只会在下一次迭代时终止。通过将条件放在 while 循环中,我们可以在需要时准确地中断循环。