【问题标题】:How can I make sure that the user did not enter his/her entire name in the First Text Field named as "First Name"如何确保用户没有在名为“名字”的第一个文本字段中输入他/她的全名
【发布时间】:2020-09-18 02:53:31
【问题描述】:

这个问题说向用户询问“名字”和“姓氏”,然后显示带有用户全名的欢迎消息。还要确保用户没有在第一个只要求名字的文本字段中输入他/她的全名 我认为如果用户在第一个文本字段中输入他/她的全名,我们可以从他/她输入空格或('')的事实中知道这一点。如果没有,我们可以简单地显示消息 Welcome + full name 。但是它并没有像我想象的那样工作......有人可以帮助我吗enter image description here

【问题讨论】:

  • 请在询问和展示您的作品之前展示您尝试了多少。
  • 我确实嵌入了一张图片,请先检查一下,然后指出不存在的缺陷
  • 不过,您应该在此处发布代码,以便其他人轻松重现问题,而不是从您的图像中输入所有内容。
  • 这个问题解决起来并不简单。对空格的粗略检查将如何处理像“Mary Ann”这样的给定名称?用户非常对假定他们没有有效名称的软件感到恼火。

标签: java netbeans


【解决方案1】:

如果我理解您,以下内容将通过忽略空格后的数据并询问用户的姓氏来完成您需要的工作。

代码: 公共静态 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】:

实际上有几种方法可以做到这一点,但如果我正确理解了你的问题,下面是一个简单的方法,它来自http://math.hws.edu/javanotes/c2/ex6-ans.html,并在我学习 Java 时帮助我更多地了解 Java,你只会改变满足您的需求。

代码: 公共类名姓{

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));
    
}

}

编辑: 如果这没有意义,我可以通过更详细的更好示例给出更好的解释。

关于类似问题的更多说明。 https://www.homeandlearn.co.uk/java/substring.html

【讨论】:

  • 我理解你刚刚发布的代码并浏览了链接,但我想问的是假设有两个文本字段,你应该在第一个中输入你的名字文本字段和您在第二个文本字段中的姓氏.. 但是您最终只在第一个文本字段中输入了您的名字和姓氏.. 我怎么知道它发生了?检查我嵌入的图像
  • 啊,如果它说“输入你的名字?”那么重要。并且用户输入 Joe blow,您希望名字变量只分配“Joe”,然后程序会询问“您的姓氏是什么?”。所以基本上你的名字是什么?乔布洛 你姓什么? Blow firstName = Joe lastName = Blow 这就是你想要的吗?如果是这样,我可以编写代码。
  • 是的!!!!这正是我问的......谢谢卢卡斯
  • 太棒了!你能接受我的回答吗?它对寻找类似问题的人有很大帮助。
【解决方案3】:

您的代码的问题是,您检查每个字符,然后对每个字符执行 if/else。这意味着如果最后一个字符不是空格,它将在最后处理 else 树。

解决方案是检查一次:

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

【讨论】:

  • 是的,我明白我的错误,非常感谢 Max
猜你喜欢
  • 2013-10-29
  • 2022-01-05
  • 1970-01-01
  • 2020-12-24
  • 2010-11-30
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多