课外呼叫方法

时间:2016-01-19 23:05:12

标签: java

所以我试图在不同的类中调用我的方法。

这是一个简单的任务,我在这里看了很多教程和其他帖子,但由于某种原因我无法让它工作。

这是第一堂课

public class ArrayProcessor {

   public void print3IntArray(int[] array) {
      for (int i = 0; i < 3; i++) {
         System.out.println("Entry " + i + " is " + array[i]);
      }
   }
}

我第一次尝试

import java.util.Random;

public class ArrayProgram {

   public static void main(String[] args) {

   Random rand = new Random();

   int[] anArray = new int[3];

      for(int i = 0; i < 3; i++) {
         int random = rand.nextInt(11);
         anArray[i] = random;
      }
   }
   ArrayProcessor.print3IntArray(anArray);
}

但是在看了这个网站上的一些帖子之后我试过了

import java.util.Random;

public class ArrayProgram {

   public static void main(String[] args) {

   ArrayProcessor ap = new ArrayProcessor();

   Random rand = new Random();

   int[] anArray = new int[3];

      for(int i = 0; i < 3; i++) {
         int random = rand.nextInt(11);
         anArray[i] = random;
      }
   }
   ap.print3IntArray(anArray);
}

这两个代码都给我以下错误:

ArrayProgram.java:21: error: <identifier> expected
   ap.print3IntArray(anArray);
                    ^
ArrayProgram.java:21: error: <identifier> expected
   ap.print3IntArray(anArray);
                            ^
2 errors

2 个答案:

答案 0 :(得分:1)

你在说这个

ap.print3IntArray(anArray);

main方法的结束大括号之后,因此您的代码实际上不驻留在任何方法或容器代码块中,这是编译器无法接受的。变化

} // closing curly brace of main()
ap.print3IntArray(anArray);

    ap.print3IntArray(anArray);
} // closing curly brace of main()

答案 1 :(得分:0)

你不能打电话 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Rectangle2D; import java.util.Map; import java.util.TreeMap; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.Scrollable; public class Main { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new Main(); } }); } public Main() { // For this example, I just randomised some data, you would // Need to load it yourself... int width = 256; int height = 256; int[][] data = new int[width][height]; for (int c = 0; c < height; c++) { for (int r = 0; r < width; r++) { data[c][r] = (int) (256 * Math.random()); } } Map<Integer, Integer> mapHistory = new TreeMap<Integer, Integer>(); for (int c = 0; c < data.length; c++) { for (int r = 0; r < data[c].length; r++) { int value = data[c][r]; int amount = 0; if (mapHistory.containsKey(value)) { amount = mapHistory.get(value); amount++; } else { amount = 1; } mapHistory.put(value, amount); } } JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new JScrollPane(new Graph(mapHistory))); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } protected class Graph extends JPanel implements Scrollable { protected static final int MIN_BAR_WIDTH = 4; private Map<Integer, Integer> mapHistory; public Graph(Map<Integer, Integer> mapHistory) { this.mapHistory = mapHistory; } @Override public Dimension getPreferredSize() { int width = (mapHistory.size() * MIN_BAR_WIDTH) + 11; return new Dimension(width, 256); } @Override public Dimension getMinimumSize() { int width = (mapHistory.size() * MIN_BAR_WIDTH) + 11; return new Dimension(width, 128); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (mapHistory != null) { int xOffset = 5; int yOffset = 5; int width = getWidth() - 1 - (xOffset * 2); int height = getHeight() - 1 - (yOffset * 2); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.DARK_GRAY); g2d.drawRect(xOffset, yOffset, width, height); int barWidth = Math.max(MIN_BAR_WIDTH, (int) Math.floor((float) width / (float) mapHistory.size())); int maxValue = 0; for (Integer key : mapHistory.keySet()) { int value = mapHistory.get(key); maxValue = Math.max(maxValue, value); } int xPos = xOffset; for (Integer key : mapHistory.keySet()) { int value = mapHistory.get(key); int barHeight = Math.round(((float) value / (float) maxValue) * height); g2d.setColor(new Color(key, key, key)); int yPos = height + yOffset - barHeight; Rectangle2D bar = new Rectangle2D.Float( xPos, yPos, barWidth, barHeight); g2d.fill(bar); g2d.setColor(Color.DARK_GRAY); g2d.draw(bar); xPos += barWidth; } g2d.dispose(); } } @Override public Dimension getPreferredScrollableViewportSize() { return new Dimension(512, 256); } @Override public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { return 128; } @Override public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { return 128; } @Override public boolean getScrollableTracksViewportWidth() { return getPreferredSize().width <= getParent().getSize().width; } @Override public boolean getScrollableTracksViewportHeight() { return getPreferredSize().height <= getParent().getSize().height; } } }  在方法外部(在本例中为main方法)但是如果你想在main方法中调用它,那么在ArrayProcessor类中使函数print3IntArray为静态。

ArrayProcessor.print3IntArray(anArray);