Home  >  Article  >  Java  >  How to input a number from the keyboard in java

How to input a number from the keyboard in java

WBOY
WBOYforward
2023-04-30 17:46:143316browse

Keyboard input allows users to input data into the program while the program is running, achieving the effect of human-computer interaction and improving user experience.

1. Keyboard input process

(1) The import package (importjava.util.Scanner) tells the program where this tool is.

(2) Create object (Scannersc=newScanner(http://System.in).

(3) Get data (intnum=sc.nextInt();) Use tools.

2. Example

import java.util.Scanner;
 
public class Test{
 
public static void main(String[] args){
 
Scanner sc = new Scanner(http://System.in);
 
System.out.println("请输入您的年龄:");
 
int age = sc.nextInt();
 
if(age<18){ System.out.println("毛头小子");
 
}else{ System.out.println("成年人了");
 
}
 
}
 
}

The above is the detailed content of How to input a number from the keyboard in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete