我在这个项目中使用 eclipse。我尝试在命令提示符下编译,但发生了同样的问题。扫描仪一直工作,直到我到达“电话”,然后它似乎只是跳过用户输入并在同一行上打印其他所有内容。
我希望将用户输入存储到分配的变量中,然后将它们打印在单独的行上。
我添加了 sc.close;
来看看这是否有帮助,但没有。一些帮助将不胜感激。另外,我的变量“地址”没有完全打印出来。
我想我可能没有正确使用扫描仪?
import java.util.Scanner;
公共(public)类ContactDisplay{
public static void main(String[] args) {
//Write a program that displays your name, address, and telephone number;
//create scanner
Scanner sc = new Scanner(System.in);
//Creates the variables;
String name;
String address;
String phone;
//Asks for name
System.out.print("What is your name? ");
//stores the name
name = sc.next();
//Asks and stores the address
System.out.print("What is your address? ");
address = sc.next();
//Asks and stores the phone number
//PROBLEM IS BELOW
System.out.print("What is your phone number? ");
phone = sc.next();
//Prints everything out
System.out.println(name);
System.out.println(address);
System.out.println(phone);
}
}
这是一个屏幕截图:
最佳答案
您应该使用“sc.nextLine()”来扫描字符串值,并且应该使用“sc.nextInt”来扫描整数值。如果您在输入一堆代码时按 ctrl 和空格键,它会显示您可能想要编写的内容。
关于java - 扫描仪停止工作并且 Println 出现问题,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29334245/