您的位置:

java输入字符,java输入字符串统计各个字符个数

本文目录一览:

java中怎么中键盘输入字符串

首先,导入java.util.*包。

import java.util.*;

然后,你需要新建一个读取标准输入(键盘)的扫描器对象。

Scanner in = new Scanner(System.in);

现在,你可以从键盘输入字符串了。

String s = in.nextLine();

以上这一行把键盘输入的一行字符串读取到变量 s 中。

请看一个完整的简单示例:

import java.util.*;

public class Main

{

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

String s = in.nextLine();

System.out.println(s);

}

}

Java的常用输入输出语句?

常用的输入语句是:

输入字符串:new Scanner(System.in).next();

输入整数:new Scanner(System.in).nextInt();

输入小数:new Scanner(System.in).nextDouble();

常用的输出语句:

换行输出: System.out.println(变量或字符串);

非换行输出: System.out.print(变量或字符串);

换行输出错误提示(默认是红字):System.err.println(变量或字符串);

不换行输出错误提示(默认是红字): System.err.print(变量或字符串));

java中如何输入一个字符

import java.util.*;

public class Test_01

{

public static void main(String[] args)throws Exception

{

System.out.println("请输入一个字符");

char c=(char)System.in.read();

System.out.println(c);

}

}

扩展资料:

还可以输入字符串,输入字符串的方法

import java.io.*;

public class Test

{

public static void main(String[] args) throws IOException

{

BufferedReader buf = new BufferedReader (new InputStreamReader(System.in));

BufferedWriter buff = new BufferedWriter(new FileWriter("abc.txt"));

String str = buf.readLine();

while(!str.equals("exit"))

{

buff.write(str);

buff.newLine();

str = buf.readLine();

}

buf.close();

buff.close();

}

}

Java中怎么输入一个字符?(用char来定义)

1.先创建一个scanner对象

2.调用scanner对象的next()方法获取控制台输入,返回的是一个string类型,因为没有nextchar()方法

3.调用string的charat(0)方法获取第一个字符

scanner

sc

=

new

scanner(system.in);

string

s

=

sc.next();

char

c

=

s.charat(0);

在java中如何输入一个字符形参

可以创建Scanner类来从键盘输入一个字符,用String类型来接收,再使用String的charAt功能,就可以输入字符了。