2015-07-19 502 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对象不负责读取/所有解析用户的输入。然而,这可能是一个“教训”,你是不是准备好了,只是还没有。 )