如何在此程序中仅使用两个字符串?
我的老师给了我这样一个问题:
编写一个执行以下操作的程序:
- 输入您的名字: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用一个字符串来获取全名,并使用一个字符串来获取年份。
你可以这样做
You can use one string to get the full name and one for the year..
you can do it this way
您可以重复使用字符串,因此
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 withfirst = 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)
编辑:抱歉,我以为您正在使用 java.util.Scanner 类,因此以下内容不会有帮助,除非您想切换到该类。
以下内容是在您使用的假设下编写的:
上一篇文章:
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:
Previous post: