/**
程序分三种:
顺序、分支、循环结构
分支(选择):如果
当我们程序需要选择性(需要判断)执行的时候使用分支
if(条件判断){
//满足条件后的代码
}else{
//否则执行
}
<60 :打
>=60&&<80: 买糖吃
>=80&&<100: 买游戏机
>=100&&<120: 送女朋友
*/
public class IfDemo
{
public static void main(String [] args){
int count = -12;
if(count>0&&count<60){
System.out.println("男女混合双打....");
}else if(count>=60&&count<80){
System.out.println("买糖吃...");
}else if(count>=80&&count<100){
System.out.println("买游戏机...");
}else if(count>=100&&count<120){
System.out.println("送女朋友...");
}else{
System.out.println("分数统计有误");
}
}
}
/**
嵌套的方式:
比如:
运动会 >=6 进入决赛 再以性别分为男子组 和 女子组
*/
import java.util.Scanner;
public class IfElseDemo
{
public static void main(String [] args){
Scanner input = new Scanner (System.in);
System.out.println("请输入你的比赛成绩:");
int score = input.nextInt();
if(score >=6){
System.out.println("请输入你的性别:");
char gender = input.next().charAt(0);
if(gender=='男'){
System.out.println("男子组...");
}else if(gender=='女'){
System.out.println("女子组...");
}else{
System.out.println("性别识别错误");
}
}else{
System.out.println("抱歉,你已被淘汰...");
}
}
}
/**
Switch:
等值判断
break:
跳过本次判断结构,如果没有他,编译出问题
*/
import java.util.Scanner;
public class SwitchDemo
{
public static void main(String [] args){
Scanner input = new Scanner (System.in);
System.out.println("今天健身第几天哇~");
int day = input.nextInt();
switch(day){
case 1:
System.out.println("黄瓜...");
break;
case 2:
System.out.println("鸡胸肉...");
break;
case 3:
System.out.println("鸡蛋...");
break;
case 7:
System.out.println("你有没有乖乖做训练啊?(1、有/2、没有)");
int result = input.nextInt();
if(result==1){
System.out.println("鸡腿..");
}else{
System.out.println("喝凉水吧~");
}
}
}
}
/**
while(先判断,再执行)
当同样的行为不停的重复或重复很多遍时用到循环
循环结构由四个部分组成的:
【1】初始化
【2】条件判断
【3】循环体
【4】迭代(更新)
*/
public class WhileDemo
{
public static void main(String [] args){
int i = 1;
while(i<=100){
System.out.println("好好学习,天天向上..."+i);
i++;
}
}
}
/**
Do-while(先执行,在判断)
书写流程:
【1】初始化
【2】循环体
【3】迭代
【4】条件判断
*/
public class DoWhileDemo
{
public static void main(String [] args){
int i = 1;
do{
System.out.println("小吕哇~");
i++;
}while(i<=10);
}
}
/**
for:
*/
public class ForDemo
{
public static void main(String [] args){
for(int i = 1;i<=10;i++){
System.out.println("小仙女真好看~~~~");
}
}
}
/**
for的嵌套:
外循环走一遍 内循环走一轮
外循环:
控制行数
内循环:
列数
*/
public class ForDemo02
{
public static void main(String [] args){
int rows = 5;
for(int i =1;i<=rows;i++){
for(int j = 1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
/**
BreakAndContinue:
作用:
break; 终止整个循环,程序跳转到循环块外的下一条语句
Continue: 跳过本次循环中剩余语句而执行下-次循环
使用场合:
break: 可用于switch结构中和循环结构中
Continue:只能使用在循环中
*/
public class BreakAndContinue
{
public static void main(String [] args){
for(int i = 1;i<=10;i++){
if (i==5)
{
System.out.println("现在是第"+i+"圈了。不用跑了");
break;
}
System.out.println("现在是第"+i+"圈了。");
}
System.out.println("=================================================");
for(int i = 1;i<=10;i++){
if (i==5)
{
System.out.println("现在是第"+i+"圈了。喝口水...");
continue;
}
System.out.println("现在是第"+i+"圈了。");
}
}
}
/**
方法:
方法是定义一个类中的一个小程序,用于完成独立的功能
定义方式:
public static 返回值类型 方法名(数据类型 参数1,数据类型 参数2....){
方法的主体
程序语句
[return 表达式;]
}
定义的方法的两个明确:
【1】明确方法运行之后是否有结果,如果有,写结果类型,如果没有 写void
【2】方法在运行的时候,是否不确定因素,不确定因素作为方法的形式参数
方法的调用:
【1】方法的调用处负责提供方法的实际参数
【2】负责对方法的运算结果进行处理
注意事项:
【1】方法与方法之间是平级的关系,不能嵌套
【2】方法的运算结果叫作返回值,返回值的结果只有一个
【3】方法的实际参数与方法的形式参数的类型,个数,顺序完全一致
*/
public class MethodDemo
{
public static void main(String [] args){
int s = MethodDemo.reduce(100,22);
System.out.println(s);
add(433,32);
}
public static int reduce(int a,int b){
int result =a-b;
return result;
}
public static void add(int a ,int b ){
System.out.println(a+b);
}
}