2015-01-10 142 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瞭解更多詳情。