简体   繁体   English

如何从第一种方法获得输入的结果?

[英]How can I get the result of the input from the first method?

import java.util.*;
public class OverloadingFG2 {

    public static void main(String[] args) {
       
       userenter();
       userenter(false);
    }
    public static void 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 male = "M";
        String female = "F";
        
        if(sex.equalsIgnoreCase(male)){
            System.out.println("Hello Mr. " + fullname + " Welcome to the party!");
        }else if(sex.equalsIgnoreCase(female)){
            System.out.println("Hello Ms. " + fullname + " Welcome to the party!");
        }
        System.out.println("");
    }
    public static boolean userenter(boolean yes){
        Scanner input = new Scanner(System.in);
        System.out.println("Are you with a company? Yes/No");
        String ans = input.nextLine();
        String y = "yes";

        if (ans.equalsIgnoreCase(y)){
            userenter("withmate");
        }else
            System.out.println("Have a Good Day ahead!");
        return yes;
    }
    public static void 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 male = "M";
        String female = "F";
        
        if(sex.equalsIgnoreCase(male)){
            System.out.println("Hello Mr. " + fullname + " Welcome to the party!");
        }else if(sex.equalsIgnoreCase(female)){
            System.out.println("Hello Ms. " + fullname + " Welcome to the party!");
        }
        System.out.println("");
    }

    
    
}

** I have trouble getting the result from the first user. ** 我无法从第一个用户那里获得结果。 The plan is to greet both of them but I can't seem to get the value of the other method I'm using.计划是向他们俩打招呼,但我似乎无法获得我正在使用的另一种方法的价值。 The goal is to use overloading and to greet the both of them now I just have a problem on greeting the both of the user.目标是使用重载并问候他们两个现在我只是在问候两个用户时遇到问题。 I mean greet them together **我的意思是一起问候他们**

I am not sure if I am understanding the question correctly.我不确定我是否正确理解了这个问题。 Do you want to greet both of the users towards the end?您想在最后向两个用户打招呼吗?

Then you'll need to set up a global variable.然后你需要设置一个全局变量。

import java.util.*;
public class OverloadingFG2 {

public static String fullName1;

public static void main(String[] args) {

    userenter();
    userenter(false);
}
public static void userenter(){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your Fullname: ");
    fullName1 = 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 male = "M";
    String female = "F";

    if(sex.equalsIgnoreCase(male)){
        System.out.println("Hello Mr. " + fullName1 + " Welcome to the party!");
    }else if(sex.equalsIgnoreCase(female)){
        System.out.println("Hello Ms. " + fullName1 + " Welcome to the party!");
    }
    System.out.println("");
}
public static boolean userenter(boolean yes){
    Scanner input = new Scanner(System.in);
    System.out.println("Are you with a company? Yes/No");
    String ans = input.nextLine();
    String y = "yes";

    if (ans.equalsIgnoreCase(y)){
        userenter("withmate");
    }else
        System.out.println("Have a Good Day ahead!");
    return yes;
}
public static void 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 male = "M";
    String female = "F";

    if(sex.equalsIgnoreCase(male)){
        System.out.println("Hello Mr. " + fullname + " and " + fullName1 + " Welcome to the party!");
    }else if(sex.equalsIgnoreCase(female)){
        System.out.println("Hello Ms. " + fullname + " and " + fullName1 + " Welcome to the party!");
    }
    System.out.println("");
}

} }

In response to your question:针对您的问题:

In that case yes, you'll have to create a global variable for their gender.在这种情况下,是的,您必须为他们的性别创建一个全局变量。 I've simplified the code for you for demonstration:我已经为您简化了代码以进行演示:

import java.util.*;
public class OverloadingFG2 {

public static String fullName1;
public static String gender1;

public static void main(String[] args) {

    userenter();
    userenter(false);
}

public static void userenter() {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your Fullname: ");
    fullName1 = 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();
    gender1 = (sex.equalsIgnoreCase("M") ? "Mr. " : "Ms. ");

    System.out.println("Hello " + gender1 + fullName1 + " Welcome to the party!");
    System.out.println("");
}

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

    if (ans.equalsIgnoreCase(y)) {
        userenter("withmate");
    } else
        System.out.println("Have a Good Day ahead!");
    return yes;
}

public static void 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 gender2 = (sex.equalsIgnoreCase("M") ? "Mr. " : "Ms. ");

    System.out.println("Hello " + gender2 + fullname + " and " + gender1 + fullName1 + " Welcome to the party!");
    System.out.println("");
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM