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

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

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

  1. 如果我理解您的意思以下内容将通过忽略空格后面的数据并要求用户提供他们的姓来实现您所需的功能
  2. 代码
  3. public static void main(String[] args) {
  4. // 属性
  5. Scanner keyboard = new Scanner(System.in);
  6. String firstName, lastName;
  7. // 向用户询问他们的名字
  8. System.out.println("What is your first name? ");
  9. System.out.print("--> "); // 这只是为了样式,不是必需的
  10. firstName = keyboard.next();
  11. // 向用户询问他们的姓
  12. System.out.println("What is your last name? ");
  13. System.out.print("--> "); // 这只是为了样式,不是必需的
  14. lastName = keyboard.next();
  15. // 显示数据
  16. System.out.println("Your first name is: " + firstName);
  17. System.out.println("Your last name is: " + lastName);
  18. }
英文:

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) {

  1. // Properties
  2. Scanner keyboard = new Scanner(System.in);
  3. String firstName, lastName
  4. // Ask the user for their first name
  5. System.out.println("What is your first name? ");
  6. System.out.print("--> "); // this is for style and not needed
  7. firstName = keyboard.next();
  8. // Ask the user for their last name
  9. System.out.println("What is your last name? ");
  10. System.out.print("--> "); // this is for style and not needed
  11. lastName = keyboard.next();
  12. // Display the data
  13. System.out.println("Your first name is : " + firstName);
  14. System.out.println("Your last name is : " + lastName);
  15. }

答案2

得分: 2

以下是翻译好的内容:

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

代码:

  1. public class FirstNameLastName {
  2. public static void main(String[] args) {
  3. String input; // 用户输入的行。
  4. int space; // 输入中空格的位置。
  5. String firstName; // 从输入中提取的名字。
  6. String lastName; // 从输入中提取的姓氏。
  7. System.out.println();
  8. System.out.println("请输入您的名字和姓氏,用空格分隔。");
  9. System.out.print("? ");
  10. input = TextIO.getln();
  11. space = input.indexOf(' ');
  12. firstName = input.substring(0, space);
  13. lastName = input.substring(space+1);
  14. System.out.println("您的名字是 " + firstName + ",有 "
  15. + firstName.length() + " 个字符。");
  16. System.out.println("您的姓氏是 " + lastName + ",有 "
  17. + lastName.length() + " 个字符。");
  18. System.out.println("您的首字母分别为 " + firstName.charAt(0) + lastName.charAt(0));
  19. }
  20. }

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

有关类似问题的更多说明,请参阅以下链接。
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 {

  1. public static void main(String[] args) {
  2. String input; // The input line entered by the user.
  3. int space; // The location of the space in the input.
  4. String firstName; // The first name, extracted from the input.
  5. String lastName; // The last name, extracted from the input.
  6. System.out.println();
  7. System.out.println("Please enter your first name and last name, separated by a space.");
  8. System.out.print("? ");
  9. input = TextIO.getln();
  10. space = input.indexOf(' ');
  11. firstName = input.substring(0, space);
  12. lastName = input.substring(space+1);
  13. System.out.println("Your first name is " + firstName + ", which has "
  14. + firstName.length() + " characters.");
  15. System.out.println("Your last name is " + lastName + ", which has "
  16. + lastName.length() + " characters.");
  17. System.out.println("Your initials are " + firstName.charAt(0) + lastName.charAt(0));
  18. }

}

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 部分。

解决方案是只检查一次:

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

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:

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

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:

确定