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

时间:2012-10-13 01:21:21

标签: java eclipse

我在计算机科学课程中,我正在努力开发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;


          }

            }

    }

P.S。我不是计算机科学专业,事实上,当涉及到这些东西时,我是一个尼安德特人。请理解我的局限性。

1 个答案:

答案 0 :(得分:1)

如果Dave Newton回答了这个问题,我肯定会支持这个答案。我想补充一点,你想设置gameover何时设置为真的条件,就像你使用rightDirection一样(当然,除了这是假的)。

我会在循环结束时添加:

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

我假设您希望汽车停在房子的后端,与它开始的位置相反。希望有所帮助!