开发者

Basic Question, how to read a chart (i.e. a user input his age, sex, social number...) - Java

开发者 https://www.devze.com 2023-03-02 00:44 出处:网络
How do I start doing that on java, I mean, I start with two diferent variables? One for Int (which is the whole num开发者_运维百科bers, such as age and social number) and String or anything else for t

How do I start doing that on java, I mean, I start with two diferent variables? One for Int (which is the whole num开发者_运维百科bers, such as age and social number) and String or anything else for the words inputs.

And after reading that, we have to take the avarage age in the "group" of people who used that program also, show the total number of people who participated.

My concept went a bit like this:

import java.util.*;

public class apple {
    public static void main(String args []) {
           Scanner input = new Scanner(System.in);
           int age, snum;
           String sex, exp = yes or no;
           //aaand i donno how to proceed here. >*<
    }
}


Personally, I wouldn't shove everything in your main method.

I'd have a class called say:

Person, which has private variables like name, gender, age etc. And you'd have public modifier methods to get to those (your sets and gets)

For example:

public class Demo
{
    private String fred;

    public void setFred(String theFred)
    {
        fred = theFred;
    }

    public String getFred()
    {
        return fred;
    }
}

Then you'd have a generic input method inside your class, which would deal with the input then you'd do something like:

public void getInput()
{
    // get your input stream...then do:
    System.out.println("Please Enter your name: ");
    person.setName(scanner.getString()); // or whatever you need to do to get the data you entered.
    // and repeat...
}

And in your static main then, you just call the getInput method, that way you will learn to write proper methods, and the code will be much simpler to understand (at least in my view)

0

精彩评论

暂无评论...
验证码 换一张
取 消