[英]Reverse name task
Here is what my code is trying to do: Write a method called processName that accepts a Scanner for the console as a parameter and prompts the user to enter a full name, then prints the name in reverse order (ie, last name, first name). 这是我的代码尝试执行的操作:编写一个名为processName的方法,该方法接受控制台的Scanner作为参数,并提示用户输入全名,然后以相反的顺序打印该名称(即姓氏,名字) )。
Here is an example dialogue with the user: 这是与用户对话的示例:
Please enter your full name: Sammy Jankis
请输入您的全名:Sammy Jankis
Your name in reverse order is Jankis, Sammy
您的反向名称是Jankis,Sammy
import java.util.*;
public class Project3 {
public static void main(String[] args) {
System.out.print("Please enter your full name: ");
String firstname = console.next();
String lastname = console.next();
processName(firstname, lastname);
}
public static void processName(String y, String z) {
System.out.print("Your name in reverse order is"
+ z + ", " + y);
}
}
I get an error reading: 我收到错误消息:
Project3.java:16: cannot find symbol
symbol : variable console
location: class Project3
String firstname = console.next();
^
Project3.java:17: cannot find symbol
symbol : variable console
location: class Project3
String lastname = console.next();
^
My question is, how can I divide the name string into two parts so that I can reverse it? 我的问题是,如何将名称字符串分为两部分,以便可以将其反转? Please use simple terms because I am new to coding.
请使用简单的术语,因为我是编码的新手。
You forgot to declare (and initialize) console
. 您忘记了声明(和初始化)
console
。 I think you wanted 我想你想要
Scanner console = new Scanner(System.in);
before String firstname = console.next();
在
String firstname = console.next();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.