初学Java-为什么不能编译?

时间:2014-09-29 08:53:10

标签: java string methods parameters int

为什么这个程序不会编译?我尝试运行它时有7个错误。

我正在尝试这样做:

  

编写一个名为inputBirthday的方法,该方法接受控制台的扫描仪作为参数,并提示用户输入出生月份,日期和年份,然后以合适的格式打印出生日期。

以下是与用户的对话示例:

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!
import java.util.*;

public class Practice2 {
    public static void inputBirthday(int number, String sentence, int number2){
        Scanner console = new Scanner(System.in);
        System.out.println("On what day of the month were you born?");
        int number = console.nextInt();
        System.out.println("What is the name of the month in which you were born?");
        String sentence = console.nextString();
        System.out.println("During what year where you born?");
        int number2 = console.nextInt();
    }
    public static void main(String[] args){
        System.out.print("You were born on" + sentence + number
            + "," + number2 + ". You're might old!");
    }
}

5 个答案:

答案 0 :(得分:1)

尝试下面的代码,它将编译

     import java.util.*;

        public class Practice2 {

public static void inputBirthDay(int number,String sentence,int number2)
{
 System.out.print("You were born on" + sentence + number
                    + "," + number2 + ". You're might old!");
}

            public static void main(String[] args){
                 Scanner console = new Scanner(System.in);
                System.out.println("On what day of the month were you born?");
                int number = console.nextInt();
                System.out.println("What is the name of the month in which you were born?");
                String sentence = console.next();
                System.out.println("During what year where you born?");
                int number2 = console.nextInt();
              inputBirthDay(number,sentence,number2);

            }
        }

输出结果为:

你出生在这个月的哪一天? 21

您出生的月份名称是什么? septemper

你出生的那一年? 1990年

你出生于onseptemper21,1990。你可能老了!

答案 1 :(得分:1)

您应该在main()方法中调用该函数。试试这个。

import java.util.*;
public class Practice2 {
int number=0, number2=0;
String sentence="";
public static void inputBirthday(){
    Scanner console = new Scanner(System.in);
    System.out.println("On what day of the month were you born?");
    number = console.nextInt();
    System.out.println("What is the name of the month in which you were born?");
    sentence = console.nextString();
    System.out.println("During what year where you born?");
    number2 = console.nextInt();
}
public static void main(String[] args){
    inputBirthday();
    System.out.print("You were born on" + sentence + number
        + "," + number2 + ". You're might old!");
}
}

答案 2 :(得分:0)

您的主要方法是应用程序的入口点! 你在那里调用的所有变量都是未定义的,因为它们是在inputBirthday()方法中声明的局部变量。

答案 3 :(得分:0)

首先,您必须在使用main方法之前定义变量。

其次,一旦在inputBirthday方法中定义,必须使用变量,而不是重新定义(它们将被称为重复)。

第三,扫描未提供,nextString()方法,使用,next()

而且,这些都是非常基本的东西,我建议您在进行编码之前先阅读基本教程。

答案 4 :(得分:0)

您的变量sentence, number, number2超出了Main方法的范围。因此,您将收到错误。