gpt4 book ai didi

java - 如何在此程序中只使用两个字符串?

转载 作者:行者123 更新时间:2023-11-30 07:28:05 25 4
gpt4 key购买 nike

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

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%)

现在我对 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。请帮忙!附言他说只要我不把它当成我自己的想法就可以得到帮助。

最佳答案

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

    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

关于java - 如何在此程序中只使用两个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9322029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com