Insane Hacks
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Tut - User Input

Go down

Tut - User Input Empty Tut - User Input

Post  LightzOut Fri Nov 27, 2009 10:32 am

Code:


/*
 * Created by LightzOut
 * Tutorial for using user input
 * Create 11/27/09
*/


import java.util.Scanner; // First add this bit of code

public class UserInputTutMain // Add your class like normal - makes sure its named properly.. (i.e. its the name of your class file)
{
      private static Scanner in = new Scanner(System.in); // Add this bit of code as well
   
      public static void main(String[] args) // Then you make your main method like usual
      {
         // Alright, to use user input, all you have to do is the following - Ill give an example for each data type
         System.out.print("Whats your name? ");
         String name = in.next(); // you can also put for strings - in.nextLine() - The difference is when you add "Line" it just makes the code stop when you "enter" instead of space.
         
         System.out.print("How old are you? ");
         int age = in.nextInt();
            
         System.out.print("What is your GPA? ");
         double gpa = in.nextDouble();
         
         System.out.print("Are your parents divorced? (true/false): ");
         boolean divorce = in.nextBoolean();
         
         // you will notice that i am leaving out char. This is because it is a lot different than the others when it comes to user input. For now you can look it up on google or something if you really need it, i will add it in later when i have the time.
         
         System.out.println("Your name is " + name + ", you are " + age + " years old, you have a " + gpa + " GPA and it is " + divorce + " that your parents are divorced.");
         
         
      }
      
   
}


/* Output:
    
    Whats your name? LightzOut
   How old are you? 17
   What is your GPA? 3.2
   Are your parents divorced? (true/false): false
   Your name is LightzOut, you are 17 years old, you have a 3.2 GPA and it is false
   that your parents are divorced.
   Press any key to continue...
   
*/


If you have any questions, just leave a comment.
If you need a tut on how to use IDE's (how to compile this source) then go here: http://www.dxtgaming.com/forums/showthread.php?t=30826


-Head Admin
LightzOut
LightzOut
LightzOut
Head Admin

Posts : 12
Join date : 2009-11-26
Age : 32
Location : With your mom

https://insanehacks.board-directory.net

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum