[英]Java help needed for printing user entered string variable
I'm trying to write a program that accepts user input for their full name and their gender (m/f). 我正在尝试编写一个程序,接受用户输入的全名和性别(m / f)。
import java.util.Scanner;
public class CSCD210Lab2 { // class declaration
public static void main(String[]args) { // main method
Scanner userInput = new Scanner(System.in); // scanner object for console input
System.out.print("Enter Your Full Name: ");
String fullName;
fullName = userInput.next( );
System.out.print = fullName; // print user input
}
}
At the moment, I only have the code for the user entering their name. 目前,我只有用户输入其姓名的代码。 I wanted to make sure the fullName variable was being stored, so I tried to have the console print whatever the user typed.
我想确保存储了fullName变量,因此我尝试让控制台打印用户键入的内容。 However, I'm getting a syntax error on the
System.out.print = fullName;
但是,我在
System.out.print = fullName;
上收到语法错误System.out.print = fullName;
. 。 It gives me this error message:
它给了我这个错误信息:
CSCD210Lab2.java:12: error: cannot find symbol
System.out.print = fullName;
^
symbol: variable print
location: variable out of type PrintStream
1 error
What am I doing wrong here? 我在这里做错了什么?
Bad syntax. 错误的语法。 System.out.print is a function, and as such is not eligible to be a left-hand-side of an assignment operation.
System.out.print是一个函数,因此不适合作为赋值操作的左侧。 Try this
尝试这个
System.out.print(fullName);
//or
System.out.println(fullName);
print
是一种方法,而不是变量:
System.out.print(fullName);
Instead of using .next()
go ahead and try putting this in instead: userInput.nextLine();
不要使用
.next()
,而是尝试将其放入: userInput.nextLine();
Meaning just add the .nextLine()
method call instead of the next()
method. 意思是只添加
.nextLine()
方法调用而不是next()
方法。 Also System.out.println("text goes here ");
还有
System.out.println("text goes here ");
Is how this function is used. 是如何使用此功能的。 Hope this helps, cheers.
希望这会有所帮助,欢呼。
fullName = userInput.nextLine(); //next()will stop with first space ,so it is not compatible with your code , but it is right
System.out.print(fullName);//print() is method you need to pass to it argument not to assign to it values like variables
I ran your code and tested and found that when you use, 我运行了您的代码并进行了测试,发现当您使用时,
System.out.print
you need to include the parenthesis () after print. 您需要在打印后加上括号()。 Regardless if it's just a variable being printed like
fullName
不管它只是像
fullName
一样打印的变量
So here would be your fix 所以这是你的解决办法
System.out.print(fullName);
Also, I notice that when you enter the full name, it only prints the first name. 另外,我注意到当您输入全名时,它只会打印名字。 In order to fix this, use the method
nextLine();
为了解决这个问题,请使用
nextLine();
方法nextLine();
instead of next();
而不是
next();
This will allow the full name to be printed. 这将允许打印全名。
import java.util.Scanner;
public class CSCD210Lab2 { // class declaration
public static void main(String[]args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter Your Full Name: ");
String fullName;
fullName = userInput.nextLine( );
System.out.print(fullName); // print user input
}
}
Complete code is here. 完整的代码在这里。
nextline()
will allow you to take spaces between entrties. nextline()
将允许您在实体之间使用空格。 Each line will be considered as single entry. 每行将被视为单个条目。
Cheers!! 干杯!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.