生活随笔
收集整理的這篇文章主要介紹了
编码练习——Java-2-流程控制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
條件
public class Test {
public static void main (String[] args) {
int a =
100 ;
if (a==
100 ) {System.
out .println(
"a的值是 100" );}}
}
public class Test {
public static void main (String[] args) {
int x =
45 ;
int y =
12 ;
if (x>y) {System.
out .println(
"變量x 大于 變量y" );}
if (x<y) {System.
out .println(
"變量x 小于 變量y" );}}
}
public class Test {
public static void main (String[] args) {
int math =
95 ;
if (math>
60 ) {System.
out .println(
"數學及格了" );}
else {System.
out .println(
"數學沒有及格" );}}
}
public class Test {public static void main (String[] args) {
int x =
20 ;
if (x>
30 ) {System.out.println(
"a的值大于30" );}
else if (x>
10 ) {System.out.println(
"a的值大于10,但小于30" );}
else if (x>
0 ) {System.out.println(
"a的值大于0,但小于10" );}
else {System.out.println(
"a的值小于0" );}}
}
public class Test {
public static void main (String[] args) {System.
out .println(
"今天是星期幾:" );
int week =
2 ;
switch (week) {
case 1 :System.
out .println(
"Monday" );
break ;
case 2 :System.
out .println(
"Tuesday" );
break ;
case 3 :System.
out .println(
"Wednesday" );
break ;
default :System.
out .println(
"Sorry, I don't know" );}}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import java.util.Scanner;
public class Test {
public static void main (String[] args) {Scanner scan =
new Scanner(System.
in );System.
out .println(
"請輸入登錄用戶名:" );String username = scan.nextLine();System.
out .println(
"請輸入登錄密碼:" );String password = scan.nextLine();
if (!username.equals(
"mr" )) {System.
out .println(
"用戶名非法" );}
else if (!password.equals(
"mrsoft" )) {System.
out .println(
"登錄密碼錯誤" );}
else {System.
out .println(
"恭喜您, 登錄信息通過驗證" );}}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import java.util.Scanner;
public class Test {
public static void main (String[] args) {Scanner scan =
new Scanner(System.
in );System.
out .println(
"請輸入新員工的姓名:" );String name = scan.nextLine();System.
out .println(
"請輸入新員工應聘的編程語言:" );String language = scan.nextLine();
switch (language.hashCode()) {
case 3254818 :
case 2301506 :
case 2269730 :System.
out .println(
"員工" + name +
"被分配到Java程序開發部門。" );
break ;
case 3104 :
case 2112 :System.
out .println(
"員工" + name +
"被分配到C#項目維護組。" );
break ;
case -
709190099 :
case 955463181 :
case 9745901 :System.
out .println(
"員工" +name+
"被分配到Asp.net程序測試部門。" );
break ;
default :System.
out .println(
"本公司不需要" + language +
"語言的程序開發人員。" );}}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
循環
public class Test {
public static void main (String[] args) {
int x =
1 ;
int sum =
0 ;
while (x<=
10 ) {sum = sum+x;x++;}System.
out .println(
"從1到10的和是:" + sum);}
}
public class Test {public static void main (String[] args) {
int a =
100 ;
while (a==
60 ) { System.out.println(
"Ok! a==60" );a--;}
int b =
100 ;do { System.out.println(
"Ok! b==60" );b--;}
while (b==
60 );}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class Test {public static void main (String[] args) {
int sum =
0 ;
for (
int i=
1 ; i<=
100 ; i+=
1 ) {sum = sum+i;}System.out.println(
"1到100之間數的和為:" + sum);}
}
public class Test {public static void main (String[] args) {
int arr[] = {
7 ,
10 ,
1 };System.out.println(
"一維數組中的元素分別為:" );
for (
int x : arr) {System.out.print(x+
"\t" );}}
}
public class Test {public static void main (String[] args) {String[] aves =
new String[] {
"白露" ,
"丹頂鶴" ,
"黃鸝" ,
"烏鴉" ,
"喜鵲" ,
"布谷鳥" ,
"灰文鳥" ,
"百靈鳥" };
int index =
0 ;System.out.println(
"我的花園里有很多鳥,種類有:" );
while (index < aves.length) {System.out.print(aves[index++] +
" " );}}
}
public class Test {public static void main (String[] args) {
for (
int i=
1 ; i<=
9 ; i++) {
for (
int j=
1 ; j<=i; j++) {System.out.print(j +
"*" + i +
"=" + (i*j) +
"\t" );}System.out.println();}}
}
public class Test {public static void main (String[] args) {
int sum =
0 ;String flag =
"從1到100之間連續整數的和是:" ;
for (
int i=
1 ; i<=
100 ; i++) {sum +=i;
if (sum>
1000 ) {flag =
"從1到" + i +
"之間連續整數的和是:" ;
break ;}}System.out.println(flag + sum);}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class Test {public static void main (String[] args) {
int i =
0 ;System.out.println(
"10以內的奇數有:" );
while (i<
10 ) {i++;
if (i%
2 ==
0 ) {
continue ;}System.out.print(i +
" " );}}
}
public class Test {public static void main (String[] args) {System.out.println(
"\n----中斷單層循環的例子----" );String[] array =
new String[] {
"白露" ,
"丹頂鶴" ,
"黃鸝" ,
"鸚鵡" ,
"烏鴉" ,
"喜鵲" ,
"老鷹" ,
"布谷鳥" ,
"老鷹" ,
"灰文鳥" ,
"老鷹" ,
"百靈鳥" };
for (String string : array) {
if (string.equalsIgnoreCase(
"老鷹" ))
break ;System.out.print(
"有:" + string +
" " );}System.out.println(
"\n\n----中單2層循環的例子----" );
int [][] myScores =
new int [][] {{
67 ,
78 ,
63 ,
22 ,
66 }, {
55 ,
68 ,
78 ,
95 ,
44 },{
95 ,
97 ,
92 ,
93 ,
81 }};System.out.println(
"寶寶這次考試成績:\n數學\t語文\t英語\t美術\t歷史" );No1:
for (
int [] is : myScores) {
for (
int i : is) {System.out.print(i +
"\t" );
if (i<
60 ) {System.out.println(
"\n等等," + i +
"分的是什么?這個為什么不及格?" );
break No1;}}System.out.println();}}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
public class Test {public static void main (String[] args) {String[] array =
new String[] {
"白露" ,
"丹頂鶴" ,
"黃鸝" ,
"鸚鵡" ,
"烏鴉" ,
"喜鵲" ,
"老鷹" ,
"布谷鳥" ,
"老鷹" ,
"灰文鳥" ,
"老鷹" ,
"百靈鳥" };System.out.println(
"在我的花園里有很多鳥類,但是最近來了幾只老鷹,請幫我把他們抓走。" );
int eagleCount =
0 ;
for (String string : array) {
if (string.equals(
"老鷹" )) {System.out.println(
"發現一只老鷹,已經抓到籠子里。" );eagleCount++;
continue ;}System.out.println(
"搜索鳥類,發現了:" + string);}System.out.println(
"一共捉到了:" + eagleCount +
"只老鷹。" );}
}
public class Test {public static void main (String[] args) {printHollowRhombus(
10 );}
public static void printHollowRhombus (
int size) {
if (size%
2 ==
0 ) size++;
for (
int i=
0 ; i<size/
2 +
1 ; i++) {
for (
int j=size/
2 ; j>i+
1 ; j--) {System.out.print(
" " );}
for (
int j=
0 ; j<
2 *i+
1 ; j++) {
if (j==
0 || j==
2 *i) System.out.print(
"*" );
else System.out.print(
" " );}System.out.println(
"" );}
for (
int i=size/
2 +
1 ; i<size; i++) {
for (
int j=
0 ; j<i-size/
2 ; j++) {System.out.print(
" " );}
for (
int j=
0 ; j<
2 *size-
1 -
2 *i; j++) {
if (j==
0 || j==
2 *(size-i-
1 ))System.out.print(
"*" );
else System.out.print(
" " );}System.out.println(
"" );}}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
public class Test {public static void main (String[] args) {
int triangle[][] =
new int [
8 ][];
for (
int i=
0 ; i<triangle.length; i++) {triangle[i] =
new int [i+
1 ];
for (
int j=
0 ; j<=triangle[i].length-
1 ; j++) {
if (i==
0 ||j==
0 ||j==triangle[i].length-
1 )triangle[i][j] =
1 ;
else triangle[i][j] = triangle[i-
1 ][j] + triangle[i-
1 ][j-
1 ];System.out.print(triangle[i][j] +
"\t" );}System.out.println();}}
}
from: http://blog.csdn.net/xuezhisdc/article/details/52185229
總結
以上是生活随笔 為你收集整理的编码练习——Java-2-流程控制 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。