文章目录
一、顺序结构
顺序结构语句是
java
程序默认的执行流程,按照代码的先后顺序,从上到下依次执行。
如:
public class sunxv {
public static void main(String[] args) {
System.out.println("大鹏一日同风起,扶摇直上九万里。");
System.out.println("假令风歇时下来,犹能簸却沧溟水。");
System.out.println("世人见我恒殊调,闻余大言皆冷笑。");
System.out.println("宣父犹能畏后生,丈夫未可轻年少。");
}
}
二、分支结构
1.if(判断)
if语句的第一种格式:
if(关系表达式)
{
语句体;
}
需求:定义变量记录女婿的酒量,如果女婿的酒量大于2斤,老丈人就搭理他,否则就不搭理
示范:
public class ifpanduan {
public static void main(String[] args) {
Scanner cs =new Scanner(System.in);
System.out.println("请输入女婿的酒量:");
int njiu = cs.nextInt();
//对酒量进行判断
if( njiu > 2 ) {
System.out.println("小伙子不错哦!");
}
}
}
if 语句的第二种格式
if(关系表达式){
语句体1;
}else{
语句体2;
}
需求:键盘录入一个整数,表示身上的钱。
如果大于等于100块,就是网红餐厅。
否则,就吃经济实惠的沙县小吃。
示范:
import java.util.Scanner;
public class ifdier {
public static void main(String[] args) {
Scanner cs =new Scanner(System.in);
System.out.println("请输入有多少钱:");
int mey = cs.nextInt();
if(mey >= 100) {
System.out.println("吃网红餐厅!");
}else{
System.out.println("吃沙县小吃!");
}
}
}
示范:
import java.util.Scanner;
public class iflianx {
public static void main(String[] args) {
//分析:
//1.键盘录入一个整数表示电影票的票号。
Scanner cs =new Scanner(System.in);
System.out.println("请输入票号");
int piao = cs.nextInt();
//2.判断票号是奇数还是偶数
if(piao >= 0 && piao <= 100){
if(piao %2 == 1){
System.out.println("坐左侧");
}else{
System.out.println("坐右侧");
}
}else{
System.out.println("对不起,没有搜到你的票号");
}
}
}
if 语句的第三种格式
2.switch(选择)
Switch语法格式:
switch(表达式){
case 值1:
语句体1;
break;
case 值2:
语句体2;
break;
...
default:
语句体n+1;
break;
}
运行流程图
示范:
package com.guitongwan.demo;
import java.util.Scanner;
public class shifan {
public static void main(String[] args) {
//兰州拉面,武汉热干面,北京炸酱面,陕西油泼面
//1.定义变量记录 想吃的面
String noodles = "兰州拉面";
//2.拿着这个面利用switch跟四种面条匹配
switch (noodles) {
case "兰州拉面":
System.out.println("吃兰州拉面!!!");
break;
case "武汉热干面":
System.out.println("吃武汉热干面!!!");
break;
case"北京炸酱面":
System.out.println("吃北京炸酱面!!!");
break;
case"陕西油泼面":
System.out.println("吃陕西油泼面!!!");
break;
default:
System.out.println("吃方便面!");
break;
}
}
}
switch新特性(JDK12)
public class texin {
public static void main(String[] args) {
//需求:
// 1 2 3 一 二 三
int number = 1;
switch (number){
case 1 -> {
System.out.println("一");
}
case 2 ->{
System.out.println("二");
}
case 3 ->{
System.out.println("三");
}
default -> {
System.out.println("没有这种选项");
}
}
}
}
三、循环结构
- 重复的做某些事情
- 具有明确的开始和停止标记
for循环
格式
for( 初始化语句 ; 条件判断语句 ; 条件控制语句 ){
循环体语句;
}
示范:
for(int i = 1;i <= 10;i++){
System.out.println("HelloWorld");
}
图解:
示范:
public class xunhf {
public static void main(String[] args) {
for(int i = 1 ;i <= 10 ; i++){
System.out.println("Helloworld");
}
}
}
while循环
语法格式:
初始化语句;
while(条件判断语句){
循环体语句;
条件控制语句;
}
需求:利用while循环打印 1~100
示范:
public class whDemo {
public static void main(String[] args) {
//需求:利用while循环打印 1~100
int i = 1;
while(i <= 100 ) {
System.out.println(i);
i++;
}
}
}
无限循环
示范:
while(true){
System.out.println("wx");
}
跳转控制语句(continue)
示范:
public class tioa {
public static void main(String[] args) {
//跳过某一次循环
for (int i = 1; i <= 5; i++){
if(i == 3){
//结束本次循环,继续下一次循环。
continue;
}
System.out.println("小老虎在吃第" + i + "个包子");
}
}
}
跳转控制语句(break)
示范:
public class tioa {
public static void main(String[] args) {
//跳过某一次循环
for (int i = 1; i <= 5; i++){
if(i == 3){
//结束整个循环
break;
}
System.out.println("小老虎在吃第" + i + "个包子");
}
}
}
四、猜数字游戏
生成随机数
从 0 ~ 99
//1.导包
import java.util.Random;
public class suiji {
public static void main(String[] args) {
//先获取一个随机数。
// 范围: 0 ~ 99
//2.创建对象
Random r = new Random();
//3.生成随机数
//判断技巧:
//在小括号中,书写的是生成随机数的范围
//这个范围一定是从0开始的。
//到这个数 -1 结束
for(int i = 0; i < 100;i++) {
int number = r.nextInt(100);// 0 ~ 100
System.out.println(number);
}
获得指定位随机数
从 10 ~ 99
rand.nextInt(90) + 10;
猜数字实例
需求:程序自动生成一个 1 ~ 100之间的随机数字,使用程序实现猜出这个数字是多少?
实例:
//1.导包
import java.util.Random;
import java.util.Scanner;
public class caishuzi {
public static void main(String[] args) {
//2.生成一个 1 ~ 100 随机数
Random r = new Random();
int shu = r.nextInt( 100)+1;//1 ~ 100
//3.猜这个数字是多少
Scanner sc = new Scanner(System.in);
//4.循环你需要猜的东西
while(true) {
System.out.println("请输入要输入的参数");
int nub = sc.nextInt();
if(nub > shu) {
System.out.println("猜大了");
}else if(nub < shu){
System.out.println("猜小了");
}else{
System.out.println("猜中了");
break;
}
}
}
}