所以我正在嘗試創建一個程式來顯示它所生活的恐龍時代和其他東西,但我目前被困在這個時代。
這是問題:
setTimePeriod(method) – 采用一個 int 引數,表示恐龍生活在幾百萬年前,并且不回傳任何內容;將時間段實體變數設定為時間段的中間。請參閱下面的詳細資訊
getTimePeriod(method) – 不帶引數;回傳一個包含“三疊紀”、“侏羅紀”或“白堊紀”之一的字串。使用時間段實體變數來計算時間段。設定時間段回想中生代,恐龍生活的時候,分為三個時期
三疊紀(大約 252 到 2.01 億年前)
侏羅紀(大約 201 到 1.45 億年前)
白堊紀(大約 145 到 6600 萬年前)將時間段的實體變數設定為時間段的中間。
這給出了以下數字
三疊紀設定為 227
侏羅紀設定為 173
白堊紀設定為 106
這是我寫的:
public String getTimePeriod()
{
if (Triassic > 201 && Triassic < 252)
{
return "Triassic period";
}
if (Jurrasic > 145 && Jurrasic <201)
{
return "Jurrasic period";
}
if (Cretaceous > 66 && Cretaceous < 145)
{
return "Cretaceous period";
}
}
// Returns a String representing which
// period the dinosuar lived in
// based on the value of the time period instance variable
// setTimePeriod
public int setTimePeriod(int age)
{
int Triassic = 227;
int Jurrasic = 173;
int Cretaceous = 106;
}
誰能告訴我該怎么做?
uj5u.com熱心網友回復:
我建議您重復 Java 中的封裝。
這個任務的所有條件都很難理解,所以這是我解決這個問題的方法:
public class Dinosauros {
private String namePeriod;
private int timePeriod;
public void setTimePeriod(int age) {
if (age < 252 && age > 201) {
this.timePeriod = 227;
} else if (age < 201 && age > 145) {
this.timePeriod = 173;
} else if (age < 145 && age > 66) {
this.timePeriod = 106;
} else {
System.out.println("Enter correct time period.");
}
}
public String getNamePeriod() {
if (timePeriod == 227) this.namePeriod = "Triassic";
else if (timePeriod == 173) this.namePeriod = "Jurassic";
else if (timePeriod == 106) this.namePeriod = "Cretaceous";
return namePeriod;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/511289.html
標籤:爪哇班级变量方法类方法
