課程
/后端開發
/Java
/Java入門第一季(IDEA工具)升級版
能否用while語句打印*長方形?
2015-09-17
源自:Java入門第一季(IDEA工具)升級版 4-14
正在回答
public class Rectangle {?? ?public static void main(String[] args) {?? ??? ?// 打印長方形?? ??? ?// 第一種方法:for循環方法打印 打印8行,20列的長方形?? ??? ?/*?? ??? ? * for(int i=1;i<=5;i++){//外層循環控制行數 for(int?? ??? ? * j=1;j<=15;j++){//內層循環控制每一行打印的數量,即列數 System.out.print("*"); }?? ??? ? * System.out.println();//換行 }?? ??? ? */?? ??? ?// 第二種方法:while循環?? ??? ?int x = 1;?? ??? ?int y = 1;?? ??? ?while (x <= 5) {?? ??? ??? ?while (y <= 15) {?? ??? ??? ??? ?System.out.print("*");?? ??? ??? ??? ?y++;?? ??? ??? ?}?? ??? ??? ?System.out.println();?? ??? ??? ?y = 1;?? ??? ??? ?x++;?? ??? ?}?? ?}}
dumbaodouble
346746946 提問者
可以的,while和for幾乎是可以互換的,只是for可以定義些臨時變量而已,
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=0;
while(i < 3){
System.out.println("*********");
i++;
}
這樣算嗎,哈哈,反正看起來也是個長方形
舉報
0基礎萌新入門第一課,從Java環境搭建、工具使用、基礎語法開始
2 回答用while語句怎樣打印三角形哪里錯了
2 回答使用*打印長方形是什么意思?
1 回答用for循環怎么打印不出長方形?
1 回答打印長方形 我的怎么么不一樣?
3 回答為什么這段代碼不能打印長方形呢?請學長們指教。。。
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2015-09-17
public class Rectangle {
?? ?public static void main(String[] args) {
?? ??? ?// 打印長方形
?? ??? ?// 第一種方法:for循環方法打印 打印8行,20列的長方形
?? ??? ?/*
?? ??? ? * for(int i=1;i<=5;i++){//外層循環控制行數 for(int
?? ??? ? * j=1;j<=15;j++){//內層循環控制每一行打印的數量,即列數 System.out.print("*"); }
?? ??? ? * System.out.println();//換行 }
?? ??? ? */
?? ??? ?// 第二種方法:while循環
?? ??? ?int x = 1;
?? ??? ?int y = 1;
?? ??? ?while (x <= 5) {
?? ??? ??? ?while (y <= 15) {
?? ??? ??? ??? ?System.out.print("*");
?? ??? ??? ??? ?y++;
?? ??? ??? ?}
?? ??? ??? ?System.out.println();
?? ??? ??? ?y = 1;
?? ??? ??? ?x++;
?? ??? ?}
?? ?}
}
2015-09-18
可以的,while和for幾乎是可以互換的,只是for可以定義些臨時變量而已,
2015-09-17
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=0;
while(i < 3){
System.out.println("*********");
i++;
}
這樣算嗎,哈哈,反正看起來也是個長方形