1.输入字符串
import java.util.Scanner;
class Example {
public static void main(String[] args){
int i=0,j;
String[] arr = new String[13];
Scanner scan=new Scanner(System.in);
//next(),nextLine()获取字符串
String tmp = scan.next();//输入arr[0];
while(tmp!=null&&i!=4){//循环输入arr[1]-arr[4]
arr[i++]=tmp;
tmp=scan.next();
}
for(j=0;j<5;j++){循环输出arr[0]-arr[4]
System.out.println(arr[j]);
}
}
}
import java.util.Scanner;
public class Nextline {
public static void main(String[] args){
int i=0,j;
String[] arr = new String[13];
Scanner scan=new Scanner(System.in);
//next(),nextLine()获取字符串
//String tmp = scan.next();//输入arr[0];
while(i!=4){//循环输入arr[1]-arr[4]
arr[i++]=scan.nextLine();
}
for(j=0;j<4;j++){循环输出arr[0]-arr[4]
System.out.println(arr[j]);
}
}
}
2.输入数字
import java.util.Scanner;
public class Shuzi {
public static void main (String[] args){
int i=0,a;
float f=0.0f;
double d=0.0;
Scanner scan=new Scanner(System.in);
/*hasNextxxx()验证,hasNextInt()方法是判断控制台接收是否为数字,
当你在控制台输入一个字符的时候,hasNextInt()判断你输入这个字符是不是数字,
而不是接收值,当if判断通过之后nextInt()执行接收,也就是你输入的那个字符*/
while(scan.hasNextInt()&&(i!=2)) //循环输入整型数字
{
a = scan.nextInt();
System.out.println(a);
i++;
}
f=scan.nextFloat();//输入float型数字
System.out.println(f);
d=scan.nextDouble();//输入double型数字
System.out.println(d);
}
}
3.输入char型
import java.util.Scanner;
import java.io.IOException;
public class Example2 {
public static void main (String[] args)throws IOException{
char c=(char)System.in.read();
System.out.println(c); //char变int型
}
}
import java.util.Scanner;
public class Example2 {
public static void main (String[] args){
int i=0,a;
float f=0.0f;
double d=0.0;
Scanner scan=new Scanner(System.in);
a = scan.nextInt();
System.out.println((char)a);//整型变char型
i++;
f=scan.nextFloat();
System.out.println((int)f);//float型变int型
d=scan.nextDouble();
System.out.println((int)d);//double型变int型
char c=(char)System.in.read();
System.out.println((int)(c-48)); //char变int型
}
}
5.BufferedReader输入字符串
import java.io.*;
public class Bufferedreader1 {
public static void main (String[] args)throws IOException{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String str=buf.readLine();
System.out.println(str);
}
}