我正在尝试找到一种从键盘输入 char
的方法。
我尝试使用:
Scanner reader = new Scanner(System.in);
char c = reader.nextChar();
这个方法不存在。
我尝试将c
作为String
。然而,在每种情况下它并不总是起作用,因为我从我的方法中调用的另一个方法需要一个char
作为输入。因此,我必须找到一种明确地以字符形式作为输入的方法。
有什么帮助吗?
我正在尝试找到一种从键盘输入 char
的方法。
我尝试使用:
Scanner reader = new Scanner(System.in);
char c = reader.nextChar();
这个方法不存在。
我尝试将c
作为String
。然而,在每种情况下它并不总是起作用,因为我从我的方法中调用的另一个方法需要一个char
作为输入。因此,我必须找到一种明确地以字符形式作为输入的方法。
有什么帮助吗?
import java.util.Scanner;
public class userInput{
public static void main(String[] args){
// Creating your scanner with name kb as for keyBoard
Scanner kb = new Scanner(System.in);
String name;
int age;
char bloodGroup;
float height;
// Accepting Inputs from user
System.out.println("Enter Your Name");
name = kb.nextLine(); // for entire line of String including spaces
System.out.println("Enter Your Age");
age = kb.nextInt(); // for taking Int
System.out.println("Enter Your BloodGroup : A/B/O only");
bloodGroup = kb.next().charAt(0); // For character at position 0
System.out.println("Enter Your Height in Meters");
height = kb.nextFloat(); // for taking Float value
// closing your scanner object
kb.close();
// Outputting All
System.out.println("Name : " +name);
System.out.println("Age : " +age);
System.out.println("BloodGroup : " +bloodGroup);
System.out.println("Height : " +height+" m");
}
}
// Use a BufferedReader to read characters from the console.
import java.io.*;
class BRRead {
public static void main(String args[]) throws IOException
{
char c;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
// read characters
do {
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}
}
import java.util.Scanner;
public class InputC{
public static void main(String[] args) {
// TODO Auto-generated method stub
// Declare the object and initialize with
// predefined standard input object
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
// Character input
char c = scanner.next().charAt(0);
// Print the read value
System.out.println("You have entered: "+c);
}
}
输出
Enter a character:
a
You have entered: a
请尝试以下方法。
Scanner reader = new Scanner(System.in);
char c = reader.next().charAt(0);
这将从键盘获取一个字符。
你只需要编写这个代码以获取 char 类型的值。
char c = reader.next().charAt(0);
import java.io.*;
class abc // enter class name (here abc is class name)
{
public static void main(String arg[])
throws IOException // can also use Exception
{
BufferedReader z =
new BufferedReader(new InputStreamReader(System.in));
char ch = (char) z.read();
} // PSVM
} // class
Scanner key = new Scanner(System.in);
//shortcut way
char firstChar=key.next().charAt(0);
//how it works;
/*key.next() takes a String as input then,
charAt method is applied on that input (String)
with a parameter of type int (position) that you give to get
that char at that position.
You can simply read it out as:
the char at position/index 0 from the input String
(through the Scanner object key) is stored in var. firstChar (type char) */
//you can also do it in a bit elabortive manner to understand how it exactly works
String input=key.next(); // you can also write key.nextLine to take a String with spaces also
char firstChar=input.charAt(0);
char charAtAnyPos= input.charAt(pos); // in pos you enter that index from where you want to get the char from
从用户输入中读取字符的简单解决方案。 先读取一个字符串,然后使用String的charAt(0)方法。
Scanner reader = new Scanner(System.in);
String str = reader.next();
char c = str.charAt(0);
就是这样。
试试这个
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
char c=s.charAt(0);
InputStreamReader
和BufferedReader
有什么区别?你在代码中使用了它们,但没有解释。 - VimNing