Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyClass
- {
- // Variable declarations
- Scanner input;
- String someString;
- int someInt;
- // etc...
- public static void main(String[] args)
- {
- // This is where you put code that will run when you run your application
- MyClass mc = new MyClass();
- // This will run our "getInt()" method defined below
- // If you look at the method, you'll see that it prompts
- // the user to enter an integer, then sets "someInt" to whatever they typed
- mc.setInt();
- // And this calls setString defined below, which sets "someString"
- mc.setString()
- // And then we can print the value of those variables now that we've prompted the user to change them
- System.out.println("You entered: " + mc.someInt);
- System.out.println("And then entered: " + mc.someString);
- }
- // You'll typically have a constructor here
- // A constructor is where you'll set default values for your variables (which we declared at the top of the file)
- public MyClass()
- {
- input = new Scanner(System.in);
- someString = "";
- someInt = 100;
- }
- // And then you can create different helper methods here
- public void setInt()
- {
- System.out.println("Enter an int");
- someInt = input.nextInt();
- }
- public void setString()
- {
- System.out.println("Enter a string");
- someString = input.nextString();
- }
- }
Add Comment
Please, Sign In to add comment