Java Input to the console
There are many types of inputs, But what today we are going to discuss is ,,, When a user types something from the keyboard to the java console this is called input, More types of inputs are text, video ,audio and action etc.
Syntax for java console inputs
1. When we give input to the java console , We should include a library in that class ( where we create our input ) at the top
import java.util.Scanner;
2. Create a Scanner object That way you can scan a data carefully And put System.in in the middle of round braces , So you can get the data from the user using this , As follow
3.
4.
5.
1. When we give input to the java console , We should include a library in that class ( where we create our input ) at the top
import java.util.Scanner;
2. Create a Scanner object That way you can scan a data carefully And put System.in in the middle of round braces , So you can get the data from the user using this , As follow
Scanner scan = new Scanner(System.in);
3.
scan.nextLine();
is use to give input in text form.Here scan is just the name of the Scanner object , we created above. While nextLine(); is essentially important to be given for inputs line.4.
nextInt();
is use to give inputs in numbers5.
nextDouble();
is use to give input in fraction numbers .That includes dot point.
Example 1 : Write down a program in which as a student his name use a string , ask for his/her marks use an integer and also ask for his/her % percentage use a double.
And then display back to him/her , his/her name , marks and percentage.
And then display back to him/her , his/her name , marks and percentage.
package clock;
import java.util.Scanner;
public class main {
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
/*declare 3 variable
*a string
* an integer
* and
* a double
*/
String name;
int marks;
double percentage;
System.out.println("what is your name?");
/*take name in the string*/
name = scan.nextLine();
System.out.println("How much marks you got?");
/* take marks in integer*/
marks = scan.nextInt();
System.out.println("What is your percentage?");
/*take percentage in double*/
percentage = scan.nextDouble();
/*display him/her
* name ,
* marks
* and result */
System.out.println("Your name is : ");
System.out.println(name + "\n");
System.out.println("You got : ");
System.out.println(marks +" marks\n");
System.out.println("Your percentage is : ");
System.out.println(percentage);
}
}
Out Put
what is your name?
abdul wahab
How much marks you got?
30
What is your percentage?
30.2
Your name is :
abdul wahab
You got :
30 marks
Your percentage is :
30.2
No comments:
Post a Comment