Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

Beginner in Java - Using Parameters and Scanner

Write a method called inputBirthday that accepts a Scanner for the console as a parameter and prompts the user to enter a month, day, and year of birth, then prints the birthdate in a suitable format. Here is an example dialogue with the user:

On what day of the month were you born? 8
What is the name of the month in which you were born? May
During what year were you born? 1981
You were born on May 8, 1981. You're mighty old!

So this is what I have done:

import java.util.Scanner;

public static void inputBirthday(int month,String day,int year){
    Scanner sc=new Scanner(System.in);
    System.out.print("On what day of the month were you born?");
    month=sc.nextInt();

    System.out.print("What is the name of the month in which you were born?");
    day=sc.nextLine();

    System.out.print("During what year were you born?");
    year=sc.nextInt();  
}

My code failed to compile. Can someone give me some hints and I will try it out on my own.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Classes need declarations. Java is an OO language so using a class is a must:

class MyClass {
   public static void inputBirthday(int month, String day, int year) {
      ...
   }
}

Your inputBirthday could be replaced with a main method which would give your class an entry point from which to run your program.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...