Java基础第四天
1.1分支结构2:switch语句
1.根据变量的值,选择相应的case去判断,一旦满足case条件,就执行case的相应语句。如果没有break或者已经 到结尾的话,会继续执行其下的case语句。 2.default:是可选的,而且位置是灵活的。 3.变量可以是哪些类型?char byte short int 枚举 String(jdk1.7) 4.case 条件:其中条件只能是值,不能是取值范围!
switch语句应用举例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 35 36 | import java.util.Scanner;
class TestSwitch
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println( "请输入季节:" );
String season = s.next();
switch (season){
case "Spring" :
System.out.println( "春天" );
break ;
case "Summer" :
System.out.println( "夏天" );
break ;
case "Autum" :
System.out.println( "秋天" );
break ;
case "Winter" :
System.out.println( "冬天" );
break ;
default :
System.out.println( "输入有误" );
break ;
}
}
}
|
练习: 根据用于指定月份,打印该月份所属的季节。 3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
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 | import java.util.Scanner;
class TestSwitch2
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println( "请输入月份:" );
int mounth = s.nextInt();
if (mounth > 12 || mounth < 1 ){
System.out.println( "输入有误!" );
} else {
switch (mounth/ 3 ){
case 1 :
System.out.println( "春季" );
break ;
case 2 :
System.out.println( "夏季" );
break ;
case 3 :
System.out.println( "秋季" );
break ;
default :
System.out.println( "冬季" );
break ;
}
}
}
}
|
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 35 36 37 38 39 40 41 42 | import java.util.Scanner;
class TestSwitch1
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println( "请输入月份:" );
int mounth = s.nextInt();
switch (mounth){
case 12 :
case 1 :
case 2 :
System.out.println( "冬季!" );
break ;
case 3 :
case 4 :
case 5 :
System.out.println( "春季!" );
break ;
case 6 :
case 7 :
case 8 :
System.out.println( "夏季!" );
break ;
case 9 :
case 10 :
case 11 :
System.out.println( "秋季!" );
break ;
default :
System.out.println( "输入有误!" );
break ;
}
}
}
|
编写程序:从键盘上输入2016年的“month”和“day”,要求通过程序输出输入的日期为2014年的第几天。 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 35 36 37 38 39 40 41 42 43 44 45 | import java.util.Scanner;
class TestSwitch3
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println( "请输入月份:" );
int mounth = s.nextInt();
System.out.println( "请输入日期" );
int day = s.nextInt();
int sum = 0 ;
switch (mounth){
case 12 :
sum += 30 ;
case 11 :
sum += 31 ;
case 10 :
sum += 30 ;
case 9 :
sum += 31 ;
case 8 :
sum += 31 ;
case 7 :
sum += 30 ;
case 6 :
sum += 31 ;
case 5 :
sum += 30 ;
case 4 :
sum += 31 ;
case 3 :
sum += 28 ;
case 2 :
sum += 31 ;
case 1 :
sum += day;
}
System.out.println(sum);
}
}
|
1.2循环结构
循环语句功能 在某些条件满足的情况下,反复执行特定代码的功能 循环语句的四个组成部分 初始化部分(init_statement) 循环条件部分(test_exp) 循环体部分(body_statement) 迭代部分(alter_statement) 循环语句分类 for 循环 while 循环 do/while 循环
for 循环
案例:
1 2 3 4 5 6 7 8 9 | class TestFor
{
public static void main(String[] args)
{
for ( int i = 0 ; i < 100 ;i++){
System.out.println( "Hello World!" + i);
}
}
}
|
题目:输出100以内的所有偶数及所有偶数的和(累加的思想)及偶数的个数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class TestFor1
{
public static void main(String[] args)
{
int sum = 0 ;
int count = 0 ;
for ( int i = 0 ; i <= 100 ; i++){
if (i % 2 == 0 ){
System.out.println(i);
sum += i;
count++;
}
}
System.out.println(sum);
System.out.println(count);
}
}
|
小测试: 编写程序FooBizBaz.java,从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出“foo”,在每个5的倍数行上打印“biz”,在每个7的倍数行上打印输出“baz”。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class FooBizBaz
{
public static void main(String[] args)
{
for ( int i = 1 ;i <= 150 ;i++){
System.out.print(i + " " );
if (i % 3 == 0 ){
System.out.print( "foo " );
}
if (i % 5 == 0 ){
System.out.print( "biz " );
}
if (i % 7 == 0 ){
System.out.print( "baz " );
}
System.out.println(); //换行
}
}
}
|
练习:输出所有的水仙花数,所谓水仙花数是指一个3 位数,其各个位上数字立方和等于其本身。 例如: 153 = 1*1*1 + 3*3*3 + 5*5*5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class TestFor2
{
public static void main(String[] args)
{
for ( int i = 100 ; i < 1000 ; i++){
int i1 = i / 100 ;
int i2 = (i - i1* 100 )/ 10 ;
int i3 = i - i1* 100 - i2* 10 ; //int i3 = i % 10;
if (i == i1*i1*i1 + i2*i2*i2 + i3*i3*i3 ){
System.out.println(i);
}
}
}
}
|
While循环
语法格式 [初始化语句] while( 布尔值测试表达式){ 语句或语句块; [更改语句;] } While案例:100以内的偶数的输出,及其和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class TestWhile{
public static void main(String[] args)
{
int i = 1 ;
int sum = 0 ;
while (i <= 100 ){
if (i % 2 == 0 ){
System.out.println(i);
sum += i;
}
i++;
}
System.out.println(sum);
System.out.println(i);
}
}
|
do-while 循环语句
语法格式 [初始化语句] do{ 语句或语句块; [更改语句;] }while(布尔值测试表达式); 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 | class TestDoWhile
{
public static void main(String[] args) {
int i = 1 ;
do {
if (i % 2 == 0 ){
System.out.println(i);
}
i++;
} while (i <= 100 );
//do-while与while的区别
int j = 10 ;
do {
System.out.println(j);
j++;
} while (j < 10 );
while (j < 10 ){
System.out.println(j);
j++;
}
}
}
|
案例:从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。 补充: 最简单无限循环格式:while(true) , for(;;),无限循环存在的原因是并不知道循环多少次,需要根据某些条件,来控制循环。 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 35 36 37 38 39 40 41 42 43 44 | import java.util.Scanner;
class TestWhileTure
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
Scanner s = new Scanner(System.in);
int a = 0 ;
int b = 0 ;
/*for(;;){
System.out.println("请输入一个整数");
int num = s.nextInt();
if(num > 0){
a++;
}else if(num < 0){
b++;
}else{
break;
}
}
*/
while ( true ){
System.out.println( "请输入一个整数" );
int num = s.nextInt();
if (num > 0 ){
a++;
} else if (num < 0 ){
b++;
} else {
break ;
}
}
System.out.println( "正数有" +a+ "个" );
System.out.println( "负数有" +b+ "个" );
}
}
|
1.3嵌套循环将一个循环放在另一个循环体内,就形成了嵌套循环。其中,for ,while ,do…while均可以作为外层循环和内层循环。
案例:
/* ***** ***** ***** ***** */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class TestForFor
{
public static void main(String[] args)
{
/* for ( int i = 0 ; i < 4 ; i++){ //行循环
for ( int j = 0 ; j < 5 ; j++){ //列循环
System.out.print( "*" );
}
System.out.println();
}
}
}
|
/* * ** *** **** */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class TestForFor
{
public static void main(String[] args)
{
/* for ( int i = 0 ; i < 4 ; i++){ //行循环
for ( int j = 0 ; j < i+ 1 ; j++){ //列循环
System.out.print( "*" );
}
System.out.println();
}
}
}
|
/* ***** **** *** ** * */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class TestForFor
{
public static void main(String[] args)
{
/* for ( int i = 0 ; i < 5 ; i++){ //行循环
for ( int j = 0 ; j < 5 -i ; j++){ //列循环
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 | class TestForFor
{
public static void main(String[] args)
{
for ( int i = 0 ;i < 5 ; i++){
for ( int j = 0 ;j < i+ 1 ; j++){
System.out.print( "*" );
}
System.out.println();
}
for ( int i = 0 ; i < 4 ; i++){
for ( int j = 0 ; j < 4 -i; j++){
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 | class TestForFor {
public static void main(String[] args)
{
for ( int i = 0 ;i < 4 ;i++){
for ( int k = 0 ;k < 4 -i;k++){
System.out.print( " " );
}
for ( int j = 0 ;j < i+ 1 ;j++){
System.out.print( "* " );
}
System.out.println();
}
for ( int i = 0 ;i < 5 ;i++){
for ( int k = 0 ;k < i;k++){
System.out.print( " " );
}
for ( int j = 0 ;j < 5 -i; j++){
System.out.print( "* " );
}
System.out.println();
}
}
}
|
打印九九乘法表:
1 2 3 4 5 6 7 8 9 10 11 12 13 | class TestJiuJiu
{
public static void main(String[] args)
{
for ( int i = 1 ;i < 10 ;i++){
for ( int j = 1 ;j < i+ 1 ;j++){
System.out.print(i+ "*" +j+ "=" +i*j+ "\t" );
}
System.out.println();
}
}
}
|
1.4特殊流程控制语句1 break 语句 break语句用于终止某个语句块的执行 { …… break; …… }
continue 语句 continue语句用于跳过某个循环语句块的一次执行 continue语句出现在多层嵌套的循环语句体中时,可以通过标签指明要跳过的是哪一层循环 break只能用于switch语句和循环语句中。 continue 只能用于循环语句中。 二者功能类似,但continue是终止本次循环,break是终止本层循环。 break、continue之后不能有其他的语句,因为程序永远不会执行其后的语句。 标号语句必须紧接在循环的头部。标号语句不能用在非循环语句的前面。
|