简体   繁体   English

Eclipse无法识别我的源代码中的循环

[英]Eclipse is not recognizing the loops in my source code

I am in a computer science course and I am struggling with loops in developing a program in Eclipse. 我正在上一门计算机科学课程,并且在使用Eclipse开发程序中遇到循环问题。 It has taken me an embarrassing number of hours to get this far. 我花了令人尴尬的时间才能达到目标。

This homework assignment requires that the animated cars in my program for the top three houses travel from the right side of the top three houses to the left side, passing in front of the houses. 这项作业要求我程序中前三座房屋的动画车从前三座房屋的右侧移动到左侧,并经过房屋的前面。 The two lower houses must have the cars traveling from the left to the right, passing in the back of the houses. 下部的两个房屋必须使汽车从左向右行驶,并穿过房屋的后部。

When I run the program as is, the cars do not initially appear as they should and when they do appear they move across the entire screen. 当我按原样运行程序时,汽车最初并没有出现应有的外观,当它们出现时它们会在整个屏幕上移动。 The cars are all moving in the appropriate directions, but do not initially appear when they should and do not stop where they are supposed to. 这些汽车都朝着适当的方向移动,但最初并没有出现在应有的位置,也没有在应有的位置停下来。

I realize that the program is not recognizing my loop for the car's animation. 我意识到该程序无法识别我的汽车动画循环。 I cannot understand why this is - not to mention how to resolve the problem. 我不明白为什么会这样-更不用说如何解决问题了。

Here is my code copied from Eclipse: 这是我从Eclipse复制的代码:

import java.awt.*;

import graphics.*;

import javax.swing.*;

public class NeighborhoodDrives {


