简体   繁体   中英

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

How do I start doing that on java, I mean, I start with two diferent variables? One for Int (which is the whole numbers, 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM