每當我運行我的程序時,什麼都不顯示。在控制檯窗口中,所有可見的都是空白的空白區域。Java:控制檯不顯示任何輸出
截圖:
這裏是我的代碼粘貼:
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,並從我的代碼,你可能會說,我努力把握創建類,方法和實例化對象的概念。實際上,我們應該打印出對象的所有信息,但我不太明白這意味着什麼。我所擁有的絕大部分都是基於在線或來自我的書籍的例子。 我知道有類似的問題,但我認爲我的情況稍有不同。非常感謝。
你在main中創建一個空的HeartRates對象,但不要對它做任何事情,比如調用方法 - 你期望在控制檯中看到什麼?爲什麼?爲了更好地理解正在發生的事情,請仔細閱讀你的代碼,看看它在每一步都在做什麼。請記住,只有在main方法中調用的代碼纔會運行。 –