提示用户在一行上输入名称,然后将其打印为"Last,First" [英] Prompt user to enter name on one line and print it out as "Last, First"

查看:59
本文介绍了提示用户在一行上输入名称,然后将其打印为"Last,First"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们整个实验室中,没有人能够解决这个问题,因此我们必须做它来做家庭作业.我们将提示用户在一行上输入他们的名字和姓氏.主要方法应该处理所有println语句,并且应将名称打印为Last,First.

line 26 states "cannot find symbol."
line 36 states "not a statement. ';' expected.
line 39 states unreachable statement.

我已经使用了一个小时的代码,几乎已经放弃了.有帮助吗?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab11;

import java.util.Scanner;

/**
 *
 * @author xxxxxx
 */
public class Lab11 {
    public static void main(String[] args) {
        System.out.println("Enter your name: ");
        Scanner user_input=new Scanner(System.in);

        changeNameFormat();
        System.out.println(lastName+", " + firstName);

        System.out.println("Enter a word. ");
        palindrome();
    }

    public static String changeNameFormat() {
        String firstName, lastName;
        firstName, lastName = user_input.nextLine;
        return lastName;
        return firstName;
    }

解决方案

以下是您需要执行的步骤列表:

获取输入[将以示例:约翰·史密斯"]

将其分为两部分[有几种方法可以做到这一点]

firstName = input.substring(0, input.indexOf(" "));
lastName = input.substring(input.indexOf(" ")+1, input.length());

substring()返回字符串的一部分.

indexOf()在字符串中找到特定字符.

它需要一个开始值和一个结束值.

在这种情况下,我们从头开始,到找到空格时结束.

要获取姓氏,请在找到空格的地方开始,然后在字符串的末尾结束.

您还可以将其与 .trim()方法结合使用,该方法可从中删除空格字符串的开头/结尾.

最后,我们返回连接的值:

return lastName + ", " + firstName;

完整代码:

package lab11;
import java.util.Scanner;
public class Lab11 {
    public static Scanner user_input;
    public static void main(String[] args) {
        user_input = new Scanner(System.in);
        System.out.print("Enter your name: ");
        System.out.println(changeNameFormat(user_input.nextLine()));
    }
    public static String changeNameFormat(String input) { //Pass a name
        String firstName, lastName;
        firstName = input.substring(0, input.indexOf(" "));
        lastName = input.substring(input.indexOf(" ")+1, input.length());
        return lastName + ", " + firstName;
    }
}

经过以下测试: http://www.compileonline.com/compile_java_online.php

最后一件事.按照您的方法命名,这是一个很好的约定.

由于您的名字叫"changeNameFormat()",我们可能应该给它传递我们要更改的名称.

然后在我们的主目录中,我们可以获取输入,甚至可以将其作为参数传递.

No one in our entire lab was able to get this problem, so we have to do it for homework. We are to prompt the user to enter their first and last name on one line. The main method is supposed to handle all println statements and should print the name as Last, First.

line 26 states "cannot find symbol."
line 36 states "not a statement. ';' expected.
line 39 states unreachable statement.

I have been playing with this code for an hour and have just about given up. Any help?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab11;

import java.util.Scanner;

/**
 *
 * @author xxxxxx
 */
public class Lab11 {
    public static void main(String[] args) {
        System.out.println("Enter your name: ");
        Scanner user_input=new Scanner(System.in);

        changeNameFormat();
        System.out.println(lastName+", " + firstName);

        System.out.println("Enter a word. ");
        palindrome();
    }

    public static String changeNameFormat() {
        String firstName, lastName;
        firstName, lastName = user_input.nextLine;
        return lastName;
        return firstName;
    }

解决方案

Here is a list of steps you need to do:

Get the input [Will be Example: "John Smith"]

Split it into two pieces [There are several ways to do this]

firstName = input.substring(0, input.indexOf(" "));
lastName = input.substring(input.indexOf(" ")+1, input.length());

substring() returns a portion of the string.

indexOf() finds a specific character in the string.

It takes a start value and an end value.

In this case, we start at the beginning, and end when we find a space.

To get the last name, we start where we find a space, and end at the end of the string.

You can also combine this with the .trim() method that removes whitespace from the beginning/end of strings.

Finally, we return the concatenated value:

return lastName + ", " + firstName;

Complete code:

package lab11;
import java.util.Scanner;
public class Lab11 {
    public static Scanner user_input;
    public static void main(String[] args) {
        user_input = new Scanner(System.in);
        System.out.print("Enter your name: ");
        System.out.println(changeNameFormat(user_input.nextLine()));
    }
    public static String changeNameFormat(String input) { //Pass a name
        String firstName, lastName;
        firstName = input.substring(0, input.indexOf(" "));
        lastName = input.substring(input.indexOf(" ")+1, input.length());
        return lastName + ", " + firstName;
    }
}

Tested on: http://www.compileonline.com/compile_java_online.php

One final thing. It's a good convention to make your methods work as they are named.

Since yours is called "changeNameFormat()", we should probably pass it the name we want to change.

Then in our main, we can get the input, or even pass it as a parameter.

这篇关于提示用户在一行上输入名称,然后将其打印为"Last,First"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