目录
顺序结构
选择结构
循环结构
习题
顺序结构:是程序中最简单最基本的流程控制,没有特定的语法结构,按照代码的先后顺序,依次执行,直到整个代码全部执行完成。
public class Demo {
public static void main(String[ ] args) {
System.out.println("开始");
System.out.println("语句1");
System.out.println("语句2");
System.out.println("语句3");
System.out.println("结束");
}
}
选择结构:又称分支结构;可根据不同的条件执行不同的语句。包括if条件语句与switch多分支语句。
基本格式
if(条件表达式){
当条件为true时执行的代码;
}else{
当条件为false时执行的代码;
}
单if形式
单if形式
if(条件表达式){
当条件为true时执行的代码;
}
执行流程图
嵌套if-else形式
嵌套if-else形式
if(条件1){
条件1为true时执行的代码;
if(条件2){
条件2为true时执行的代码;
}else{
条件2为false时执行的代码;
}
}else{
条件1为false时执行的代码;
}
执行流程图
if-else-if形式
if-else-if形式
if(条件1){}else if(条件2){
}else if(条件3){
}else{
}
执行流程图
形式:
switch(变量){
case 值1:
执行语句1;
break;
case 值2:
执行语句2;
break;
case 值3:
执行语句3;
break;
........
deafult:
执行语句n;
break;
}
执行流程图
switch所要求的变量的数据类型:byte short int char String 枚举;
ps注意: 1、if 它既可以对一个单一的值进行判断,也可以对一个区间的值进行判断;但switch只能判断单一的值 。
2、case子句中的值必须是常量,且所有case子句中的值应是不同的;
3、break语句用来在执行完一个case分支后使程序跳出swich语句块;如果没有break程序会顺序执行到swich结尾;
循环结构:循环语句就是在满足一定条件的情况下反复执行某一个操作。包括while循环语句、do···while循环语句和for循环语句。
循环三要素:
形式:
for(1循环的初始化 ; 2循环的继续条件 ; 4循环的步长 ){
3循环体
}
执行流程图
举例将Hello World打印十遍则为
for(int x=1; x<=10; x++) {
System.out.println("Hello World");
}
形式:
1.循环的初始化
while(2.循环的继续条件){
3.循环体
4.循环的步长
}
拓展形式:
while(true){
1.循环初始化
2.循环的继续条件
3.循环体
4.循环的步长
}
注意:这么写while循环的话,逻辑上比较顺畅的,但是有危险,在于本身是死循环,需要在合适的时候用break关键字跳出循环
执行流程图:
举例子
int x=1;
while(x<=10) {
System.out.println("HellloWorld");
x++; }
import java.util.Scanner;
class Demo03_01{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入a,b,c:");
double a=scanner.nextDouble();
double b=scanner.nextDouble();
double c=scanner.nextDouble();
double delt=b*b-4*a*c;
if(delt>0){
double r1=(-b+Math.sqrt(delt))/(2*a);
double r2=(-b-Math.sqrt(delt))/(2*a);
System.out.printf("r1=%.2f,r2=%.2f",r1,r2);
}else if(delt==0){
double r=(-b-Math.sqrt(delt))/(2*a);
System.out.printf("r=%.2f",r);
}else{
System.out.println("无实数解!");
}
}
}
import java.util.Scanner;
public class Demo3_2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 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 criterion = a * d - b * c;
if(criterion == 0) {
System.out.println("The equation has no solution");
}else {
double x = (e * d - b * f) / (a * d - b * c);
double y = (a * f - e * c) / (a * d - b * c);
System.out.println("x is " + x + " and y is " + y);
}
}
}
import java.util.Scanner;
class Demo3_3{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("今天是周几:");
int today=scanner.nextInt();
System.out.print("未来的几天:");
int future=scanner.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(futureDay==0){
futureDayStr="周日";
}else if(futureDay==1){
futureDayStr="周一";
}else if(futureDay==2){
futureDayStr="周二";
}else if(futureDay==3){
futureDayStr="周三";
}else if(futureDay==4){
futureDayStr="周四";
}else if(futureDay==5){
futureDayStr="周五";
}else if(futureDay==6){
futureDayStr="周六";
}
System.out.println("今天是"+todayStr+",未来的日子是"+futureDayStr);
}
}
import java.util.Scanner;
class Demo3_4{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入一个数字:");
int num=scanner.nextInt();
int temp=num;
int sum=0;
sum=sum*10+num%10;
num/=10;
sum=sum*10+num%10;
num/=10;
sum=sum*10+num%10;
num/=10;
if(sum==temp){
System.out.println("是回文");
}else{
System.out.println("不是回文");
}
}
}
import java.util.*;
class Demo3_5{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int comnumber = random.nextInt(100);
System.out.print("请输入二位数字:");
int number = scanner.nextInt();
if(number>99||number<10){
System.out.println("输入错误,程序结束");
}
int numberA = number/10%10;
int numberB = number%10;
int comnumberA = comnumber/10%10;
int comnumberB = comnumber%10;
if(number==comnumber){
System.out.println("恭喜你,获得10000美元");
}else if((numberA==comnumberA&&numberB==comnumberB)||
(numberA==comnumberB&&numberB==comnumberA)){
System.out.println("恭喜你,获得3000美元");
}else if((numberA==comnumberA)||(numberA==comnumberB)||
(numberB==comnumberB)||(numberB==comnumberA)){
System.out.println("恭喜你,获得1000美元");
}else{
System.out.println("谢谢参与");
}
}
}
import java.util.*;
class Demo3_6{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入 剪刀0 石头1 布2:");
int usr=scanner.nextInt();
Random random=new Random();
int com=random.nextInt(3);
String usrStr="";
String comStr="";
switch(usr){
case 0: //if usr==0
usrStr="剪刀";
break;
case 1:// if usr==1
usrStr="石头";
break;
case 2:// if usr==2
usrStr="布";
break;
}
switch(com){
case 0:
comStr="剪刀";
break;
case 1:
comStr="石头";
break;
case 2:
comStr="布";
break;
}
if(usr==com){
System.out.printf("用户是%s,电脑是%s,平局",usrStr,comStr);
}else if(usr==0&&com==2 || usr==1&&com==0 || usr==2&&com==1){
System.out.printf("用户是%s,电脑是%s,用户赢",usrStr,comStr);
}else{
System.out.printf("用户是%s,电脑是%s,用户输",usrStr,comStr);
}
}
}
import java.util.Scanner;
class Demo3_7{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//1.先输入年份
System.out.print("请输入年份:");
int year=scanner.nextInt();
//2.输入月份 1月 2月分别用13 14代替 同时year-1
System.out.print("请输入月份:");
int month=scanner.nextInt();
//3.输入日期
System.out.print("请输入日期:");
int day=scanner.nextInt();
//4.对特殊的1月和2月做处理
if(month==1||month==2){
month+=12;
year-=1;
}
//5.套公式
int h=(day+26*(month+1)/10+year%100+year%100/4+year/100/4+5*year/100)%7;
switch(h){
case 0:
System.out.println("是周六");
break;
case 1:
System.out.println("是周日");
break;
case 2:
System.out.println("是周一");
break;
case 3:
System.out.println("是周二");
break;
case 4:
System.out.println("是周三");
break;
case 5:
System.out.println("是周四");
break;
case 6:
System.out.println("是周五");
break;
}
}
}
import java.util.Scanner;
class Demo3_8{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入坐标点:");
double x=scanner.nextDouble();
double y=scanner.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("点在圆内");
}
}
}
import java.util.Scanner;
class Demo3_9{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入一个坐标:");//1.提示用户输入一个点的坐标
double x=scanner.nextDouble();
double y=scanner.nextDouble();
if(x>=0&&x<=200&&y<=-0.5*x+100){
System.out.println("点再三角形内");
}else{
System.out.println("点再三角形外");
}
}
}
import java.util.Scanner;
class Demo03_10{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//1.先输入大矩形的中心,宽和高
System.out.print("请输入第1个矩形的信息:");
double x1=scanner.nextDouble();
double y1=scanner.nextDouble();
double w1=scanner.nextDouble();
double h1=scanner.nextDouble();
//2.再输入小矩形的中心,宽和高
System.out.print("请输入第2个矩形的信息:");
double x2=scanner.nextDouble();
double y2=scanner.nextDouble();
double w2=scanner.nextDouble();
double h2=scanner.nextDouble();
double inXMin=x1-(w1-w2)/2;
double inXMax=x1+(w1-w2)/2;
double inYMin=y1-(h1-h2)/2;
double inYMax=y1+(h1-h2)/2;
double outXMin=x1-(w1+w2)/2;
double outXMax=x1+(w1+w2)/2;
double outYMin=y1-(h1+h2)/2;
double outYMax=y1+(h1+h2)/2;
if(x2>=inXMin&&x2<=inXMax&&y2>=inYMin&&y2<=inYMax){
System.out.println("小矩形在大矩形里面!");
}else if(x2<=outXMin||x2>=outXMax||y2<=outYMin||y2>=outYMax){
System.out.println("小矩形在大矩形外面!");
}else{
System.out.println("小矩形和大矩形相交!");
}
}
}
/*
输入一个数字
再判断数字是否为0
如果为0则退出
不是则继续
*/
import java.util.Scanner;
class Demo03_11{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
double sum=0; //总和
int positives=0; //正数的个数
int negatives=0; //负数的个数
System.out.print("请输入若干数字:");
while(true){//不管三七二十一 先进来再说
int num=scanner.nextInt();
if(num!=0){
sum+=num;
if(num>0){
positives++;
}else{
negatives++;
}
}else{
break; //跳出当前循环
}
}
if(positives+negatives==0){
System.out.println("没有其他数字的输入,除了0");
}else{
System.out.println("正数的个数"+positives);
System.out.println("负数的个数"+negatives);
System.out.println("总和"+sum);
System.out.println("平均值"+sum/(positives+negatives));
}
}
}
import java.util.Scanner;
class Demo3_12{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入两个数字:");
int n1=scanner.nextInt();
int n2=scanner.nextInt();
int gcd=1;
for(int i=n1=1;i--){
if(n1%i==0&&n2%i==0){
gcd=i;
break;
}
}
System.out.println(gcd);
}
}
import java.util.Scanner;
class Demo3_13{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入一个数字:");
int num=scanner.nextInt();
while(true){
for(int i=2;i<=num;i++){
if(num%i==0){
System.out.print(i+" ");
num=num/i;
break;
}
}
if(num==1){
break;
}
}
}
}
import java.util.Scanner;
class Demo3_14{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入行数:");
int line=scanner.nextInt();
for(int i=1;i<=line;i++){
for(int k=1;k<=(line-i);k++){
if(line<10){
System.out.print(" ");
}else{
System.out.print(" ");
}
}
for(int x=-(i-1);x<=i-1;x++){
if(line<10){
System.out.printf("%-2d",Math.abs(x)+1);
}else{
System.out.printf("%-3d",Math.abs(x)+1);
}
}
System.out.println();
}
}
}
class Demo3_16{
public static void main(String[] args){
for(int i=1;i<=8;i++){
for(int k=1;k<=8-i;k++){
System.out.print(" ");
}
for(int x=-(i-1);x<=i-1;x++){
System.out.printf("%4d",(int)Math.pow(2,i-1-Math.abs(x)));
}
System.out.println();
}
}
}
class Demo3_17{
public static void main(String[] args){
int count=0; //当前素数的个数
boolean flag=true;
for(int num=2;num<=1000;num++){
for(int m=2;m<=num-1;m++){
if(num%m==0){
flag=false;
break;
}
}
if(flag){
count++;
System.out.print(num+" ");
if(count%8==0){ //8 16 24 32
System.out.println();
}
}
flag=true;
}
}
}
import java.util.Scanner;
class Demo3_18{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入一个数字:");
int imax=scanner.nextInt();
double sum=0;
double flag=1;
for(int i=1;i<=imax;i++){
//sum+=Math.pow(-1.0,i+1);/(2*i-1);
sum+=flag/(2*i-1);
flag=-flag;
}
double pi=sum*4;
System.out.println(pi);
}
}
import java.util.Scanner;
class Demo3_19{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入一个数字:");
int imax=scanner.nextInt();
double e=1;
double item=1.0;
for(int i=1;i<=imax;i++){
item=item*i; //n!=(n-1)*n;
e+=1/item;
}
System.out.println(e);
}
}
class Demo3_20{
public static void main(String[] args){
int count=0;
for(int i=101;i<=2100;i++){
if(i%4==0&&i%100!=0 || i%400==0){
count++;
System.out.print(i+" ");
if(count%10==0){
System.out.println();
}
}
}
System.out.println("\n闰年一共"+count+"个");
}
}
class Demo3_21{
public static void main(String[] args){
int sum=0;
for(int n=2;n<=10000;n++){
for(int i=1;i<=n/2;i++){
if(n%i==0){
sum+=i;
}
}
if(n==sum){
System.out.println("完全数"+n);
}
sum=0;
}
}
}
import java.util.Scanner;
class Demo3_23{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入一个数字:");
int num=scanner.nextInt();
String binStr="";
while(true){
binStr=num%2+binStr;//"1100"
num/=2;
if(num==0){
break;
}
}
System.out.println(binStr);
}
}
import java.util.Scanner;
class Demo3_24{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int num=0;
int max=0;
int count=0;
System.out.print("请输入数字:");
while(true){
num=scanner.nextInt();
if(num==0){
break;
}else if(num>max){
max=num;
count=1;
}else if(num==max){
count++;
}
}
System.out.println("max="+max+",count="+count);
}
}