如何在此程序中仅使用两个字符串?

发布于 2025-01-06 05:48:21 字数 1642 浏览 3 评论 0原文

我的老师给了我这样一个问题:

编写一个执行以下操作的程序:

  • 输入您的名字:Peter
  • 输入您的姓氏:Opunga
  • 输入您的出生年份:1992
  • 嗨,Peter Opunga,到今年年底您就 20 岁了。
  • 奖励 1:正确的评论。 (10%)
  • 奖励 2:在整个程序中仅创建 2 个字符串。 (10%)
  • 奖励 3:恰好使用 4 个 System.out.print。 (10%)

现在我对 Java 完全陌生。一个多星期前我才被介绍到它,这就是我的想法:

import java.io.*;

public class A1Chuah {

    public static void main(String[] args) throws IOException {

        BufferedReader in = new BufferedReader (new InputStreamReader (System.in));

        //Prints instructions for user to enter first name.
        System.out.println("Enter first name: ");
        //Obtains user input for first name.
        String first = in.readLine ();
        //Prints instructions for user to enter last name.
        System.out.print ("Enter last name: ");
        //Obtains user input for last name.
        String last = in.readLine();
        //Prints instructions for user to enter birth year.
        System.out.print ("Enter your birth year: ");
        //Obtains user input for birth year.
        String year = in.readLine ();
        //Converts String year to int useryr
        int useryr = Integer.parseInt(year);
        //Sets int oriyr to value of 2012
        int oriyr = 2012;
        //Sets int outyr as oriyr - useryr
        int outyr = oriyr - useryr;
        //Prints out information.
        System.out.println("Hi" + " " + " " + last + "," + " " + "you will be" +  " " + outyr + " " + "by the end of this year");

我已经成功完成了奖励 1 和 3,但似乎无法弄清楚奖励 2。请帮忙! PS 他说只要我不试图冒充我自己的想法,我就可以获得帮助。

My teacher gave me this question:

Write a program that does the following:

  • Enter your first name: Peter
  • Enter your last name: Opunga
  • Enter your birth year: 1992
  • Hi Peter Opunga, you will be 20 by the end of this year.
  • Bonus 1: Proper commenting. (10%)
  • Bonus 2: Create only 2 Strings in the entire program. (10%)
  • Bonus 3: Use exactly 4 System.out.print. (10%)

Now I am completely new to Java. I have just been introduced to it a little over a week ago and this is what I came up with:

import java.io.*;

public class A1Chuah {

    public static void main(String[] args) throws IOException {

        BufferedReader in = new BufferedReader (new InputStreamReader (System.in));

        //Prints instructions for user to enter first name.
        System.out.println("Enter first name: ");
        //Obtains user input for first name.
        String first = in.readLine ();
        //Prints instructions for user to enter last name.
        System.out.print ("Enter last name: ");
        //Obtains user input for last name.
        String last = in.readLine();
        //Prints instructions for user to enter birth year.
        System.out.print ("Enter your birth year: ");
        //Obtains user input for birth year.
        String year = in.readLine ();
        //Converts String year to int useryr
        int useryr = Integer.parseInt(year);
        //Sets int oriyr to value of 2012
        int oriyr = 2012;
        //Sets int outyr as oriyr - useryr
        int outyr = oriyr - useryr;
        //Prints out information.
        System.out.println("Hi" + " " + " " + last + "," + " " + "you will be" +  " " + outyr + " " + "by the end of this year");

I've managed to complete Bonus 1 and 3 but can't seem to figure out Bonus 2. Please help!
P.S. He said that I can get help as long as I don't try to pass it off as my own idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霓裳挽歌倾城醉 2025-01-13 05:48:21

您可以使用一个字符串来获取全名,并使用一个字符串来获取年份。
你可以这样做

    System.out.println("Enter first name: ");

    String name = in.readLine();

    System.out.print ("Enter last name: ");

    name+= " " + in.readLine();

    System.out.println(name); // should print first and last

You can use one string to get the full name and one for the year..
you can do it this way

    System.out.println("Enter first name: ");

    String name = in.readLine();

    System.out.print ("Enter last name: ");

    name+= " " + in.readLine();

    System.out.println(name); // should print first and last
初熏 2025-01-13 05:48:21

您可以重复使用字符串,因此 String last = in.readLine(); 可以替换为 first = in.readLine();

现在,如果您只需要一个字符串从中读取,并且只需要一个字符串作为答案...您应该能够从那里弄清楚如何仅使用两个字符串

(提示 http://docs.oracle.com/javase/tutorial/java/data/strings.html

You can reuse strings, so String last = in.readLine(); can be replace with first = in.readLine ();

Now, if you only need one string to read in from, and only need one string for the answer... you should be able to figure out from there how to only use two strings

(Hint http://docs.oracle.com/javase/tutorial/java/data/strings.html)

瞳孔里扚悲伤 2025-01-13 05:48:21

编辑:抱歉,我以为您正在使用 java.util.Scanner 类,因此以下内容不会有帮助,除非您想切换到该类。

以下内容是在您使用的假设下编写的:

java.util.Scanner in = new java.util.Scanner(System.in);
in.nextLine();

上一篇文章:

我认为您正在寻找 in.nextInt(); 方法而不是
解析返回的字符串以获取出生年份。

另请注意,扫描仪具有更多功能,例如扫描
模式(RegEx)和标记字符串输入。

将来您可能想要使用像 Netbeans 这样的 IDE
允许您快速浏览类的可能方法
自动完成。如果您被允许这样做,那就是。

EDIT: Sorry I thought you were using the java.util.Scanner class so the following will not be of help unless you feel like switching to that instead.

The following was written under the assumption you were using:

java.util.Scanner in = new java.util.Scanner(System.in);
in.nextLine();

Previous post:

I think you are looking for the in.nextInt(); method instead of
parsing a returned string for year of birth.

Also be aware Scanner has way more capability such as scanning for
patterns (RegEx) and tokenizing string input.

In the future you may want to use an IDE like Netbeans which
allows you to quickly browse through possible methods of a class with
the auto-complete. If you are allowed to do so that is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文