You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java方法重载中如何获取首个方法输入结果并实现共同问候

问题分析与解决方案

你的核心问题是第一个userenter()方法为void类型,执行完成后未保存主用户的姓名和称谓信息,导致后续无法调用这些数据实现共同问候。以下是具体修复方案:

1. 封装用户信息

定义一个User内部类,用来存储用户的姓名和对应称谓(Mr./Ms.):

static class User {
    String fullname;
    String title;

    public User(String fullname, String title) {
        this.fullname = fullname;
        this.title = title;
    }
}

2. 修改无参userenter()方法,返回用户对象

把原来直接打印问候的逻辑改成返回User对象,保存主用户的信息:

public static User userenter() {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your Fullname: ");
    String fullname = input.nextLine();

    System.out.println("");

    System.out.println("Are you Male or Female?");
    System.out.println("M for Male F for female: ");
    String sex = input.nextLine();
    String title = "";

    if(sex.equalsIgnoreCase("M")){
        title = "Mr.";
    }else if(sex.equalsIgnoreCase("F")){
        title = "Ms.";
    }
    return new User(fullname, title);
}

3. 调整重载方法,支持共同问候

修改接收布尔参数的重载方法,改为接收主用户的User对象,当用户有同行伙伴时,获取伙伴信息后一起输出共同问候:

public static void userenter(User mainUser) {
    Scanner input = new Scanner(System.in);
    System.out.println("Are you with a company? Yes/No");
    String ans = input.nextLine();

    if (ans.equalsIgnoreCase("yes")){
        User mate = userenter("withmate");
        // 共同问候逻辑
        System.out.println("Hello " + mainUser.title + " " + mainUser.fullname + " and " + mate.title + " " + mate.fullname + ", welcome to the party!");
    }else {
        System.out.println("Hello " + mainUser.title + " " + mainUser.fullname + ", welcome to the party!");
        System.out.println("Have a Good Day ahead!");
    }
}

4. 修改处理伙伴信息的重载方法

同样让该方法返回User对象,而非直接打印:

public static User userenter(String withmate) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your mates Fullname: ");
    String fullname = input.nextLine();

    System.out.println("");

    System.out.println("Are you Male or Female?");
    System.out.println("M for Male F for female: ");
    String sex = input.nextLine();
    String title = "";

    if(sex.equalsIgnoreCase("M")){
        title = "Mr.";
    }else if(sex.equalsIgnoreCase("F")){
        title = "Ms.";
    }
    return new User(fullname, title);
}

5. 调整主方法调用逻辑

先获取主用户信息,再传入重载方法处理:

public static void main(String[] args) {
    User mainUser = userenter();
    userenter(mainUser);
}

完整修改后的代码

import java.util.*;
public class OverloadingFG2 {

    static class User {
        String fullname;
        String title;

        public User(String fullname, String title) {
            this.fullname = fullname;
            this.title = title;
        }
    }

    public static void main(String[] args) {
        User mainUser = userenter();
        userenter(mainUser);
    }

    public static User userenter() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your Fullname: ");
        String fullname = input.nextLine();

        System.out.println("");

        System.out.println("Are you Male or Female?");
        System.out.println("M for Male F for female: ");
        String sex = input.nextLine();
        String title = "";

        if(sex.equalsIgnoreCase("M")){
            title = "Mr.";
        }else if(sex.equalsIgnoreCase("F")){
            title = "Ms.";
        }
        return new User(fullname, title);
    }

    public static void userenter(User mainUser) {
        Scanner input = new Scanner(System.in);
        System.out.println("Are you with a company? Yes/No");
        String ans = input.nextLine();

        if (ans.equalsIgnoreCase("yes")){
            User mate = userenter("withmate");
            System.out.println("Hello " + mainUser.title + " " + mainUser.fullname + " and " + mate.title + " " + mate.fullname + ", welcome to the party!");
        }else {
            System.out.println("Hello " + mainUser.title + " " + mainUser.fullname + ", welcome to the party!");
            System.out.println("Have a Good Day ahead!");
        }
    }

    public static User userenter(String withmate) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your mates Fullname: ");
        String fullname = input.nextLine();

        System.out.println("");

        System.out.println("Are you Male or Female?");
        System.out.println("M for Male F for female: ");
        String sex = input.nextLine();
        String title = "";

        if(sex.equalsIgnoreCase("M")){
            title = "Mr.";
        }else if(sex.equalsIgnoreCase("F")){
            title = "Ms.";
        }
        return new User(fullname, title);
    }
}

额外优化建议

  • 避免重复创建Scanner:可将Scanner设为类的静态变量,整个程序仅使用一个实例,减少资源消耗。
  • 增加输入校验:比如判断姓名是否为空、性别输入是否合法,提升程序健壮性。

内容的提问来源于stack exchange,提问作者user18451561

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 17:48:11