JAVA 入門編程

來源:互聯網
上載者:User

標籤:lin   ase   nbsp   tin   int   掃描   退出   switch   ++   

1、輸入以及輸出

  當通過new Scanner(System.in)建立一個Scanner,控制台會一直等待輸入,直到敲斷行符號鍵結束,把所輸入的內容傳給Scanner,作為掃描對象。如果要擷取輸入的內容,則只需要調用Scanner的nextLine()方法即可。代碼如下:

 

Scanner input = new Scanner(System.in);
System.out.println("提示:輸入");
int a = input.nextInt();

2、基本語句

if語句:if(布林運算式){

//如果布林運算式為ture,就執行的語句

}

例子:

Scanner input = new Scanner(System.in);
System.out.println("輸入一個數");
int a = input.nextInt();
if (a > 10) {
System.out.println(a);
}
if else語句:if(布林運算式){

//當布林運算式為ture時執行

}else{

//當布林運算式偉false時執行

}

例子:

Scanner input = new Scanner(System.in);
System.out.println("輸入一個數");
int a = input.nextInt();
if (a > 10) {
System.out.println(a);
}else{
System.out.println(a);
}

switch語句:

switch (變數) {
case 值:

break;
case 值:

break;

default:
break;
}

for迴圈://已知迴圈次數

for(初始化;布林運算式;更新){

//語句

}

例子:9*9法則

for (int a = 1; a <= 9; a++) {
for (int b = 1; b <= a; b++) {
int c = a * b;
System.out.print(b+ "*" + a+ "=" + c+" ");
}
System.out.println();
}

while迴圈://未知迴圈次數

while(布林運算式){

//語句

}

例子:

int a=1;
while(a<=10){
System.out.print(a+" ");
a++;
}

break語句://終止當前迴圈

例子:

int k=1;
while(k<=10){
System.out.print(k+" ");
if(k==6){
break;
}
k++;
}

continue語句://退出當前迴圈,但是不影響後邊的迴圈

例子:

System.out.print("for迴圈");
System.out.println();
for (int i = 10; i > 0; i--) {
if (i % 2 == 0) {
continue;
}System.out.print(i+" ");
}

 

JAVA 入門編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.