Java大佬是怎样炼成的3-4

三、选择

3.1 Boolean数据类型

  • 声明一个值是true或值是false的变量
  • 六种关系操作符
Java操作符名称
<小于
<=小于等于
>大于
>=大于等于
==等于
!=不等于
  • 相等的关系符是==而不是=
  • true 和 false 都是直接量,不能用作程序中的标识符

3.2 If语句

  • if 语句是一个结构,允许程序确定执行的路径。
  • 单分支 if 语句
Created with Raphaël 2.2.0 开始 布尔表达式? 语句 结束 yes no
  • 双分支 if-else 语句
Created with Raphaël 2.2.0 开始 布尔表达式? 语句(组) 结束 语句(组) yes no

if (//布尔表达式> {
//当布尔表达式为true时执行
}else{
//布尔表达式为false时执行
}

  • 可以使用else if(){}来进行多重判断
Created with Raphaël 2.2.0 开始 布尔表达式? 语句(组) 结束 布尔表达式2? 语句(组) …… 语句(组) 语句(组) yes no yes no yes no
import java.util.Scanner;
public class Work3_3{
	/*
	找到将来的日期
	提示用户输入代表今天日期的数字(星期日为0...)
	提示用户输入一个今天之后的天数
	如果今天%7==0
		if(今天+天数%7==0)
		 输出今天星期日未来星期日
		else if(今天+天数%7==1)
			输出今天星期日未来星期一
		...
	else if(今天%7==1)
		if(今天+天数%7==0)
		 输出今天星期一未来星期一
		else if(今天+天数%7==1)
			输出今天星期一未来星期二
		...
	...
	*/
	public static void show(int today,int future,String outToday){
		if(today+future%7==0){
				System.out.println("Today is "+outToday+" and the future day is Sunday");
			}else if(today+future%7==1){
				System.out.println("Today is "+outToday+" and the future day is Monday");
			}else if(today+future%7==2){
				System.out.println("Today is "+outToday+" and the future day is Tuesday");
			}else if(today+future%7==3){
				System.out.println("Today is "+outToday+" and the future day is Wednesday");
			}else if(today+future%7==4){
				System.out.println("Today is "+outToday+" and the future day is Thursday");
			}else if(today+future%7==5){
				System.out.println("Today is "+outToday+" and the future day is Friday");
			}else if(today+future%7==6){
				System.out.println("Today is "+outToday+" and the future day is Saturday");
			}
	}
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter today's day:");
		int today=scanner.nextInt();
		System.out.print("Enter the number of days elapsed since today:");
		int future=scanner.nextInt();
		if(today%7==0){
			String outToday="Sunday";
			show(today,future,"Sunday");
		}else if(today%7==0){
			show(today,future,"Monday");
			
		}else if(today%7==2){
			show(today,future,"Tuesday");
		}else if(today%7==3){
			show(today,future,"Wednesday");
			
		}else if(today%7==4){
			show(today,future,"Thursday");
			
		}else if(today%7==5){
			show(today,future,"Friday");
			
		}else if(today%7==6){
			show(today,future,"Saturday");
		}
	}
}
  • 可以使用嵌套的 if 语句和多分支 if-else 语句
if(x>1){
	y=1;
}else{
	if(z<0){
		y=-1
	}
}

