如何以接收用户输入用于阵列,以然后使用油漆的方法? [英] How to receive user input for Array to then use for a paint method?

查看:93
本文介绍了如何以接收用户输入用于阵列,以然后使用油漆的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package package13;

import java.awt.Graphics;
import java.util.Scanner;

import javax.swing.JApplet;

public class BarChartApplet extends JApplet {


    final int baseX=30;
    final int baseY=100;
    final int width=10;
    final int space=30;

    //int height[]={30,20,40,90};

    int height[]=new int[4];

    System.out.println("Enter 4 heights of the bars");
    Scanner scan = new Scanner (System.in);

    for (int k=0;k<height.length;k++) {
    height[k]=scan.nextInt();
    }

    int upperLeftX;
    int upperLeftY;


    public void paint(Graphics g) {

        g.drawLine(5, 100, 200, 100);

        for (int i=0;i<height.length;i++) {

            upperLeftX=baseX+i*(width+space);
            upperLeftY=baseY-height[i];
            g.fillRect(upperLeftX,upperLeftY,width,height[i]);

        }

    }
}}

我试图获取用户输入一个数组的值。我使用这些阵列的值,然后使用设置高度在我创建条形图酒吧。那我遇到的问题是,当我试图创建一个 System.out.print 来要求用户输入值,因为我不使用它在我得到一个错误(公共静态无效的主要),但是当我尝试添加公共静态无效的主要,我得到paint方法错误。

I am trying to get user input for values of an array. I am using the values of these arrays to then use to set a height for the bars in the bar graph that I am creating. The problem that I am having is when I attempt to create a System.out.print to ask the user to enter values I get an error because I am not using it under (public static void main), but when I try to add public static void main, I get an error for the paint method.

任何帮助是极大AP preciated。

Any help is greatly appreciated.

推荐答案

这是学习Java的通病。因为这是应用程序开始执行,你必须声明公共静态无效的主要(字串[] args){} 方法。你可以认为这个方法为分离到类。它仍必须在类中声明的,但你可以在任何类中声明的方法,并用它做的事情,不同的是,如果你在另一个类中声明它则必须从该类执行程序。

This is a common problem with learning java. You must declare a public static void main(String[] args){} method because that's where the application starts executing. You can think of this method as being separate to your class. It must still be declared inside a class but you can declare the method in any class and do things with it, the difference is that if you declare it in another class you must then execute the program from that class.

为了使用从你的类方法你可以做两件事情之一:

In order to use methods from your class in main you can do one of two things:

声明的方法为静态。这意味着该方法不直接与该类别中的任一项的对象相关联,但仅仅是类本身的一部分。所以,如果你有一个类:

Declare the methods as static. This means that the method is not directly associated with any one object of that class but is just part of the class itself. So if you have a class:

public class Apple{
    public void eat(){}
    public static void describe(){}
}

那么你必须有一个具体的新的苹果yourApple()吃(); 吃的,但你可以去描述任何苹果苹果.describe(); 。其实这个例子有点虚构的,因为在现实生活中,你可以描述任何一个特定的苹果,以及作为一般的苹果,但这个Java情况下,你只能形容的平均苹果,如果你去新的苹果yourApple ().describe(); 这将等同于 Apple.describe()

then you have to have a specific new Apple yourApple().eat(); to eat but you can describe any apple by going Apple.describe();. In fact this example is a bit fictitious because in real life you can describe any one specific apple as well as a general apple but in this java case you can only describe an average apple, if you go new Apple yourApple().describe(); it will be equivalent to Apple.describe().

使用方法的这种静态的方式是困难的,因为它试图解决的java类格式在大项目扩大,但其作为一个快速的方法,使一个简单的例子。我已经种已显示的第二种方式,即实例化和它的作品通过苹果主类中并调用它的方法:

This static way of using methods is hard to expand over large projects because it tries to work around the class format of java, but its a good quick way to make a simple example. The second way that I've kind of already shown is called instantiating and it works by making an Apple inside your main class and calling methods on it:

public class Apple{
    public static void main(String[] args){
        Apple apple = new Apple();
        Apple.eat();
    }
    public void eat(){
        System.out.println("Nom");
    }
}

我希望帮助。

这篇关于如何以接收用户输入用于阵列,以然后使用油漆的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