在带有for循环的if语句中使用数组时找不到符号

时间:2015-03-25 19:51:20

标签: java arrays loops if-statement for-loop

我在代码底部(第150行)找不到符号 - 方法占用者(int)可能遗漏了一些简单但无法解决的问题。只需要帮助这部分,该程序远非不完整。

import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class Accommodation extends JFrame
    implements ActionListener
    {

        /** Window layout constants (these need to be "static" to be referred to in main) */
        private static final int panelWidth = 700;
        private static final int panelHeight = 300;

        /** The apartment block data structure */
        private final int apartments = 10;                   // Total number of apartments (numbered from 1 for the user, but from 0 in the array!)
        private String[] occupant;                              // Occupant information: one string per apartment
        private int selectedApartment;                       // To identify the currently selected apartment

        /** Configuration constants for the display */
        private final int aptHeight = 40;                    // Dimensions for individual apartment rectangles
        private final int aptWidth = 50;

        /** A useful colour */
        private final Color emptyColor = Color.green;

        /** Graphical interface components */
        private JButton next;         // Dummy selection button

        /** The drawing panel. */
        private JPanel panel;

        /**
         *  The main program launcher for the Accommodation application.
         *
         * @param  args  The command line arguments (ignored here).
         */
        public static void main( String[] args )
        {
            Accommodation frame = new Accommodation();
            frame.setSize( panelWidth+40, panelHeight+200 );
            frame.setUpAccommodation();
            frame.setUpGUI();
            frame.setVisible( true );
            frame.setTitle("Accomodation");
            frame.setLocationRelativeTo(null);
        } // End of main

        /** Organizes overall set up of the apartment block details at launch time. */
        private void setUpAccommodation()
        {
            // Create the occupant array and fill in first two apartments.
            // The other elements need to contain "Empty"
            occupant = new String[apartments];
            occupant[0] = "Luke Skywalker";                          // Occupants in apartments 1 and 2
            occupant[1] = "The Hulk";
            occupant[2] = "Empty";
            occupant[3] = "Empty";
            occupant[4] = "Empty";
            occupant[5] = "Empty";
            occupant[6] = "Iron Man";
            occupant[7] = "Empty";
            occupant[8] = "Empty";
            occupant[9] = "Empty";

            selectedApartment = 0;                             // Apartment 1 initially selected
        } // End of setUpAccommodation

        /** Organizes overall set up of the GUI at launch time. */
        private void setUpGUI()
        {
            // Now set up the initial GUI: window, button, graphics panel and background colours.
            setDefaultCloseOperation( EXIT_ON_CLOSE );
            Container window = getContentPane();
            window.setLayout( new FlowLayout() );

            window.setBackground( Color.gray );

            // Set up the next button
            next = new JButton("Next");
            window.add(next);
            next.addActionListener(this);

            // Set up the drawing panel
            panel = new JPanel()
            {
                // paintComponent is called automatically when a screen refresh is needed
                public void paintComponent(Graphics g)
                {
                    // g is a cleared panel area
                    super.paintComponent(g); // Paint the panel's background
                    paintScreen(g);          // Then the required graphics
                }
            };
            panel.setPreferredSize( new Dimension( panelWidth, panelHeight ) );
            panel.setBackground( Color.white );
            window.add( panel );



        } // End of setUpGUI

        /**
         *  Handle button clicks
         *
         * @param  e  Information about the button click
         */
        public void actionPerformed( ActionEvent e )
        {
            // In this template there is only a Next button.
            // In the second stage (up to 69 marks) it should causes the display to be updated
            // to show the details of the next occupant of the apartment block.
            // Here is an indication of what it should do:

            // Adjust the selected apartment indicator
            selectedApartment++;
            // And refresh the display
            repaint();
        } // End of actionPerformed

        /**
         *  Display the apartment block, and the details of the occupant of the selected apartment.
         *
         * @param  g  The Graphics area to be drawn on, already cleared.
         */
        private void paintScreen( Graphics g )
        {
            // Now draw the display
            displayStatus(g);
            displayBlock(g);

        } // End of paintScreen

        /** Display information about the currently selected apartment */
        private void displayStatus(Graphics g)
        {
            // Just information about apartment 1 in this template
            g.setColor(Color.black);
            g.drawString(occupant[0], 190, 70);
        } // End of displayStatus

        /** Draw the apartments themselves: boundary, empty/occupied colouring, number */
        private void displayBlock(Graphics g)
        {

            for(int i = 0; i < 10; i++)
            {           
                if (occupant(i).equals("Empty"))
                {
                    g.setColor(Color.green);
                    g.fillRect(150+(i*50), 120, aptWidth, aptHeight); //Sets the rectangle red to show the room is occupied

                }
                else
                {
                    g.setColor(Color.red);
                    g.fillRect(150+(i*50), 120, aptWidth, aptHeight); //Sets the rectangle green to show the room is free
                }
                g.setColor(Color.black);
                g.drawRect(150+(i*50), 120, aptWidth, aptHeight);

                // Draws the numbers in the Corresponding boxes
                g.setColor(Color.black);  
                g.drawString(Integer.toString(i+1), 150+(i*50)+15, 120+25);  
            }

            //Drawing the key allowing the user to see what the colours refer to
            g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
            g.drawString("Key", 20,120);
            g.drawString("Occupied",60,150);
            g.drawString("Empty", 60, 170);           

            // Drawing the boxes to show what the colours mean in the key             
            g.setColor(Color.red);
            g.fillRect(20, 130, 25, 25);
            g.setColor(Color.black);
            g.drawRect(20, 130, 25, 25);
            g.setColor(Color.green);
            g.fillRect(20,150,25,25);
            g.setColor(Color.black);
            g.drawRect(20,150,25,25);




        } // End of displayBlock

    } // End of Accommodation

2 个答案:

答案 0 :(得分:1)

occupant(i)应为occupant[i],因为它是一个数组,而不是一个方法

答案 1 :(得分:0)

occupant(i)

这是方法调用

occupant[i]

这是数组i

中的元素occupant