3.3常见错误

  • if ( boolean 值)
    如果花括号内只有一条语句,则可以省略花括号,容易产生 错误,建议都加上{}
    -在 if 行出现错误的分号
 if(boolean值){;
   //语句
   }
  • 对布尔值的冗余测试
    例如:
    if(flag==true){ //flag的值本来就是Boolean类型 }
  • 悬空 else 出现的歧义
 if(){
  		if(){
  		}
  else{//这个else实际是第一个if的否定
  }
  }
  • 两个浮点数值的相等测试,浮点数具有有限的计算精度,计算可能引人取整错误

3.3 switch 语句

  • switch 语句基于变量或者表达式的值来执行语句
switch (status) {//status的值是byte、short、int、char、String、枚举类型的
		case n: //这里的n是常量
		//语句;
		break;
		default: 
		//如果前面都没执行,执行default
		break;
 }
  • 默认情况(default)是可选的
  • 关键字 break 是可选的
  • 不要忘记在需要的时候使用 break 语句

3.4条件表达式

if(x>0){
	y=1;
}else{
	y=-1;
}

也可以使用

int y=x>0?1:-1;

例子:

System.out.println((num%2==0) ? "num is even" : "num is odd"):

3.5代码实现

import java.util.Scanner;
public class Work3_4{
	/*
	判断回文数
	提示用户输入一个三位数
	将数字拆分
	反序重新组成
	判断是否相等
	如果相等
		输出它是回文数
	否则
	  输出不是回文数
	*/
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a three-digit integer:");
		int num=scanner.nextInt();
		int orig=num;
		int a=num%10;//个位
		num/=10;
		int b=num%10;//十位
		num/=10;
		int c=num%10;//百位
		int number=a*100+b*10+c;
		if(orig==number){
			System.out.println(orig+" is a palindrome");
		}else{
			System.out.println(orig+" is not a palindrome");
		}
	}
}
import java.util.Scanner;
public class Test{
	/*
	随机生成一个两位数随机数
	让用户输入一个两位数
	数字%10获得个位数
	数字/10获得十位数
	判断个位数是否相等
	判断十位数是否相等
	如果两个按顺序相等,输出奖金10000
	判断输入的两个数字只要相等,奖金为3000
	如果有一个数字相等,获得奖金1000
	*/
	public static void main(String[] args){
		int random=(int)(Math.random()*100);
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter 0~99 of num:");
		int num=scanner.nextInt();
		if(num<100){
			int range=random%10;
			int ranshi=random/10;
			int numge=num%10;
			int numshi=num/10;
			if(range==numge&&ranshi==numshi){
				System.out.println("you get 10000 yuan");
			}else if((range==numshi)&&(ranshi==numge)){
				System.out.println("you get 3000 yuan");
			}else if((range==numge||range==numshi)||(ranshi==numge||ranshi==numshi)){
				System.out.println("you get 1000 yuan");
			}else{
			  	System.out.println("you get 0 yuan");
			}
	  }
	  System.out.println("中奖号码是:"+random);
  }
}
import java.util.Scanner;
public class Work3_6{
/*
一个剪刀石头布的游戏
0代表剪刀,1代表石头,2代表布
电脑随机产生数字0,1,2
提示用户随机输入0,1,2
如果随机数字==输入数字,平局
如果随机(数字加1)%3==输入数字,用户赢
否则,电脑赢
*/
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		int random=(int)(Math.random()*3);
		System.out.print("scissor(0),rock(1),paper(2):");
		int user=scanner.nextInt();
		String ranStr=null;
		String userStr=null;
		if(random==0){
			ranStr="scissor";
		}else if(random==1){
			ranStr="rock";
		}else{
			ranStr="paper";
		}
		if(user==0){
			userStr="scissor";
		}else if(user==1){
			userStr="rock";
		}else{
			userStr="paper";
		}
		if(random==user){
			System.out.printf("The computer is %s.you is %s.It is a draw",ranStr,userStr);
		}else if(((random+1)%3)==user){
			System.out.printf("The computer is %s.you is %s.you win",ranStr,userStr);
		}else{
			System.out.printf("The computer is %s.you is %s.you lose",ranStr,userStr);
		}
	}
}
import java.util.Scanner;
public class Work3_10{
/*
判断点是否在三角形(0,0)(200,0)(0,100)内
提示用户输入坐标点x,y
判断
	如果x<200&&y<(-1/2x+100),在三角形内
	否则,在三角形外
*/
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a point's x and y coordinates:");
		double x=scanner.nextDouble();
		double y=scanner.nextDouble();
		if(x>0&&x<200&&y<((-1.0/2)*x+100)){
			System.out.println("The point is in the triangle");
		}else{
			System.out.println("The point is not in the triangle");
		}
	}
}

四、循环

  • 循环可以用于让一个程序重复地执行语句
  • Java 提供了三种类型的循环语句:while 循环、do-while 循环和 for 循环。
  • 在循环控制中,不要使用浮点值来比较值是否相等,在较大数之前先增加较小数是减小误差的一种方法。
  • 一个常见的程序设计错误是无限循环(也就是说,循环会永远执行下去)。
  • 程序员经常会犯的错误就是使循环多执行一次或少执行一次。
/*
从大到小添加数字没有从小到大添加数字得到的值精确。这种现象是有限精度算术的产物。
*/
//从大到小累加,结果是:50.49999999999995
double currentValue = 1.0;
for (int count = 0;count < 100;count++){
	sum +=currentValue;
	currentValue-= 0.01;
}
//从小到大累加,结果是:50.50000000000003
double currentValue = 0.01;
for (int count= 0;count < 100;count++){
	sum += currentValue;
	currentValue +=0.01;
}

4.1 for循环

Created with Raphaël 2.2.0 开始 初始化 循环条件? 循环体 迭代之后的动作 结束 yes no
for (初始操作;循环鏈续条件;每次迭代后的操作){
		// 循环体;
		语句(组);
}
  • 初始动作、循环继续条件和每次迭代后的动作都要用分号分隔。
  • for 循环使用一个变量来控制循环体的执行次数
  • 控制变量必须在循环控制结构体内或循环前说明
for(int i=0;i<10;i++){
	//循环体
}
//或者
int i=0;
for(i=1;i<10;i++){
	//循环体
}
  • for 循环中的初始动作可以是 0 个或是多个以逗号隔开的变量声明语句或賦值表达式。
for(int i=0,int j=1;i<10;i++){
	//循环体
}
  • 如果省略 for 循环中的循环继续条件,則隐含地认为循环继续条件为 true。
for(  ;  ;  ){
	//这里将无限循环
}

4.2 while循环

  • while 循环在条件为真的情况下,重复地执行语句。
Created with Raphaël 2.2.0 开始 布尔表达式? 语句(组) 结束 yes no
  • 只有当循环体只包含一条语句或不包含语句时,循环体的花括号才可以省略。(建议别省略)
  • 要保证循环继续条件最终可以跳出循环,以便程序能够结束。

