2015-07-19 43 views
-1

每當我運行我的程序時,什麼都不顯示。在控制檯窗口中,所有可見的都是空白的空白區域。Java:控制檯不顯示任何輸出

截圖: enter image description here

這裏是我的代碼粘貼:

import java.util.Scanner; 

public class Foothill 
{ 
    public static void main(String[] args) 
    { 
     // declare the references 
     //warning for line below 
     HeartRates heartrates; 
     //warning for line above: the value of the local variable heartrates is not used 

     // instantiate the object 
     heartrates = new HeartRates(); 
    } 
} 

class HeartRates { 

    // member data 
    public String firstName, lastName; 
    public int birthMonth, birthDay, birthYear, personAge, maxRate, minRange, maxRange; 

    // default constructor 
    HeartRates() { 
    } 

    // 2-parameter constructor 
    HeartRates (String userFirstname, String userLastname, int userBirthmonth, int userBirthday, int userBirthYear, int userAge, int heartMax, int userMin, int userMax){ 
     firstName = userFirstname; 
     lastName = userLastname; 
     birthMonth = userBirthmonth; 
     birthDay = userBirthday; 
     birthYear = userBirthYear; 
     personAge = userAge; 
     maxRate = heartMax; 
     minRange = userMin; 
     maxRange = userMax; 
    } 

    // accessor "get" methods -------------------------------- 
    public String getFirstname(String userFirstname, String firstName) { 
     firstName = userFirstname; 
     Scanner inputStream = new Scanner (System.in); 
     userFirstname = inputStream.nextLine(); 
     System.out.print("Your first name is: " + userFirstname); 
     inputStream.close(); 
     return firstName; 
    } 
    public String getLastName(String userLastname, String lastName){ 
     lastName = userLastname; 
     Scanner inputStream = new Scanner (System.in); 
     userLastname = inputStream.nextLine(); 
     System.out.print("Your last name is: " +userLastname); 
     inputStream.close(); 
     return lastName; 
    } 
    public int getBirthmonth(int userBirthmonth, int birthMonth){ 
     birthMonth = userBirthmonth; 
     Scanner inputStream = new Scanner (System.in); 
     userBirthmonth = inputStream.nextInt(); 
     int monthBirth = Integer.parseInt("userBirthmonth"); 
     System.out.print(monthBirth); 
     inputStream.close(); 
     return birthMonth; 
    } 
    public int getBirthday(int userBirthday, int birthDay){ 
     birthDay = userBirthday; 
     Scanner inputStream = new Scanner (System.in); 
     userBirthday = inputStream.nextInt(); 
     int dayBirth = Integer.parseInt("userBirthday"); 
     System.out.print(dayBirth); 
     inputStream.close(); 
     return birthDay; 
    } 
    public int getBirthyear(int userBirthyear, int birthYear){ 
     birthYear = userBirthyear; 
     Scanner inputStream = new Scanner (System.in); 
     userBirthyear = inputStream.nextInt(); 
     int yearBirth = Integer.parseInt("userBirthyear"); 
     System.out.print(yearBirth); 
     inputStream.close(); 
     return birthYear; 
    } 
    public int getAge(int birthMonth, int birthDay, int birthYear, int userAge){ 
     personAge = userAge; 
     Scanner inputStream = new Scanner (System.in); 
     System.out.print("Please enter the current month in numbers: "); 
     int theMonth = inputStream.nextInt(); 
     System.out.print("Please enter the current day in numbers: "); 
     int theDay = inputStream.nextInt(); 
     System.out.print("Please enter the current year in numbers: "); 
     int theYear = inputStream.nextInt(); 
     userAge = theYear - birthYear; 
     if ((theMonth == birthMonth && theDay < birthDay) || theMonth < birthMonth){ 
      userAge--; 
      System.out.println("Your date of birth is: " + birthMonth + "/" + birthDay + "/" + birthYear); 
      System.out.println("You are " + userAge + " years old."); 
     } 
     else { 
      System.out.println("Your are " + userAge + " years old."); 
     } 
     inputStream.close(); 
     return userAge; 
    } 
    public int getMaximumHeartRate(int heartMax, int userAge, int maxRate){ 
     maxRate = heartMax; 
     heartMax = 220 - userAge; 
     return maxRate; 
    } 
    public double getMinTargetHeartRate(int heartMax, int userMin, int minRange){ 
     minRange = userMin; 
     userMin = (int)(heartMax*0.5); 
     return minRange; 
    } 
    public int getMaxTargetHeartRate(int heartMax, int userMax, int maxRange){ 
     maxRange = userMax; 
     userMax = (int)(heartMax*0.85); 
     return maxRange; 
    } 
    public void getTargetHeartRange(int heartMax, int userMin, int userMax){ 
     System.out.println("Your maximum heart rate is " + heartMax + "beats per minute."); 
     System.out.print("Your target-heart-rate range is from " + userMin + " to " + userMax + " beats per minute."); 
    } 

