每当我运行我的程序时,什么都不显示。在控制台窗口中,所有可见的都是空白的空白区域。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方法中调用的代码才会运行。 –