Home > Java > javaTutorial > body text

How to input a number from the keyboard in java

WBOY
Release: 2023-04-30 17:46:14
forward
3256 people have browsed it

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("成年人了");
 
}
 
}
 
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!