java 返回字符串,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.

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.

Pass all this data to your method, and the method should generate and return the users Star Wars name.

Get the return value of the method, and display it to the screen.

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

Your Star Wars name is: SmiAl MaSac

解决方案

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(" ") //

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值