如何确保用户没有在名为“名字”的第一个文本字段中输入他/她的全名。

huangapple go评论65阅读模式
英文:

How can I make sure that the user did not enter his/her entire name in the First Text Field named as "First Name"

问题

这个问题是要求用户输入“名字(First Name)”和“姓氏(Last Name)”,然后显示带有用户全名的欢迎消息。还要确保用户不会在第一个文本框中输入完整的全名,该文本框只要求输入名字。

我认为如果用户在第一个文本框中输入了完整的全名,我们可以通过他/她是否输入了空格或(' ')来判断。如果没有输入空格,我们可以简单地显示欢迎消息 + 全名。然而,它并没有按照我想的方式运行…有人可以帮助我吗?

图片描述

英文:

This question says ask for the 'First Name' and the 'Last Name' from the user and then show the message Welcome with the full name of the user . also make sure that the user does not enter his/her full name in the first Text Field which asks for First Name only
I thought that if the user enters his/her full name in the first text field , we can know that from the fact that he/she entered a space or (' ') or not . If not we can simply show the message Welcome + full name . However it didn't work the way I thought it would ... Can somebody help me with itenter image description here

答案1

得分: 3

如果我理解您的意思以下内容将通过忽略空格后面的数据并要求用户提供他们的姓来实现您所需的功能

代码
public static void main(String[] args) {

        // 属性
        Scanner keyboard = new Scanner(System.in);
        String firstName, lastName;

        // 向用户询问他们的名字
        System.out.println("What is your first name? ");
        System.out.print("--> "); // 这只是为了样式,不是必需的
        firstName = keyboard.next();

        // 向用户询问他们的姓
        System.out.println("What is your last name? ");
        System.out.print("--> "); // 这只是为了样式,不是必需的
        lastName = keyboard.next();

        // 显示数据
        System.out.println("Your first name is: " + firstName);
        System.out.println("Your last name is: " + lastName);
}
英文:

If I understand you the below will work accomplish what you need by ignoring the data after the space and asking the user for their last name.

code:
public static void main(String[] args) {

    // Properties
    Scanner keyboard = new Scanner(System.in);
    String firstName, lastName

    // Ask the user for their first name
    System.out.println("What is your first name? ");
    System.out.print("--> "); // this is for style and not needed
    firstName = keyboard.next();

    // Ask the user for their last name
    System.out.println("What is your last name? ");
    System.out.print("--> "); // this is for style and not needed
    lastName = keyboard.next();

    // Display the data
    System.out.println("Your first name is : " + firstName);
    System.out.println("Your last name is : " + lastName);


}

答案2

得分: 2

以下是翻译好的内容:

实际上,有几种方法可以做到这一点,但如果我正确理解了你的问题,一个简单的方法如下,这是从http://math.hws.edu/javanotes/c2/ex6-ans.html获得的,并在我学习Java时帮助我更好地理解Java,你只需要根据你的需要进行修改。

代码:

public class FirstNameLastName {
    
    public static void main(String[] args) {
        
        String input;     // 用户输入的行。
        int space;        // 输入中空格的位置。
        String firstName; // 从输入中提取的名字。
        String lastName;  // 从输入中提取的姓氏。
        
        System.out.println();
        System.out.println("请输入您的名字和姓氏,用空格分隔。");
        System.out.print("? ");
        input = TextIO.getln();
        
        space = input.indexOf(' ');
        firstName = input.substring(0, space);
        lastName = input.substring(space+1);
        
        System.out.println("您的名字是 " + firstName + ",有 "
                                  + firstName.length() + " 个字符。");
        System.out.println("您的姓氏是 " + lastName + ",有 "
                                  + lastName.length() + " 个字符。");
        System.out.println("您的首字母分别为 " + firstName.charAt(0) + lastName.charAt(0));
        
    }

}

编辑:
如果这不太清楚,我可以用一个更详细的示例给出更好的解释。

有关类似问题的更多说明,请参阅以下链接。
https://www.homeandlearn.co.uk/java/substring.html

英文:

There is actually a few ways you can do this, but if I understand your question correctly a simple way would be below, which is from http://math.hws.edu/javanotes/c2/ex6-ans.html and helped me understand Java more when I was learning it, you just would alter it to your needs.

code:
public class FirstNameLastName {

public static void main(String[] args) {
    
    String input;     // The input line entered by the user.
    int space;        // The location of the space in the input.
    String firstName; // The first name, extracted from the input.
    String lastName;  // The last name, extracted from the input.
    
    System.out.println();
    System.out.println("Please enter your first name and last name, separated by a space.");
    System.out.print("? ");
    input = TextIO.getln();
    
    space = input.indexOf(' ');
    firstName = input.substring(0, space);
    lastName = input.substring(space+1);
    
    System.out.println("Your first name is " + firstName + ", which has "
                              + firstName.length() + " characters.");
    System.out.println("Your last name is " + lastName + ", which has "
                              + lastName.length() + " characters.");
    System.out.println("Your initials are " + firstName.charAt(0) + lastName.charAt(0));
    
}

}

edit:
If this doesn't make sense I can give a better explanation with a better example with more detail.

More notes on similar problems.
https://www.homeandlearn.co.uk/java/substring.html

答案3

得分: 0

你的代码问题在于,你检查了每个单字符,然后针对每个单字符进行 if/else 判断。这意味着如果最后一个字符不是空格,它最终会处理 else 部分。

解决方案是只检查一次:

if fn.contains(' ') {
    //如果两个名称都是在第一个字段中输入,执行你想要的操作
} else {
    //一切正常
}
英文:

The problem with your code is, that you check every single charackter and then do the if/else for every single charackter. which means if the last charackter is not a whitespace it will at the end process the else tree.

The solution is to just check once:

if(fn.contains(' '){
    //Do what you want to do, if both names were entered in the first field
}else{
    //Everything is fine
}

huangapple
  • 本文由 发表于 2020年9月18日 00:18:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63942207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定