2015-01-10 146 views
-1

我是Java的成员,我正在用这种语言做我的第一个婴儿步骤。我只是看了一个教程,并试图编写一些基本的东西。一切似乎都很好,没有任何警告或错误,但是我的代码只是不编译或工作,当我按运行时什么都没有发生。这是我的代码:Java代码不能编译或运行

import java.util.Scanner; 

public class pagrindinis { 
    private String name; 
    private int Id; 
    private int Age; 
    Scanner userInput = new Scanner(System.in); 

public void Gyvunas() { 
    System.out.println("Animal:"); 
    this.setName(userInput.nextLine()); 
    System.out.println("Age?:"); 
    this.setAge(userInput.nextInt()); 
    System.out.println("Animal is " + name + ", " + " age is " + Age + ", " + "animal ID is " + Id); 

} 

    public static void main(String[] args) { 
    } 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name;  
     } 

     public int getId() { 
      return Id; 
     } 

     public void setId(int Id) { 
      this.Id = Id; 

     System.out.println("Animal ID is - " + this.Id); 
     } 

     public void setId() { 
      this.Id = (int) Math.random()*(10-1)+1; 
     } 
     public int getAge() { 
      return Age; 
     } 
     public void setAge(int Age) { 
      this.Age = Age; 
     } 
} 

任何想法?我很确定这很愚蠢,但我仍然无法做到。谢谢!

+2

因为你的main()是空的。 – Karthikeyan

回答

2

你的代码编译得很好。如果它没有编译,你会有错误。

它也运行。但是你的main方法是空的,所以它什么都不做。

在Java中,程序运行在main方法中编写的任何内容。如果什么都没有,那么程序将什么也不做。

+0

它现在有效。谢谢! – Martynas

2

尝试调用所需的方法。

public static void main(String[] args) { 
     Gyvunas(); 
    } 

为了您的信息有某些约定,你似乎违反。方法名称应该以小写字母开头,类名称应该以大写字母开头。请按照此code naming conventions了解更多详情。