今天主角流程控制语句中的选择语句
选择语句
if语句
格式1
if 结构
if 选择结构是根据条件判断之后再做处理的一种语法结构。
if 结构,表示“如果满足某种条件,就进行某种处理”。
if(条件表达式){
条件为真时执行的代码
}else{
条件为假时执行的代码
}
上图解
上例题
数据:
a,b,c,d,e,f,x,y
步骤:
- 输入a,b,c,d,e,f的值
- 判断是否符合条件
- 输出结果
解法:
套公式
import java.util.Scanner;
public class Home03_02{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入a,b,c,d,e,f的值:");
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
double d=input.nextDouble();
double e=input.nextDouble();
double f=input.nextDouble();
double delt;
delt=a*d-b*c;
if(delt==0){
System.out.println("次方程式无解。");
}else{
double x=(e*d-b*f)/delt;
double y=(a*f-e*c)/delt;
System.out.printf("x=%.2f,y=%.2f",x,y);
}
}
}
格式2
if-else 结构
if-else 语句的 if 可以定义两个操作,此时的 if-else 语句表示“如果条件正确则执行一个操作,否则执行另一个操作”。
if(布尔表达式){
如果布尔表达式的值为true
}else{
如果布尔表达式的值为false
}
上图
下面使用 if…else 双条件来实现,代码如下:
// 如果num1等于num2
if (num1 == num2) {
System.out.println("num1等于num2");
}
// 如果num1大于num2
if (num1 > num2) {
System.out.println("num1大于num2");
} else {
// 否则就是num1小于num2
System.out.println("num1小于num2");
双条件语句减少了代码的编写量,同时增强了程序的可读性。
格式3
嵌套if-else形式
if(条件表达式1){
条件1为真时执行的代码
if(条件表达式2){
条件2为真时执行的代码
}else{
条件2为假时执行的代码
}else{
条件1为真时执行的代码
}
else if形式
if(布尔表达式 1){
//如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
//如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
//如果布尔表达式 3的值为true执行代码
}else {
//如果以上布尔表达式都不为true执行代码
if 语句的主要功能是给程序提供一个分支。然而,有时候程序中仅仅多一个分支是远远不够的,甚至有时候程序的分支会很复杂,这就需要使用多分支的 if…else if 语句。
import java.util.Scanner;
public class Home02_03{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一个0-1000之间的整数:");
int number=input.nextInt();
if(number==1000)
{
System.out.println("各位数字之和为:");
System.out.print(number/1000);
}
else if(100<=number&&number<1000)
{
System.out.print("各位数字之和为:");
System.out.print(number%10+(number/10)%10+number/100);
}
else if(10<=number&&number<100)
{
System.out.print("各位数字之和为:");
System.out.print(number%10+number/10);
}
else if(0<=number&&number<10)
{
System.out.print("各位数字之和为:" );
System.out.println(number);
}
else
{
System.out.println("请重新输入:");
}
}
}
else-if 结构实际上是 if-else 结构的多层嵌套。明显的特点就是在多个分支中只执行一个语句组,而其他分支都不执行,所以这种结构可以用于有多种判断结果的分支中。
挑战习题
第三章练习
import java.util.Scanner;
public class Home03_01{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入a,b,c的值:");
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
double d=Math.pow(b, 2)-4*a*c;
if(d>0)
{
double r1=(-b+Math.pow(d,0.5))/2*a;
double r2=(-b-Math.pow(d,0.5))/2*a;
System.out.println("r1="+r1);
System.out.println("r2="+r2);
//System.out.printf("r1=%.2f",r2=%.2f,r1,r2)
}
else if(d==0)
{
double r1=(-b+Math.pow(d,0.5))/2*a;
System.out.println("r1="+r1);
}
else
{
System.out.print("此方程无实根");
}
}
}
/*
数据:今天的周几 未来的天数 未来的周几
(0+12)%7=5
(4+4)%7=1
(今天的周几+未来的天数)%7=未来的周几
步骤:
1.输入今天是周几
2.输入未来的几天
*/
import java.util.Scanner;
public class Home03_03{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("今天是周几:");
System.out.print("未来的几天:");
int today=input.nextInt();
int future=input.nextInt();
int futureDay=(today+future)%7;
String todayStr="";
String futureDayStr="";
if(today==0){
todayStr="周日";
}
else if(today==1){
todayStr="周一";
}
else if(today==2){
todayStr="周二";
}
else if(today==3){
todayStr="周三";
}
else if(today==4){
todayStr="周四";
}
else if(today==5){
todayStr="周五";
}
else if(today==6){
todayStr="周六";
}
if(future==0){
futureDayStr="周日";
}
else if(future==1){
futureDayStr="周一";
}
else if(future==2){
futureDayStr = "周二";
}
else if(future==3){
futureDayStr="周三";
}
else if(future==4){
futureDayStr="周四";
}
else if(future==5){
futureDayStr="周五";
}
else if(future==6){
futureDayStr="周六";
}
System.out.println("今天是:" + todayStr + "将来是:" + futureDayStr);
}
}
/*编写一个程序,提示用户输人一个三位的整数,然后确定它是否回文数字。当从左到右,以及从右到左都是一样的话,这个数字称为回文数
*/
import java.util.Scanner;
public class Home03_04{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一个三位的整数:");
int number=input.nextInt();
if(number<100&&number>999){
System.out.println("输入错误");
}
if(number%10==number/100){
System.out.println("这个数字是回文数字");
}
else{
System.out.println("不是回文");
}
}
}
/*程序提示用户输入一个两位整数,然 后依照下面的规则判定用户是否贏得奖金:
1 )如果用户输人的所有数匹配彩票的确切顺序,奖金是10000美元。
2) 如果用户输人的所有数匹配彩票的所有数字,奖金是3000美元。
3 )如果用户输人的其中一个数匹配彩票号码中的一个数,奖金是1000美元。
本题主要运用的是嵌套的if-else控制语句,并且要注意语句里面的布尔表达式的运用;一个赖明位数字除以10,就是取它的第一位数字,对10取余的意思就是,将将它的个位取出。
*/
import java.util.Scanner;
import java.util.Random;
public class Home03_05{
public static void main( String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一位两位数:");
int number = input.nextInt();
Random random = new Random();
int com= random.nextInt(100) + 9;
int numberGeiwei = number % 10;
int numberShiwei = number / 10;
int comGewei = com % 10;
int comShiwei = com / 10;
if(numberGeiwei==comGewei && numberGeiwei==comShiwei){
System.out.println("匹配为实际顺序,奖金为10000美元");
}
else if(numberGeiwei==comShiwei && numberShiwei==comGewei){
System.out.println("匹配为所有数字,奖金为3000美元");
}
else if(numberGeiwei==comGewei || numberGeiwei==comShiwei || numberShiwei==comGewei || numberShiwei==comShiwei){
System.out.println("匹配为一个数字,奖金为1000美元");
}
else{
System.out.println("您没有中奖。");
}
}
}
import java.util.Scanner;
public class Home03_06{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入坐标点:");
double x=input.nextDouble();
double y=input.nextDouble();
double xo=0;
double yo=0;
double radius=10;
double distance=Math.sqrt(Math.pow(x-xo, 2)+Math.pow(y-yo,2));
if(distance>radius){
System.out.println("点在圆外");
}
else if(distance==radius){
System.out.println("点在圆上");
}
else{
System.out.println("点在圆内");
}
}
}
提示输入年月日信息
本题的关键是
1月2月分别用1314代替,同时减一
对特殊的1月和2月做处理
import java.util.Scanner;
public class Home03_07{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入年份:");
System.out.println("请输入月份:");//1月2月分别用1314代替,同时减一
System.out.println("请输入几号:");
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
switch (month){ //对特殊的1月和2月做处理
case 1:month = 13;
year--;
break;
case 2:month = 14;
year--;
break;
}
//套公式
int j = Math.abs (year/100),k = year%100;
int h = (day+(int)(26*(month+1)/10)+k+(int)(k/4)+(int)(j/4)+5*j)%7;
String week = null;
switch (h){
case 2:week = "周一";
break;
case 3:week = "周二";
break;
case 4:week = "周三";
break;
case 5:week = "周四";
break;
case 6:week = "周五";
break;
case 0:week = "周六";
break;
case 1:week = "周天";
break;
}
System.out.println ("今天是"+week);
}
}
/*(游戏:剪刀、石头、布)编写可以玩流行的剪刀-石头-布游戏的程序。(剪刀可以剪布,石头可以砸剪刀,而布可以包石头。)程序提示用户随机产生一个数,这个数为 0、1或者 2, 分别表 示石头、剪刀和布。程序提示用户输入值 0、1或者 2, 然后显示一条消息,表明用户和计算机谁贏了游戏,谁输了游戏,或是打成平手
本题的话,还是用嵌套的if-else语句
*/
import java.util.Scanner;
import java.util.Random;
public class Home03_06{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请用户输入剪刀0 石头 1 布2:");
int user=input.nextInt();
Random sc=new Random();
int com=sc.nextInt(3);
String userStr="";
String comStr="";
switch(user){
case 0:
userStr="剪刀";
break;
case 1:
userStr="石头";
break;
case 2:
userStr="布";
break;
}
switch(com){
case 0:
comStr="剪刀";
break;
case 1:
comStr="石头";
break;
case 2:
comStr="布";
break;
}
if(user==com){
System.out.printf("用户是%s,电脑是%s,平局",userStr,comStr);
}
else if(user==0&&com==2||user==1&&com==0||user==2&&com==1){
System.out.printf("用户是%s,电脑是%s,用户赢",userStr,comStr);
}
else{
System.out.printf("用户是%s,电脑是%s,用户输",userStr,comStr);
}
}
}
import java.util.Scanner;
public class Home03_09{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入一个点的x坐标y坐标:");
double x=input.nextDouble();
double y=input.nextDouble();
//int x0=0,y0=0;
//int x1=200,y1=0;
//int x2=0,y2=100;
//double delt=Math.sqrt(Math.pow(x-0,2)+Math.pow(y-0,2));
if(x>0 && x<200 && y<-1/2.0*x+100){
System.out.println("这个点在三角形内。");
}
else{
System.out.println("这个点不在三角形内。");
}
}
}
import java.util.*;
class Home03_10{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个矩形的中心点坐标x,y和长,高:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double w1 = input.nextDouble();
double h1 = input.nextDouble();
System.out.print("请输入第二个矩形的中心点坐标x,y和长,高:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double w2 = input.nextDouble();
double h2 = input.nextDouble();
//计算第二个矩形中心点的范围
double inMinX = x1-(w1-w2)/2;
double inMaxX = x1+(w1-w2)/2;
double inMinY = y1-(h1-h2)/2;
double inMaxY = y1+(h1-h2)/2;
double outMinX = x1-(w1+w2)/2;
double outMaxX = x1+(w1+w2)/2;
double outMinY = y1-(h1+h2)/2;
double outMaxY = y1+(h1+h2)/2;
//判断是否在内或外或嵌套
if(x2>=inMinX&&x2<=inMaxX&&y2>=inMinY&&y2<=inMaxY){
System.out.println("在内");
}else if(x2>=outMaxX||x2<=outMinX||y2>=outMaxY||y2<=outMinY){
System.out.println("在外");
}else{
System.out.println("嵌套");
}
}
}