文章目录
1.多分支
1.1 多重 If
任务:
语法:
流程图:
现场编程:
注意:
1.if分支只有一个;
2.else if分支可以有多个;
3.else 可以选择;
4. if,else if,else 只带一行代码 可以省略 大括号。
代码:
int score;
System .out.println("输入成绩:");
score = superman.nextInt();
if (score >= 90 ){// 98
System .out.println("优秀");
}else if (score >= 80){// score >= 80 && score < 90
System .out.println("良好");
}else if (score >= 60 ){
System .out.println("中等");
}else{
System .out.println("差");
}
1.2 switch分支
语法:
执行流程:
现场编程I:
输入1-3,分别对应不同人的号码
Scanner superman = new Scanner(System.in);
int no;
System .out.println("输入编号:");
no = superman.nextInt();
switch (no){
//byte ,short ,int ,char ,String ,enum枚举
case 1:
System .out.println("师傅的号");
break;//结束switch
case 2:
System .out.println("黄老邪的号");
break;
case 3:
System .out.println("蓉蓉的号");
break;
default :
System .out.println("输入错误");
//break;
}
注意:
1.表达式的类型 与 标签的字面值类型必须相符;
2.要求类型:byte,short,int ,char,String,枚举enum
3.switch中的标签不能重复;
4.break的作用 结束switch;
5.只能等值判断。
6.支持 标签堆叠。
现场编程 II:
输入1-12,输出对应的季节:
Scanner superman = new Scanner(System.in);
int month;
System .out.println("输入月份:");
month = superman.nextInt();
switch (month){
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;
case 12:
case 1:
case 2:
System .out.println("冬天");
break;
default:
System .out.println("输入错误");
}
多重if 和 switch区别:
1.多重if 可以进行范围 判断 也可以 等值 判断;
switch 只能进行等值的判断;
2.都是等值的判断 ,值固定 ,那么使用 switch效率更高。
2.分支嵌套
现场编程:
输入运动员的成绩,进行分组。
int ss;
String sex;
System .out.println("输入速度:");
ss = superman.nextInt();
if (ss <= 50){
//可以进入决赛
System .out.println("输入性别:");
sex = superman.next();
if (sex.equals("男")){
System .out.println("男子组");
}else if(sex.equals("女")){
System .out.println("女子组");
}
}else{
System .out.println("不能进入决赛");
}
3.循环结构
循环的特点:重复的事情
循环的条件
循环的操作
3.1 循环分类
固定次数循环:
循环次数固定。
for
非固定次数:
循环次数不固定。
while :先条件判断
do -while: 先执行,后条件判断
3.2 while循环
语法:
流程图:
现场编程:输出 5 遍好好学习
//1.---------------------------
int i = 1;// 初始值
// 7 <= 5 -> false
while (i <= 5){ // 终止值(条件)
System .out.println("好好学习");
//i = i + 1;
i ++; // 迭代(循环变量变化的值)
}
System .out.println(i);//6
//2.----------------------------------
int i = 1;
while (i <= 5){
System .out.println(i);//变量
i ++;
}
//3.-----------------------------------
int i = 1;
int sum = 0;//和
while (i <= 5){
//sum = sum + i;
sum += i;
i ++;
}
System .out.println(sum);
总结:
1.非固定次数循环;循环条件必须是boolean数据类型;
2.先条件判断,后执行循环操作;
首次判断的条件为false,循环操作一次都不执行。
现场编程:
一直循环录入成绩:
import java.util.Scanner;
public class Demo12 {
public static void main(String[] args) {
//成绩,-1结束----------------------
Scanner superman = new Scanner(System.in);
int score = 0;
while (score != -1){
System .out.println("输入成绩;");
score = superman.nextInt();
}
//姓名,q 结束-----------------------
String name = "y";
while (!name.equals("q")){// !true
System .out.println("输入名字");
name = superman.next();
}
}
}
非固定次数循环: 1.循环条件;2.循环操作;
3.3 do - while 循环
语法:
流程:
特点: 先执行,后判断;
即使 首次 判断的条件为false,循环 也 执行了一次。
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
Scanner superman = new Scanner(System.in);
int score ;
do{
System .out.println("输入成绩");
score = superman.nextInt();
}while (score != -1);
}
}
4.for循环
语法:
现场编程:
for(初始值;终止值;迭代){
循环体;
}
详解:
for:
1.运行int i = 1;
2.运行i<=5’
3.运行System .out.println(“好好学习”);
4.运行i++;
任务:输出5遍好好学习
public class Demo16 {
public static void main(String[] args) {
//纯for完成------------------------------------
// 1 2 4
for (int i = 1; i <= 5; i++ ){
System .out.println("好好学习");//3
}
//while完成------------------------------------
int i = 1; //1
while (i <= 5){//2
System .out.println("好好学习");//3
i++;//4
}
//while的死循环------------------------------------
while (true){
}
//do while的死循环------------------------------------
do{
}while (true);
//for的死循环------------------------------------
for(; ;){
sum += i;// sum = sum + i;
}
//------------------------------------
int sum = 0;
for(int i = 1; i <= 5; i ++){
sum += i;// sum = sum + i;
}
System .out.println(sum);
System .out.println(i);
}
}
5.循环区别
6.编程:[多重if] 计算器,输入两个数字和一个运算符,进行运算
import java.util.Scanner;
/**
练习:多重if
计算器,输入两个数字和一个运算符,进行运算
*/
public class Demo2_exam {
public static void main(String[] args) {
Scanner superman = new Scanner(System.in);
int n1,n2;
char op;
System .out.println("输入两个数字:");
n1 = superman.nextInt();
n2 = superman.nextInt();
System .out.println("输入一个运算符");
op = superman.next().charAt(0);
if (op == '+'){
System .out.println(n1 + n2);
}else if(op == '-'){
System .out.println(n1 - n2);
}else if(op == '*'){
System .out.println(n1 * n2);
}else if(op == '/'){
System .out.println(n1 / n2);
}else{
System .out.println("输入不正确");
}
}
}
7.编程:[多重if] String的比较
import java.util.Scanner;
/**
练习:多重if
String的比较
*/
public class Demo3_exam {
public static void main(String[] args) {
Scanner superman = new Scanner(System.in);
int n1,n2;
String op;//String
System .out.println("输入两个数字:");
n1 = superman.nextInt();
n2 = superman.nextInt();
System .out.println("输入一个运算符");
op = superman.next();
int result = 0;//存结果 @
if (op.equals("+")){
result = n1 + n2;
}else if(op.equals("-")){
result = n1 - n2;
}else if(op.equals("*")){
result = n1 * n2;
}else if(op.equals("/")){
result = n1 / n2;
}else{
//result = 0;
System .out.println("输入不正确");
}
System .out.println(result);
}
}
8.编程:字符串比较
import java.util.Scanner;
/**
语法:字符串比较
*/
public class Demo4_exam {
public static void main(String[] args) {
Scanner superman = new Scanner(System.in);
System.out.println("--输入字符串");
String s = superman.next();
boolean r = s.equals("+");
System .out.println(r);
}
}
9.编程:[siwtch] 菜单跳转
import java.util.Scanner;
/**
练习:siwtch
菜单跳转
*/
public class Demo7_exam {
public static void main(String[] args) {
Scanner superman = new Scanner(System.in);
int menuNo;
System .out.println("\t-登录菜单-");
System .out.println("\t 1.登录系统");
System .out.println("\t 2.退出");
System .out.print("-- 输入选择:");
menuNo = superman.nextInt();
switch (menuNo){
case 1:
System .out.println("\t-主菜单-");
System .out.println("\t 1.客户信息管理");
System .out.println("\t 2.购物结算");
System .out.println("\t 3.真情回馈");
System .out.println("\t 4.注销");
break;
case 2:
System .out.println("退出程序");
System.exit(0);//结束JVM
}
}
}
10.编程:[分支嵌套] 计算年龄
import java.util.Scanner;
/**
练习:分支嵌套
计算年龄
*/
public class Demo9_exam {
public static void main(String[] args) {
Scanner superman = new Scanner(System.in);
int y,m,d;
int ny,nm,nd;
System .out.println("--输入出生年月日:");
y = superman.nextInt();
m = superman.nextInt();
d = superman.nextInt();
System .out.println("--输入现在年月日:");
ny = superman.nextInt();
nm = superman.nextInt();
nd = superman.nextInt();
int age = 0;
if( ny > y){//外层if
age = ny - y;
if (m > nm){//内层if
age --;
}else if(m == nm){
if (d > nd){
age --;
}
}
if (m > nm){
age --;
}else if(m == nm && d > nd){
age --;
}
}else{
System .out.println("年输入不正确");
}
System .out.println(age);
}
}
11.编程:[while] 5个运算编程
第六个编程的意义:
第五个编程计算5!,当我们计算100!时,会发现等于零,这时因为答案超出了范围,所以我们调用BigInteger来帮忙,从而可以得出准确数值。
import java.math.*;
/**
练习:while
*/
public class Demo11_exam {
public static void main(String[] args) {
//1. 输出1-100偶数----------------------------
int i = 1;
while (i <= 100){
if(i % 2 == 0){
System .out.println(i);
}
i ++;
}
//1.5 输出1-100偶数----------------------------
int i = 0;
while (i <= 99){
i += 2;
System.out.println(i);
}
//2. 输出能同时被 2 和3 整除的数--------------
int i = 1;
while (i <= 100){
if (i % 2 == 0 && i % 3 == 0){
System .out.println(i);
}
i ++;
}
//2.5 输出能同时被 2 和3 整除的数--------------
int i = 0;
while (i <= 95){
i += 6;
System.out.println(i);
}
//3. 输出能被3整除的数的个数---------------
int i = 1;
int guojing = 0;//计数器 count
while (i <= 100){
if (i % 3 == 0){
guojing ++;
}
i++;
}
System .out.println(guojing);
//3.5 输出能被3整除的数的个数---------------
int i = 0;
int count = 0;
while (i <= 98){
i += 3;
count++;
}
System.out.println(count);
//4. 输出100,95,90,……,5,0----------
int i = 100;
while (i >= 0){
System .out.println(i);
i -= 5;
}
//5. 计算5 的阶乘-----------
int i = 1;
int sum = 1;
while (i <= 5){
sum *= i;// sum = sum * i;
i++;
}
System .out.println(sum);
//6.了解:-------------
int i = 1;
BigInteger sum = new BigInteger("1");
while (i <= 100){
sum = sum.multiply(BigInteger.valueOf(i));
i++;
}
System .out.println(sum);//93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
}
}
12.编程:[while非固定次数] 计算小明和妈妈的年龄
import java.util.Scanner;
/**
练习:while非固定次数
计算小明和妈妈的年龄
*/
public class Demo13_exam {
public static void main(String[] args) {
int xiaoming = 12;
int mather = xiaoming + 20;
int year = 0;//计数年
while (mather != 2 * xiaoming){
mather ++;
xiaoming ++;
year ++;
}
System .out.println(year + "," + mather + "," + xiaoming);
}
}
13.编程:[do-while] 循环选择菜单项,直到输入正确结束
import java.util.Scanner;
/**
练习:do-while
循环选择菜单项,直到输入正确结束。
*/
public class Demo15_exam {
public static void main(String[] args) {
Scanner superman = new Scanner(System.in);
int menuNof;
//法1 do while----------------------------------------------
do{
System .out.println("\t-菜单-");
System .out.println("\t 1.正方形");
System .out.println("\t 2.三角形");
System .out.println("\t 3.圆形");
System .out.print("-- 输入选择:");
menuNo = superman.nextInt();
switch (menuNo){
case 1:
System .out.println("正方形");
break;
case 2:
System .out.println("三角形");
break;
case 3:
System .out.println("圆形");
break;
default :
System .out.println("输入错误");
break;
}
}
//法2 while----------------------------------------------
while(menuNo != 1 && menuNo != 2 && menuNo != 3);//55
while (!(menuNo >= 1 && menuNo <= 3));//3
//法3 类bool----------------------------------------------
Scanner input = new Scanner(System.in);
int num1;
int count = 0;
do{
System.out.println("*****菜单*****");
System.out.println("1.输出正方形;2.输出三角形;3.输出圆形");
System.out.println("请输入选择(1.2.3):");
num1 = input.nextInt();
switch (num1)
{
case 1:
System.out.println("正方形");
count = 1;
break;
case 2:
System.out.println("三角形");
count = 1;
break;
case 3:
System.out.println("圆形");
count = 1;
break;
default:
System.out.println("请输入选择(1.2.3)");
}
}while (count != 1 );
//法4 bool----------------------------------------------
//了解:
int menuNo;
boolean tag = false;//标签
do{
System .out.println("\t-菜单-");
System .out.println("\t 1.正方形");
System .out.println("\t 2.三角形");
System .out.println("\t 3.圆形");
System .out.print("-- 输入选择:");
menuNo = superman.nextInt();//1
switch (menuNo){
case 1:
System .out.println("正方形");
tag = false;
break;
case 2:
System .out.println("三角形");
tag = false;
break;
case 3:
System .out.println("圆形");
tag = false;
break;
default :
System .out.println("输入错误");
tag = true;
break;
}
}while (tag);//true
}
}
14.编程:[for] 年龄调查,计算30以上和30以下的比例
import java.util.Scanner;
/**
练习:for
年龄调查,计算30以上和30以下的比例。
*/
public class Demo17_exam {
public static void main(String[] args) {
Scanner superman = new Scanner(System.in);
int age;
int old = 0;//30以上
int young = 0;//30以下
for (int i = 1; i <= 10; i++ ){
System .out.println("输入第" +i + "位年龄");
age = superman.nextInt();
if (age >= 30){
old ++;
}else{
young ++;;
}
}
System .out.println("30以上" + old / 10.0 * 100 + "%" );
System .out.println("30以下" + young / 10.0 * 100 + "%");
}
}
15.注意
1.char类型在进行比较的时候使用’'单引号进行比较,因为基本数据类型里存的是数值,而引用数据类型(str)存的是地址,所以比较±*/也不可以用""双引号
2.比较两个str是否相等,使用equals方法
3.对于局部变量来说,没有开辟空间赋初值是不可以用的。
如:int n1 = 0;
4.case ----标签
每一个case下如果没有break那么会被穿透执行,不停止。
default放在最后的话,可以省略break。
5.switch只支持byte,short,int char,String,enum枚举,这几种类型。
6.当需要输出循环的累加结果时。把println放在循环外面,即可正常输出。
7.声明:在哪声明就只能在哪里使用。如:在循环里声明只能在循环里用,出了循环就不能用了。
8.不同种类的死循环写法:
//while的死循环------------------------------------
while (true){
}
//do while的死循环------------------------------------
do{
}while (true);
//for的死循环------------------------------------
for(; ;){
sum += i;// sum = sum + i;
}