12345678_0001
2019-02-05 21:06:52
public class Program4 {public static void main(String[] args) {int num=13;String s1="*";System.out.print();}public static String makeRow(int n,String str){String result="";for(int i=1;i<=n;i++){result+= str;}return result;}要求再寫(xiě)一個(gè)printTriangle函數(shù)并調(diào)用makeRow打印如下:
3 回答

弒天下
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊
這個(gè)函數(shù)的概念在java中準(zhǔn)確來(lái)說(shuō)就是一個(gè)方法。自定義方法的話,實(shí)際上就是一個(gè)類中創(chuàng)建一個(gè)提問(wèn)者自己寫(xiě)的方法即可。
舉例如下:
public class Sum
{
public static int sum(int a,int b )//靜態(tài)方法
{
return a+b;
}
public int sum1(int a,int b ) //動(dòng)態(tài)方法
{
return a+b;
}}
備注:以上就是定義了一個(gè)求和的“函數(shù)”。之后就可以通過(guò)Sum.sum(1,2)進(jìn)行方法調(diào)用了。

青春有我
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊
詳細(xì)代碼如下:
12345678910111213141516171819202122232425262728 | public class Program4 { public static void main(String[] args) { int num= 13 ; String s1= "*" ; printTriangle(num,s1); } public static String makeRow( int n,String str) { String result= "" ; for ( int i= 1 ;i<=n;i++) { result+= str; } return result; } public static void printTriangle( int rows,String str) { for ( int i= 1 ;i<=rows;i++) { String result = makeRow(i, str); String replacement = "" ; for ( int j=rows-i;j > 0 ;j--) { replacement += " " ; } result = replacement + result; System.out.println(result); } } } |
添加回答
舉報(bào)
0/150
提交
取消