    public static void main(String[] args) {


         // The GraphicsWindow object
          GraphicsWindow win = new GraphicsWindow("Window",1400,800);   
          // The Graphics object
          Graphics page = win.getGraphics();
          // The car facing right
          Image carRight = new ImageIcon("carright.gif").getImage();
          // The car facing left
          Image carLeft = new ImageIcon("carleft.gif").getImage();

          // The five cars' initial placements
          int x1 = 0, x2 = 450, x3 = 900, x4 = 550, x5 = 990;
          int y1 = 338, y2 = 338, y3 = 338, y4 = 625, y5 = 625;
          Color houseColor = Color.magenta;
          boolean gameOver = false;
          boolean rightDirection = true;

          while (!gameOver)   // Creates a loop
          {
              win.clear();     // Clears the window

              int mid = 300;
              int top = 50;


              if (!rightDirection)    // Creates the loop to make the car drive in back of the house from the left to the right sides of the  2 lower houses
                    page.drawImage(carRight,  x4, y4, null);       

                if (!rightDirection) 
                      page.drawImage(carRight,  x5, y5, null);

                // Creates 5 different colored houses in 5 different places 
                int count = 0;
                while (count < 5) {
                    switch (count) {

                    case 0:
                        mid = 250;
                        top = 50;
                        // Makes a magenta house
                        houseColor = Color.magenta;
                        break;

                    case 1:
                        mid = 700;
                        top = 50;
                        // Makes a yellow house
                        houseColor = Color.yellow;
                        break;

                    case 2:
                        mid = 1150;
                        top = 50;
                        // Makes a green house
                        houseColor = Color.green;
                        break;

                    case 3:
                        mid = 450;
                        top = 350;
                        // Makes a light blue, cyan, house
                        houseColor = Color.cyan;
                        break;

                    case 4:
                        mid = 900;
                        top = 350;
                        // Makes a red house
                        houseColor = Color.red;
                        break;

                    }


                page.setColor(houseColor);

                page.fillRect(mid - 125, top + 130, 200, 200); // fronts of houses

                // Sets color of the roofs
                page.setColor(Color.darkGray);
                page.fillRect(mid - 150, top + 125, 250, 35); // roofs of houses
                page.fillRect(mid - 100, top + 100, 150, 40);
                page.fillRect(mid - 75, top + 85, 100, 20);
                page.fillRect(mid - 45, top + 65, 50, 20);
                page.fillRect(mid - 33, top + 55, 25, 20);

                // Sets color of the doors
                page.setColor(Color.blue);
                page.fillRect(mid - 50, top + 245, 50, 85); // doors of houses

                // Sets color of the door knobs
                page.setColor(Color.black);
                page.fillOval(mid - 15, top + 285, 10, 10); // door knobs

                // Sets color of windows on the houses
                page.setColor(Color.white);
                page.fillRect(mid - 100, top + 185, 45, 45); // second story windows on
                                                                // left of houses

                // Sets color of windows on the houses
                page.setColor(Color.white);
                page.fillRect(mid + 10, top + 185, 45, 45); // second story windows on
                                                            // right of houses

                // Sets color of window panes on the houses
                page.setColor(Color.red);
                page.drawLine(150, 258, 195, 258); // panes of window on left of magenta house
                page.drawLine(172, 236, 172, 281);
                page.drawLine(263, 258, 303, 258); // panes of window on right of magenta house
                page.drawLine(282, 236, 282, 281);
                page.drawLine(600, 258, 645, 258); // panes of window on left on yellow house
                page.drawLine(622, 236, 622, 281);
                page.drawLine(710, 258, 755, 258); // panes of window on right on yellow house
                page.drawLine(732, 236, 732, 281); 
                page.drawLine(1050, 258, 1095, 258); // panes of window on left on green house
                page.drawLine(1072, 236, 1072, 281);
                page.drawLine(1160, 258, 1205, 258); // panes of window on right on green house
                page.drawLine(1182, 236, 1182, 281);
                page.drawLine(350, 558, 395, 558); // panes of window on left on cyan house
                page.drawLine(372, 536, 372, 581);
                page.drawLine(460, 558, 503, 558); // panes of window on right on cyan house
                page.drawLine(482, 536, 482, 581);
                page.drawLine(800, 558, 845, 558); // panes of window on left on red house
                page.drawLine(822, 536, 822, 581);
                page.drawLine(910, 558, 955, 558); // panes of window on right on red house
                page.drawLine(932, 536, 932, 581);
                count++;

                }   

                  if (rightDirection)
                  x1 += 10;
                  else 
                      x1 -= 10;


                  if (rightDirection)
                  x2 += 10;
                  else \
                      x2 -= 10; 


                  if (rightDirection)
                  x3 += 10;
                  else 
                      x3 -= 10; 

                    if (!rightDirection)
                          page.drawImage(carLeft,  x1, y1, null);    

                        if (!rightDirection)
                              page.drawImage(carLeft,  x2, y2, null);

                        if (!rightDirection)
                             page.drawImage(carLeft,  x3, y3, null);

                  if (rightDirection)
                  x4 -= 10;  
                  else
                      x4 += 10;


                  if (rightDirection)
                      x5 -= 10;
                  else
                          x5 += 10;

                     if (x1 > 450 && x2 > 900 && x3 > 1300 && x4 < 150 && x5 < 550)    // Signifies when the car has completely driven across the front of the house
                          rightDirection = false;

                  win.repaint();   // Redraw the screen
                  win.pause(40);

                     if (x1 > 450 && x2 > 900 && x3 > 1300 && x4 < 150 && x5 < 550)    // Signifies when the car has completely driven across the front of the house
                          rightDirection = false;


          }

            }

    }

PS I am not a computer science major, in fact I am a neanderthal when it comes to this stuff. PS:我不是计算机科学专业的学生,​​实际上,在这方面我是一名尼安德特人。 Please be understanding of my limitations. 请了解我的限制。

If Dave Newton had answered this question, I would have definitely supported that answer. 如果Dave Newton回答了这个问题,我肯定会支持该回答。 I would add that you want to set the conditions for when gameover should be set to true like you do with rightDirection (except that's false, of course). 我要补充一点,就是您要为将gameover时间设置为true的条件设置条件,就像使用rightDirection (当然,这是错误的)。

I would add at the end of your loop: 我将在循环末尾添加:

    if(!rightDirection && x1 <= 450 && x2 <= 900 && x3 <= 1300 && x4 >= 150 && x5 >=550){
              gameover = true; 
    }

I am making the assumption you wanted the car to stop on the back-end of the house opposite from the position it started. 我假设您想让汽车停在房屋的后端,与起始位置相反。 Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM