0
// Binomial Expansions
String the_x, the_y;
System.out.println("Binomial Expansions:");
for(int i=0; i<=max; i++){
num = 1;
r = i+1;
xpow=i;
ypow=0;
System.out.print("(x + y)^"+i+" = ");
if(i==0) System.out.print("1");
for(int j=0; j<=i; j++){
if(j>0){
num = num*(r-j)/j;
System.out.print("x^"+(xpow+1)+" + ");
System.out.print(num+"x^"+xpow+"y^"+ypow);
}
xpow--;
ypow++;
}
System.out.println();
} // End of binomial expansions
我對我的代碼有幾個問題。java沒有打印任何東西0
如何不打印0和1?並且有什麼推薦的方法可以在其功率爲0或1時擺脫
^
?我打印
x
兩次的原因是,我想x位於在y之前,像這樣(x + y)^5 = x^5 + 5x^4y + 10x^3y^2 + 10x^2y^3 + 5xy^4 + y^5
但是,如果我不X打印了兩次,我的結果看起來像這樣(x + y)^5 = x^5 + 5y^1x^4 + 10y^2x^3 + 10y^3x^2 + 5y^4x^1 + 1y^5
。我能做什麼?我認爲我的標題和標籤不合適,我有什麼樣的麻煩,我可以稍後搜索?
我看了有什麼DecimalFormat
和TypeCast
,但我不認爲這兩個是不適用這種情況。在此先感謝
我可能會誤解你的問題,但爲什麼不使用if語句來檢查0或1,對於這種情況下,有不同的印刷線? – TheFooBarWay
我也這麼想過。我很好奇還有其他方法可以做到這一點。 #2對我來說是個大問題 – jaykodeveloper