流程控制
1.1顺序结构:无特定的语法结构,按照代码的先后顺序依次执行
1.2分支结构(if语句)
if语句的格式1:
if(关系表达式){
语句体;
}
/* 需求:1)a与b值是否相同控制台返回a=b,
2)a与c的值是否相同,相同返回a=c
*/
public class test{
public static void main(String[] args){
System.out.println("程序开始运行了");
int a=10;
int b=20;
if(a==b){
System.out.println("a与b相等");
}
int c=10;
if(a==c){
System.out.println("a与c的值相同");
}
System.out.println("ENDING");
}
}
if语句格式2:
if(关系表达式2){
语句体1
}else{
语句体2
}
/*需求:判断a是否大于b,如果是,在控制台输出a>b
,反之输出a不大于b*/
public class test{
public static void main(String[] args){
int a=10;
int b=20;
if(a>b){
System.out.println("a>b");
}else{
System.out.println("a不大于b");
}
}
}
/*需求:任意给出一个整数,判断程序是奇数还是偶数,并在控制台输出该数是奇数还是偶数*/
import java.util.Scanner;
public class test2{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
if(number%2==0){
System.out.println(number+"是偶数");
}else{
System.out.println(number+"是奇数");
}
}
}
if语句格式3:
if(关系表达式1){
语句体1;
}else if{
语句体2;
}……
else{
语句体n+1;
}
//需求:键盘录入一个星期数(1,2,……,7)输出对应的星期
import java.util.Scanner;
public class test{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个星期数(1~7):");
int number=sc.nextInt();
if(number==1){
System.out.prinln("今天是星期一");
}else if(number==2){
System.out.println("今天是星期二");
}else if(number==3){
System.out.println("今天是星期三");
}else if(number==4){
System.out.println("今天是星期四");
}else if(number==5){
System.out.println("今天是星期五");
}else if(number==6){
System.out.println("今天是星期六");
}else{
System.out.println("今天星期日");
}
}
}
/*需求分析:小明考试结束后,爸爸会根据小明考试分数给予不同的奖励:95~100分山地自行车;90~94分游乐园一日游;80~89分大黄蜂;80分以下竹竿炒肉*/
import java.util.Scanner;
public class test{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("小明期末考了");
int sorce=sc.nextInt();
if(sorce<0||sorce>100) {
System.out.println("输入数字有误");
}else if(sorce>=95&sorce<=100){
System.out.println("爸爸给小明奖励一辆山地自行车");
}else if(sorce>=90&sorce<=94){
System.out.println("爸爸带小明去游乐园");
}else if(sorce>=80&sorce<=89){
System.out.println("爸爸给小明买了一个大黄蜂");
}else{
System.out.println("小明被竹竿炒肉");
}
}
}
//测试数据一定要测试正确数字,边界数字和错误数字
switch语句:
switch(表达式){
case 值1:
语句体1;
break;//结束语句
case 值2:
语句体2;
break;
……
default://所有情况不匹配的时候,执行该语句的内容
语句n+1;
[break;]//可选的
}
//需求:键盘录入一个星期数(1,2,……,7)输出对应的星期
import java.util.Scanner;
public class test{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个星期数(1~7):");
int week=sc.nextInt();
switch(week){
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;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("输入的星期数有误");
break;
}
}
}
/*案例:春夏秋冬
一年有12个月,分属于春夏秋冬4个季节,键盘录入一个月份,用程序判断该月份属于那个季节,并输出
ps:春:3,4,5; 夏:6,7,8
秋:9,10,11; 冬:1,2,12
*/
public class jijie {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入月份:");
int week=scanner.nextInt();
switch (week) {
case 1:
case 2:
case 12:
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;
}
}
}
1.3循环结构
for循环
for(初始化语句;条件判断语句;条件控制语句){
循环体语句;
}
//例子
//输出5次helloworld
public class test{
//需求1:首先输出1~5,在输出5~1
//需求2:计算1~5之和
public static void main(String[] args) {
//test1();
//test2();
//test3();
test4();
}
static void test1(){
for(int i=0;i<=5;i++) {
System.out.println(i);
}
System.out.println("-----------");
for(int j=5;j>=0;j--) {
System.out.println(j);
}
}
static void test2() {
int sum=0;
for(int i=0;i<=5;i++) {
sum+=i;
}
System.out.println("1~5之间的数据是:"+sum);
}
static void test3() {
//需求3:求偶数和246810
int sum=0;
for(int i=1;i<=1000;i++) {
if(i%2==0) {
sum+=i;
}
}
System.out.println("1~1000之间的和为:"+sum);
}
static void test4() {
//水仙花数
int d,k,j,sum=0;
for(int i=100;i<1000;i++) {
d=i%10;
k=(i/10)%10;
j=i/100;
if(Math.pow(d, 3)+Math.pow(k, 3)+Math.pow(j, 3)==i) {
sum+=i;
System.out.println(i);
}
}
System.out.println("100~1000之间的水仙花数之和为:"+sum);
}
}
while循环
//基本格式
while(条件判断语句){
循环体结构;
}
----------------------------------------
public class test{
public static void main(String[] args){
// test1();
test2();
}
static void test1(){
//需求:输出5次helloworld
int i=1;
while(i<=5){
System.out.println("HelloWorld");
i++;
}
}
static void test2(){
//珠穆朗玛峰(8844.43米=8844430毫米),若我有一张足够巨大的纸,厚度为0.1毫米,折多少次能够折成珠穆朗玛峰的高度?
double paper=0.1;
int height=8844430;
int count=0;//折叠次数
while(paper<height){
paper*=2;
count++;
}
System.out.println("折叠"+count+"次就可以达到珠穆朗玛峰的高度");
}
}
do…while循环
初始化语句;---->(1)
do{
循环语句;---->(2)
条件控制语句;---->(4)
}while(条件判断语句);---->(3)
----------------------------------------
public class test{
public staic void main(String[] args){
}
static void test1(){
//使用do…while语句输出5次helloworld
int j=1;
do{
System.out.println("helloworld");
j++;
}while(j<=5);
}
}
三种循环的区别:
死循环的三种格式:
1) for(;;){}
2) while(true){}
3) do{} while(true); //命令提示符窗口中ctrl+c可以结束死循环。
区别:for循环和while循环先判断条件是否成立,然后决定是否执行循环体(先判断后执行);do…while循环是先执行一次循环体,然后判断条件是否成立,是否继续执行循环体(先执行后判断);
for和while的区别:条件控制语句控制的自增变量,因为所属for循环的语法结构中,在for循环结束后,就不能再次被访问到了;条件控制语句所控制的自增变量,对于while循环来说不归属其语法结构中,在while循环结束后,该变量还可以继续使用。
控制跳转语句
- continue:用于循环中,基于条件控制,跳过本次循环体内的执行,继续下一次的执行。
- break:用于循环中,基于条件控制,终止循环体内容的执行,也就是结束当前的整个循环。
循环嵌套
public class test{
public static void main(String[] args){
/*在控制台输出一天的小时和分钟*/
for(int i=0;i<=12;i++) {
for(int m=0;m<=60;m++) {
System.out.println(i+"时"+m+"分");
}
System.out.println("--------------");
}
}
}
Random(随机数)
//Random的使用步骤
import java.util.Random;//1.导包
public class test{
public static void mian(String[] args){
Random r=new Random();//创建对象
int number=r.nextInt(10);//获取随机数,范围是在[0,10)之间
int number2=r.nextInt(100)+1;//获取随机数,随机数的范围0~100
}
}
---------------------------------
/*猜数字:程序自动生成一个1~100之间的数字,猜数字是多少,会根据不同的情况给出相应的提示*/
import java.util.Random;
import java.util.Scanner;
public class guessNumber {
/*猜数字:
* 程序自动生成一个1~100之间的数字,
* 猜数字是多少,程序会根据不同的情况给出相应的提示*/
import java.util.Random;
import java.util.Scanner;
public static void main(String[] args) {
Random random=new Random();
int r=random.nextInt(100)+1;
while(true) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入您猜的数字:");
int s=scanner.nextInt();
if(r>s) {
System.out.println("您输入的数字太小了,请重新输入");
}else if(r<s) {
System.out.println("您输入的数字太大了,请重新输入");
}else {
System.out.println("恭喜您成功猜到正确的数字!");
break;
}
}
}
}