如何在java中输入字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22837423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to take character input in java
提问by Shahiduzzaman Shahid
In C, we are able to take input as character with the keyword char
from keyboard as
在 C 中,我们可以将输入作为字符与char
键盘上的关键字作为
scanf("%c", &ch);
scanf("%c", &ch);
But In Java how to do this?
但是在Java中如何做到这一点?
I have tried this:
我试过这个:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char c = scanner.next().charAt(0);
System.out.println("You have entered: "+c);
}
}
采纳答案by Paul Sanwald
use the System class
使用系统类
char yourChar = System.in.read()
回答by unfamous
you can use a Scanner to read from input :
您可以使用扫描仪从输入中读取:
Scanner scanner = new Scanner(System.in);
char c = scanner.next().charAt(0); //charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.
回答by svjn
Here is the sample program.
这是示例程序。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadFromConsole {
public static void main(String[] args) {
System.out.println("Enter here : ");
try{
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String value = bufferRead.readLine();
System.out.println(value);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
You can get it easily when you search in Internet. StackExchange recommends to do some research and put some effort before reaching it.
当您在互联网上搜索时,您可以轻松获得它。StackExchange 建议做一些研究,并在达到它之前付出一些努力。
回答by Tiago Vieira Dos Santos
using java you can do this:
使用 java 你可以这样做:
Using the Scanner:
使用扫描仪:
Scanner reader = new Scanner(System.in);
String line = reader.nextLine();
// now you can use some converter to change the String value to the value you need.
// for example Long.parseLong(line) or Integer.parseInt(line) or other type cast
Using the BufferedReader:
使用 BufferedReader:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
// now you can use some converter to change the String value to the value you need.
// for example Long.parseLong(line) or Integer.parseInt(line) or other type cast
In the two cases you need to pass you Default input, in my case System.in
在这两种情况下,您需要传递默认输入,在我的情况下为 System.in
回答by M.Faizal
You can simply use (char) System.in.read();
casting to char
is necessary to convert int
to char
您可以简单地使用 (char) System.in.read();
强制 char
转换int
来转换 为char
回答by harsh
use :
用 :
char ch=**scanner.nextChar**()
回答by Blagovest Anev
I had the same struggle and I this is what I used:
我也有同样的挣扎,这就是我使用的:
} public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter the string: ");
String input = scan.next();
System.out.print("Please enter the required symbol: ");
String symbol = scan.next();
char symbolChar = symbol.charAt(0);
This works just fine. The idea is to get from the string the only char in it.
这工作得很好。这个想法是从字符串中获取其中唯一的字符。
回答by abhishek agrawal
import java.util.Scanner;
class SwiCas {
public static void main(String as[]) {
Scanner s= new Scanner(System.in);
char a=s.next().charAt(0);//this line shows how to take character input in java
switch(a) {
case 'a':
System.out.println("Vowel....");
break;
case 'e':
System.out.println("Vowel....");
break;
case 'i':
System.out.println("Vowel....");
break;
case 'o':
System.out.println("Vowel....");
break;
case 'u':
System.out.println("Vowel....");
break;
case 'A':
System.out.println("Vowel....");
break;
case 'E':
System.out.println("Vowel....");
break;
case 'I':
System.out.println("Vowel....");
break;
case 'O':
System.out.println("Vowel....");
break;
case 'U':
System.out.println("Vowel....");
break;
default:
System.out.println("Consonants....");
}
}
}
回答by Nadir Hussain
import java.util.Scanner;
class CheckVowel {
public static void main(String args[]) {
Scanner obj= new Scanner(System.in);
char a=obj.next().charAt(0);
switch(a) {
case 'a': //cases can be used together for the same statement
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
{
System.out.println("Vowel....");
break;
}
default:
System.out.println("Consonants....");
}
}
}