4.2.1 循环设计步骤

  • 第一步:循环初始化
  • 第二步:循环继续条件
  • 第三步:确定需要循环的语句(循环体)
  • 第三步:循环迭代动作
while( 布尔表达式){//为false时跳出循环
			// 循环体
			语句 ( 组 );
}

4.2.2 使用标记值来结束循环

  • 在读取和处理一个集合的值时指派一个特殊值,用以表明循环的结束。
  • 下面这个程序,当输入为0时循环结束
import java.util.Scanner;
public class Work3_22{
/*
最大数的出现次数
数据:最大数max  次数count
提示用户输入数字,以0结束
循环:
		读取用户输入的数字num
		如果num>max,则max=num,count=1
		如果num==max,则count++
		如果num==0,break
	输出结果

*/
	public static void main(String[] args){
		int max=0;
		int count=0;
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter numbers:");
		while(true){
			int num=scanner.nextInt();
			if(num>max){
				max=num;
				count=1;
			}else if(num==max){
				count++;
			}else if(num==0){
			 	break;
			}
		}
		System.out.println("The largest number is:"+max);
		System.out.println("The occurence count of the largest number is:"+count);
		
	}
}

4.2.3 输入和输出重定向

如果要输人大量的数值,那么从键盘上输入是非常繁琐的事。可以将
这些数据用空格隔开,保存在一个名为 input.txt 的文本文件中,然后使用下面的命令运行这
个程序:

java Work3_22 < input.txt

在这里插入图片描述
在这里插入图片描述

4.3 do-while循环

  • do-while 循环是 while 循环的变体
  • while 循环与 do-while 循环的差别在于:计算循环继续条件和执行循环体的先后顺序不同。
  • 如果循环中的语句至少需要执行一次,建议使用 do-while 循环。
do {
	// 循环体 ;
	语句(组 );
} while ( 循环继续条件);
Created with Raphaël 2.2.0 开始 语句(组) 布尔表达式? 结束 yes no

4.4 嵌套循环

  • 一个循环可以嵌套在另外一个循环中
  • 每当重复执行一次外层循环时将再次进人内部循环,然后重新开始。
    例如:打印金字塔,就需要用嵌套循环来建立一个二维模型
public class Work3_17{
/*
打印金字塔的数字(8行)
                  1
               1  2  1
            1  2  4  2  1
         1  2  4  8  4  2  1
    第四行:
    				首先确定x轴的值:-3 -2 -1 0  1  2  3
    				确定y轴的值:2^(||x|-3|)      
*/
	public static void main(String[] args){
			//行
			for(int i=0;i<8;i++){
				//空格
				for(int k=8-i;k>0;k--){
					System.out.print("    ");
				}
				//数字
				for(int j=-i;j<=i;j++){
					System.out.printf("%4d",(int)Math.pow(2,Math.abs(Math.abs(j)-i)));
				}
				System.out.println();
			}
	}
}

4.5 关 键 字 break 和 continue

  • 关键字 break 和 continue 在循环中提供了額外的控制
  • 使用 break 和 continue 可以简化程序设计。但是,过度使用或者不正确地使用它们会使得程序难以读懂也难以调试
  • continue 只是跳出了一次迭代,而关键字 break是跳出了整个循环。
  • 在 while 和 do-while 循环中,continue 语句之后会马上计算循环继续条件;而在 for 循环中,continue 语句之后会立即先执行每次迭代后的动作,再计算循环继续条件
  • 总是可以编写在循环中不使用 break 和 continue 的程序
  • 很多程序设计语言都有 goto 语句。goto 语句可以随意地将控制转移到程序中的任意一条语句上,然后执行它。这使程序很容易出错。Java 中的 break 语句和 continue 语句是不同于 goto 语句的。它们只能运行在循环中或者 switch 语句中。break 语句跳出整个循环,而 continue 语句跳出循环的当前迭代

当用户或者电脑赢了两局时,使用break跳出循环

import java.util.Scanner;
public class Work3_21{
/*
玩、剪刀(0)、石头(1)、布(2),直到用户或者电脑赢了两局为止
用户赢得局数:userCount
电脑赢的局数:comCount
循环:提示用户输入数字(user)
			电脑生成随机数(com)
			比较两个的值
				如果user=com,平局
				如果(user+1)%3==com,电脑赢comCount++
							否则,用户赢userCount++
			如果(comCount==2||userCount==2)
			    break
	输出结果
*/
	public static void main(String[] args){
		int comCount=0;
		int userCount=0;
		Scanner scanner=new Scanner(System.in);
		while(true){
			System.out.print("Enter your number:");
			int user=scanner.nextInt();
			int com=(int)(Math.random()*3);
			if(user==com){
				System.out.println("这把平局!");
			}else if((user+1%3)==com){
				System.out.println("这把电脑赢!");
				comCount++;
			}else{
				System.out.println("这把用户赢!");
				userCount++;
			}
			if(comCount==2||userCount==2){
				break;
			}
		}
		if(userCount==2){
			System.out.println("恭喜,你赢了");		
		}else{
					System.out.println("谢谢参与");
		}

	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值