每当我运行程序时,都不会显示任何内容。在控制台窗口中,所有可见的都是空白空格。
截图:
以下是我的代码粘贴:
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的初学者,从我的代码中你可以说我正在努力掌握创建类,方法和实例化对象的概念。事实上,我们应该打印出来自对象的所有信息,但我不太明白这意味着什么。我所拥有的大部分内容都来自在线或我的书中的例子。 我知道有类似的问题,但我认为我的情况略有不同。非常感谢你。
答案 0 :(得分:1)
在代码中向控制台输出内容的函数是public int getAge(...)
或getTargetHeartRange(..)
,它们从不被调用。您只在对象上调用了无参数构造函数,然后没有执行任何其他操作。如果您希望它们起作用,您必须实际调用这些函数。
类似的东西:
heartrates.getTargetHeartRange(fill in your arguments);
在您的情况下,如果您想要打印有意义的内容,我建议您实际为这些成员变量设置一些值(即在实例化对象后使用您的setter函数)。
答案 1 :(得分:0)
我认为问题的直接原因是除了创建HeartRate
实例之外,您的程序似乎没有做任何事情。如果您的代码实际调用了System.out.println(...)
调用的方法之一,您将只获得一些输出。
如果你只是想看一些输出,只需改变它:
heartrates = new HeartRates();
到
System.out.println("Offering low rates on second hand hearts!");
heartrates = new HeartRates();
第二个问题是您的代码每次要阅读时都会创建一个新的Scanner(System.in)
对象。这是一个坏主意,它可能会导致问题。根据您调用的Scanner
方法(以及输入流的性质),您最终可能会进行“预读”。如果发生这种情况,则无法再从System.in
读取已读取的字符。
您应该做的只是创建一个Scanner(System.in)
对象,并将其放入私有字段。
(更好的是从设计的角度来看,重构你的代码,以便HeartRate
对象根本不负责读取/解析用户的输入。但是,这可能是你没有做好准备的“教训”还没有。)