2016-10-08 36 views
0

我正在制定一個程序,計算一年中給定的開始年(2011年)的人口數量,每年增加1.2%的人口。 2011年的人口是7.000(我使用的是小數,而不是數十億)。這裏是我的代碼試圖讓一段時間循環正常工作

package src; 

import java.util.Scanner; 

import java.text.DecimalFormat; 

public class Demographics { 

    public static void main(String[] args) { 
     Scanner user_input= new Scanner(System.in); 

     //Part 1 

     System.out.println("===--- Part 1 ---==="); 
     System.out.println("Population in 2011: 7.000"); 
     System.out.print("What is the desired year? (> 2011) "); 
     int startYear = 2011; 
     int endYear = user_input.nextInt(); 
     while (endYear <= startYear){ 
      System.out.println("Invalid end year."); 
      System.out.print("What is the desired year? (> 2011) "); 
      endYear = user_input.nextInt(); 
      break; 
     } 
     double t = 0.012; 
     double nbr = (endYear - startYear); 
     double pStart = 7.000; 
     double pEnd = pStart * Math.exp(nbr * t); 
     DecimalFormat nf = new DecimalFormat("#.000"); 
     System.out.println("Population in " + endYear + ":(nf.format(pEnd))); 

    //Part 2 

    System.out.println("===--- Part 2 ---==="); 
    System.out.print("What is the target population? (> 7.000) "); 
    double pTarget = user_input.nextDouble(); 
    while (pTarget <= pStart){ 
     System.out.println("Invalid target population."); 
     System.out.print("What is the target population? (> 7.000) "); 
     pTarget = user_input.nextDouble(); 
     break; 
    } 
    while (pStart < pTarget){ 
     startYear++; 
     pStart = pStart + (pStart * 0.012); 
     System.out.println("Population in " + startYear + ": " + nf.format((pStart))); 
    } 
} 
} 

部分的我的代碼1計算,當用戶進入其每年的人口工作的一部分,則第2部分展示了多少年,當用戶輸入一個將採取的計算人口達到這一點。

這裏是行不通

//Part 3 

    System.out.println("===--- Part 3 ---==="); 
    t = 1.2; 
    pStart = 7.000; 
    pEnd = pStart * Math.exp(nbr * t); 
    while (pStart < pTarget){ 
     startYear++; 
     pEnd = pStart + (pStart * 0.012); 
     if (pEnd >= pStart * 2){ 
     System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + (t/2)); 
     }else{ 
      System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + t); 
     } 
    } 

目前,當我在我的代碼它的無限循環,而不人口乘以有3部分的代碼。我在第3部分中要做的與第2部分中的內容大致相同,但在第3部分中,它將顯示人口增長率(t),並在人口翻倍時將其除以2。例如:

2019年的人口:7.705;人口增長率:1.2%

2020年人口:7.798;人口增長率:1.2%

2021年人口:7.892;人口增長率:1.2%

...

人口六八年:13.873;人口增長率:1.2%

2069年人口:14.040;人口增長率:0.6%

任何人有任何想法如何實現這一目標?

+2

呃,除非我讀錯了,否則你永遠不會在你的循環中改變pStart。那麼如何滿足循環退出條件呢? – OldProgrammer

+0

我會在循環中更改pStart? –

+0

也許沒什麼。這很可能意味着你的循環條件沒有意義。逐行檢查您的代碼。 – OldProgrammer

回答

0

看來你在while循環中有兩個變量,你試圖比較,但是這些在while循環中迭代時實際上並沒有得到更新。因此,在循環內的每個迭代之後,while條件保持爲真。 請參閱下面的一個小的改動的最後幾行到您的第3部分代碼

System.out.println("===--- Part 3 ---==="); 
t = 1.2; 
pStart = 7.000; 
pEnd = pStart * Math.exp(nbr * t); 
while (pStart < pTarget){ 
    startYear++; 
    pEnd = pStart + (pStart * 0.012); 
    if (pEnd >= pStart * 2){ 
     System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + (t/2)); 
    } else { 
      System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + t); 
    } 
    pStart = startYear; // <-- NEW, you can probably use pStart instead of startYear in this code 
} 
3

如果你有循環的原因是在條件的問題! (你的條件一致)

A)PSTART應增加價值

B)PTARGET應遞減:

while (pStart < pTarget){ 

這樣結束這段代碼,循環的這種情況一個具有發生內部in value

C)「休息」;必須發生

在你增加的代碼中:startYear和pEnd,但這不是根據你的條件關閉循環的條件。 (我之前寫過:A,B,C)

1)也startYear不重新初始化它在循環之前,並且已經開始在一個高值。你必須添加bedore循環:

startYear = 2011; 

2),你應該儘可能爲3段創建新的變量,是不是有像剛纔描述的一個問題,就是要清楚你是做。

我的第三部分的建議是: (考慮到我還沒有明確的什麼我想要做閱讀你的代碼,你必須cange它,讓它對你有好處)

System.out.println("===--- Part 3 ---==="); 
     t = 1.2; 
     startYear = 2011; // I add it 
     double pEveryYear = 7000; 
     while (pEveryYear < pTarget){ 
      startYear++; 
      pEveryYear = pEveryYear + (pEveryYear * 0.012); 
      if (pEveryYear >= pTarget ){ // this condition cange only the print in the console 
       System.out.println("Population in " + startYear + ": " + nf.format((pEveryYear)) + " Population growth rate " + ": " + (t/2)); 
       break; // if you write it before the system.out you can't read it in the console. 
      }else{ 
       System.out.println("Population in " + startYear + ": " + nf.format((pEveryYear)) + " Population growth rate " + ": " + t); 

      } 
     } 
    } 

這個控制檯輸出像「8000」輸入:

===--- Part 3 ---=== 
Population in 2012: 7084.000 Population growth rate : 1.2 
Population in 2013: 7169.008 Population growth rate : 1.2 
Population in 2014: 7255.036 Population growth rate : 1.2 
Population in 2015: 7342.097 Population growth rate : 1.2 
Population in 2016: 7430.202 Population growth rate : 1.2 
Population in 2017: 7519.364 Population growth rate : 1.2 
Population in 2018: 7609.596 Population growth rate : 1.2 
Population in 2019: 7700.912 Population growth rate : 1.2 
Population in 2020: 7793.323 Population growth rate : 1.2 
Population in 2021: 7886.842 Population growth rate : 1.2 
Population in 2022: 7981.485 Population growth rate : 1.2 
Population in 2023: 8077.262 Population growth rate : 0.6 
0

我在你的代碼中看到的唯一缺少的東西是,你是不是每次都要經過循環時間更新PSTART。這不僅會使循環無限,而且除了第一次迭代之外,每次都會進行錯誤的計算。我在你的代碼中只添加了一行:

System.out.println("===--- Part 3 ---==="); 
t = 1.2; 
pStart = 7.000; 
pEnd = pStart * Math.exp(nbr * t); 
while (pStart < pTarget){ 
    startYear++; 
    pEnd = pStart + (pStart * 0.012); 
    if (pEnd >= pStart * 2){ 
     System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + (t/2)); 
    }else{ 
     System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + t); 
    } 
    pStart = pEnd; 
}