Java 扫描仪输入数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18045614/
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
Scanner input data types
提问by Claude Rhay
I need to write a test class that will do the following:
我需要编写一个测试类来执行以下操作:
- a. Let the user input an integer and display it.
- b. Let the user input a float value and display it.
- c. Let the user input his/her name (no white spaces) and display the
name as:
“Hello <name>, welcome to Scanner!”
- d. Let the user input a character and display it.
- e. Let the user input any string (with white spaces) and display it.
- 一种。让用户输入一个整数并显示它。
- 湾 让用户输入一个浮点值并显示它。
- C。让用户输入他/她的名字(没有空格)并将名字显示为:
“Hello <name>, welcome to Scanner!”
- d. 让用户输入一个字符并显示它。
- e. 让用户输入任何字符串(带空格)并显示它。
My questions is, how can I simply scan just a Character
and display it? And in number 2, How can I input a String
with white spaces and display it? (letters "d" and "e")
我的问题是,我怎样才能简单地扫描Character
并显示它?在数字 2 中,如何输入String
带有空格的 a 并显示它?(字母“d”和“e”)
I've searched around, but I cannot find the simplest solution (since I'm new to Java and programming).
我四处搜索,但找不到最简单的解决方案(因为我是 Java 和编程的新手)。
Here is my code so far:
到目前为止,这是我的代码:
package aw; import java.io.PrintStream; import java.util.Scanner; public class NewClass1 { public static void main(String[] args) { int num; double num2; String name; char c; Scanner sc = new Scanner(System.in); PrintStream ps = new PrintStream(System.out); //for integer System.out.println("Enter a number: "); num = sc.nextInt(); ps.printf("%d\n", num); //for float System.out.println("Enter a float value: "); num2 = sc.nextDouble(); ps.printf("%.2f\n", num2); //for name w/o white space System.out.print("Enter your first name: "); name = sc.next(); ps.printf("Hello %s, welcome to Scanner\n", name); //for character System.out.print("Enter a character: "); c = sc.findWithinHorizon(".", 0).charAt(0); System.out.print(“%c”, c); //for name w/ white space System.out.print("Enter your full name: "); name = sc.nextLine(); System.out.print(“%s”, name); } }
package aw; import java.io.PrintStream; import java.util.Scanner; public class NewClass1 { public static void main(String[] args) { int num; double num2; String name; char c; Scanner sc = new Scanner(System.in); PrintStream ps = new PrintStream(System.out); //for integer System.out.println("Enter a number: "); num = sc.nextInt(); ps.printf("%d\n", num); //for float System.out.println("Enter a float value: "); num2 = sc.nextDouble(); ps.printf("%.2f\n", num2); //for name w/o white space System.out.print("Enter your first name: "); name = sc.next(); ps.printf("Hello %s, welcome to Scanner\n", name); //for character System.out.print("Enter a character: "); c = sc.findWithinHorizon(".", 0).charAt(0); System.out.print(“%c”, c); //for name w/ white space System.out.print("Enter your full name: "); name = sc.nextLine(); System.out.print(“%s”, name); } }
I hope you can help me. Thanks!
我希望你能帮助我。谢谢!
采纳答案by Ravi Thapliyal
First, there's no need to wrap System.out
in a PrintStream
because out
already supports formatting with format()
or printf()
methods.
首先,不需要包含System.out
在 a 中,PrintStream
因为out
已经支持使用format()
或printf()
方法进行格式化。
Next, you need to understand that when you input a line of data you also terminate it with a new line\n
. The next<Type>()
methods only consume the <Type>
and nothing else. So, if a next<Type>()
call may match \n
, you need to skip over any extra new lines\n
with another nextLine()
before.
接下来,您需要了解,当您输入一行数据时,您也会以新行终止它\n
。这些next<Type>()
方法只消耗<Type>
,没有别的。所以,如果一个next<Type>()
电话可以匹配\n
,你需要跳过任何额外的新线\n
与另一nextLine()
前。
Here's your code with fixes:
这是您的修复代码:
int num;
double num2;
String name;
char c;
Scanner sc = new Scanner(System.in);
//for integer
System.out.print("Enter a number: ");
num = sc.nextInt();
System.out.printf("%d\n", num);
//for float
System.out.print("Enter a float value: ");
num2 = sc.nextDouble();
System.out.printf("%.2f\n", num2);
//for name w/o white space
System.out.print("Enter your first name: ");
name = sc.next();
System.out.printf("Hello %s, welcome to Scanner\n", name);
//for character
System.out.print("Enter a character: ");
c = sc.findWithinHorizon(".", 0).charAt(0);
System.out.printf("%c\n", c);
sc.nextLine(); // skip
//for name w/ white space
System.out.print("Enter your full name: ");
name = sc.nextLine();
System.out.printf("%s", name);
回答by slanecek
Use this:
用这个:
//for a single char
char Character = sc.findWithinHorizon(".", 0).charAt(0);
//for a name with white space
System.out.print("Enter your full name: ");
String name2 = sc.next();
String surname = sc.next();
System.out.println(name2 + " " + surname);
回答by Luca Basso Ricci
Use Scanner.next(Pattern)
and pass Pattern.compile("[A-Za-z0-9]")
to let scanner accept only 1 character defined.
You can pass any regex as argument and check for next()Scanner.next();
for next line with spaces
使用Scanner.next(Pattern)
传递Pattern.compile("[A-Za-z0-9]")
让扫描器只接受定义的 1 个字符。
您可以将任何正则表达式作为参数传递并检查next()Scanner.next();
是否有空格