    // accessor "set" method ------------------------------- 
    public void setFirstname(String userFirstname, String firstName){ 
     firstName = userFirstname; 
    } 
    public void setLastname(String userLastname, String lastName){ 
     lastName = userLastname; 
    } 
    public void setBirthmonth(int userBirthmonth, int birthMonth){ 
     birthMonth = userBirthmonth; 
    } 
    public void setBirthday(int userBirthday, int birthDay){ 
     birthDay = userBirthday; 
    } 
    public void setBirthyear(int userBirthyear, int birthYear){ 
     birthYear = userBirthyear; 
    } 
    public void setAge(int userAge, int personAge){ 
     personAge = userAge; 
    } 
    public void setMaximumHeartRate(int heartMax, int maxRate){ 
     maxRate = heartMax; 
    } 
    public void setMinTargetHeartRate(int userMin, int minRange){ 
     minRange = userMin; 
    } 
    public void setMaxTargetHeartRate(int userMax, int maxRange){ 
     maxRange = userMax; 
    } 

} 

我是一個初學者到Java,並從我的代碼,你可能會說,我努力把握創建類,方法和實例化對象的概念。實際上,我們應該打印出對象的所有信息,但我不太明白這意味着什麼。我所擁有的絕大部分都是基於在線或來自我的書籍的例子。 我知道有類似的問題,但我認爲我的情況稍有不同。非常感謝。

+6

你在main中創建一個空的HeartRates對象,但不要對它做任何事情,比如調用方法 - 你期望在控制檯中看到什麼?爲什麼?爲了更好地理解正在發生的事情,請仔細閱讀你的代碼,看看它在每一步都在做什麼。請記住,只有在main方法中調用的代碼纔會運行。 –

回答

1

將代碼打印到控制檯上的函數是public int getAge(...)getTargetHeartRange(..),這些函數從不會被調用。你只需要在你的對象上調用無參數的構造函數,然後不做任何事情。如果你想讓它們工作,你必須實際調用這些函數。

喜歡的東西:

heartrates.getTargetHeartRange(fill in your arguments); 

在你的情況,如果你想打印一些有意義的東西,我建議你實際上設置這些成員變量的一些值(即使用您的setter函數實例化對象後)。

0

我認爲你的問題的直接原因是你的程序似乎沒有做任何事情,除了創建一個HeartRate實例。如果您的代碼實際調用了其中一個帶有System.out.println(...)調用的方法,則只會得到一些輸出。

如果你只是想看到一些輸出,只是改變這一點:

heartrates = new HeartRates(); 

System.out.println("Offering low rates on second hand hearts!"); 
    heartrates = new HeartRates(); 

的第二個問題是,你的代碼是你想每次都創建一個新的Scanner(System.in)對象讀。這是一個壞主意,而且很容易引起問題。根據您調用的方法(以及輸入流的性質),您最終可能會執行「預讀」。如果發生這種情況,那麼已讀取的字符將不會再從System.in中讀取。

你應該做的只是創建一個Scanner(System.in)對象,並將其放入一個專用字段。

(更妙的是從設計的角度來看,重構你的代碼,以便HeartRate對象不負責讀取/所有解析用戶的輸入。然而,這可能是一個「教訓」,你是不是準備好了,只是還沒有。 )

相關問題