接受键盘输入(Accept keyboard input)-其他
接受键盘输入(Accept keyboard input)
接受键盘输入
Scanner类的使用
import简单介绍
package mypro01;
/**
* 测试Scanner类的使用,如何接收键盘的输入.
* @author 神奇的梦
*
*/
//java.lang包里面的内容我们可以直接用
//这里Scanner的使用需要导入java.util包的名称
import java.util.Scanner;
public class TestScanner {
public static void test01(){
// 这样就形成了和键盘输入的交互
// Scanner属于java.util包
// 调用构造器,传参System.in接收键盘的输入
// System.out是输出控制台
Scanner s =new Scanner(System.in);
// 输入你的字符串,返回你的字符串
String str=s.next();//程序运行到next会阻塞,等待键盘的输入!
// 打印你的内容
System.out.println(“刚才键盘输入:”+str);
}
public static void test02(){
Scanner s =new Scanner(System.in);
System.out.println(“请输入一个加数:”);
int a=s.nextInt();
System.out.println(“请输入被加数:”);
int b=s.nextInt();
int sum=a+b;
System.out.println(“两数相加和:”+sum);
}
public static void main(String[] args) {
test02();
}
}
Accept keyboard input
Use of scanner class
Brief introduction to import
package mypro01;
/**
* test the use of scanner class and how to receive keyboard input
* @ author # magical dream
*
*/
//java. We can use the contents of Lang package directly
//Here, the use of scanner needs to import Java Name of util package
import java.util.Scanner;
public class TestScanner {
public static void test01(){
//This forms the interaction with keyboard input
//Scanner belongs to Java Util package
//Call the constructor and pass the parameter system In receives input from the keyboard
// System. Out is the output console
Scanner s =new Scanner(System.in);
//Enter your string and return your string
String str=s.next();// When the program runs to next, it will block and wait for the input of the keyboard!
//Print your content
System. out. Println (“keyboard input just now:” + STR);
}
public static void test02(){
Scanner s =new Scanner(System.in);
System. out. Println (“please enter an addend:”);
int a=s.nextInt();
System. out. Println (“please enter the addend:”);
int b=s.nextInt();
int sum=a+b;
System. out. Println (“sum of two numbers:” + sum);
}
public static void main(String[] args) {
test02();
}
}