在Java中,如何在不重复整个方法的情况下连续调用字符串方法两次?

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

How do I call a string method twice in Java without repeating the whole method?

问题

  1. import java.util.Scanner;
  2. public class firstLastName2 {
  3. static String F_NAME;
  4. static String L_NAME;
  5. static String name;
  6. static void firstName(String name) {
  7. Scanner keyboard = new Scanner(System.in);
  8. System.out.println("Please enter your first name.");
  9. F_NAME = keyboard.nextLine();
  10. System.out.println("Please enter your last name.");
  11. L_NAME = keyboard.nextLine();
  12. }
  13. public static void main(String[] args) {
  14. firstName(name);
  15. System.out.println("Hello, " + F_NAME + " " + L_NAME + "!");
  16. }
  17. }
英文:

What I am trying to do is prompt a user to enter their first and last name by calling a function twice. First call would be for the first name and the second call would be for the last name. The program would then concatenate and display "Hello, firstname lastname!" I feel like I am very close to the correct outcome but I am obviously missing something. New guy here. Thank you for any and all responses.

  1. import java.util.Scanner;
  2. public class firstLastName2 {
  3. static String F_NAME;
  4. static String L_NAME;
  5. static String name;
  6. static void firstName(String name) {
  7. Scanner keyboard = new Scanner(System.in);
  8. System.out.println("Please enter your first name.");
  9. F_NAME = keyboard.nextLine();
  10. System.out.println("Please enter your last name.");
  11. L_NAME = keyboard.nextLine();
  12. }
  13. public static void main(String[] args) {
  14. firstName(name);
  15. System.out.println("Hello, " + F_NAME + " " + L_NAME + "!");
  16. }
  17. }

答案1

得分: 2

  1. 如果您想要调用一个函数两次那么我建议该函数返回用户输入并接收问题文本您的代码似乎是正确的
  2. import java.util.Scanner;
  3. public class firstLastName2 {
  4. static Scanner keyboard = new Scanner(System.in);
  5. static String getUserInput(String question) {
  6. System.out.println(question);
  7. return keyboard.nextLine();
  8. }
  9. public static void main(String[] args) {
  10. String name = getUserInput("请输入您的名字:");
  11. String surName = getUserInput("请输入您的姓氏:");
  12. System.out.println(String.format("你好,%s %s!", name , surName));
  13. }
  14. }
英文:

If you want to call a function twice, then i suggest that the function return the user input and receive the question text. Your code seem to be right.

  1. import java.util.Scanner;
  2. public class firstLastName2 {
  3. static Scanner keyboard = new Scanner(System.in);
  4. static String getUserInput(String question) {
  5. System.out.println(question);
  6. return keyboard.nextLine();
  7. }
  8. public static void main(String[] args) {
  9. String name = getUserInput("Please enter your first name.");
  10. String sunrName = getUserInput("Please enter your last name.");
  11. System.out.println(String.format("Hello, %s %s!", name , surName));
  12. }
  13. }

huangapple
  • 本文由 发表于 2020年10月16日 23:04:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64391727.html
匿名

发表评论

匿名网友

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

确定