本文介绍了Java如何传递和返回字符串,方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Scanner;

import javax.swing.JOptionPane;

public class StarWars {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String firstName = "";
        String lastName = "";
        String maidenName = "";
        String town = "";

        System.out.print("What is your first name? ");
        firstName = reader.nextLine();
        System.out.print("What is your last name? ");
        lastName = reader.nextLine();
        System.out.print("What is your mothers maiden name? ");
        maidenName = reader.nextLine();
        System.out.print("What town were you born? ");
        town = reader.nextLine();

        String Sfirstname = firstName.substring(0,2);
        String Slastname = lastName.substring(0,3);
        String SmaidenName = maidenName.substring(0,2);
        String Stown = town.substring(0,3);
        String Star = Sfirstname + Slastname;
        String War = SmaidenName + Stown;
        String StarWar = Star + War;
        System.out.print("Your Star Wars name is: " + StarWar);
    }

    public static String StarWar (String Star, String War) {
        String name;
        name = Star + " " + War;
        return War;
    }
}

这是我关于我的项目的代码.在执行项目时,我对返回方法和传递方法有一些疑问.

So this is my code about my project. While I'm doing my project I have some problem about the returning method and passing method.

我完美地设置了main方法来打印出我想看的东西.

I set up the main method perfectly to print out thing that what I want to see.

问题是我还必须使用传递方法和返回方法.我的老师要我通过传递/返回方法做两件事.

The problem is I also have to use passing method and returning method. My teacher want me to do two things with passing/returning method.

  1. 将所有这些数据传递给您的方法,该方法应生成并返回用户星球大战"名称.
  2. 获取方法的返回值,并将其显示在屏幕上.

我不知道该怎么办(花了5个小时来做​​我学到的所有事情,但是错了..)

I have no idea what should I do with this problems (took 5 hrs to do everything I learn but wrong..).

有人可以提示或教我我的老师实际上要我做什么以及我该怎么做吗?

Can someone give a hint or teach me what actually my teacher want me to do and How I can do this?

我真的需要你们的帮助.

I really need help from you guys.

另外,如果我运行一个程序,它应该像这样.

Additional, if I run a program it should be like this.

名字?用户输入:爱丽丝的姓氏?用户输入:史密斯娘家姓?用户输入:马塔镇你出生吗?用户输入:萨克拉曼多

first name? user input: Alice last name? user input:Smith mothers maiden name? user input: Mata town were you born? user input: Sacramento

您的星球大战名称是:SmiAl MaSac

Your Star Wars name is: SmiAl MaSac

推荐答案

我们可以在此处进行一些改进,让我们从方法开始-方法名称看起来像构造函数,并且本身不执行逻辑,描述它的作用并将逻辑转移到方法中-我们不需要所有这些临时变量(我们可以使用StringBuilder),例如

There are a few things we can improve here, lets start with the method - the method name looks like a constructor and doesn't perform the logic itself, lets describe what it does and move the logic into the method - we don't need all of those temporary variables (we can use a StringBuilder) like

public static String buildStarWarsName(String firstName, String lastName,
            String maidenName, String town)
{
    return new StringBuilder(lastName.substring(0, 3)) //
            .append(firstName.substring(0, 2)) //
            .append(" ") // <-- for the space between first and last
            .append(maidenName.substring(0, 2)) //
            .append(town.substring(0, 3)) //
            .toString();
}

然后,您可以在读取变量时对其进行初始化,并最终调用该方法

Then you can initialize your variables when you read them and finally call the method

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    System.out.print("What is your first name? ");
    String firstName = reader.nextLine();
    System.out.print("What is your last name? ");
    String lastName = reader.nextLine();
    System.out.print("What is your mothers maiden name? ");
    String maidenName = reader.nextLine();
    System.out.print("What town were you born? ");
    String town = reader.nextLine();

    System.out.print("Your Star Wars name is: " + //
            buildStarWarsName(firstName, lastName, maidenName, town));
}

这篇关于Java如何传递和返回字符串,方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:32