小柯在学JAVA之第二弹

JAVA的世界很大—在新手村的我疯狂刷经验

世界很大,生活只有代码-----为了生存

代码的世界只有三种逻辑 无他,顺序,条件,循环

任何复杂的程序逻辑 三种程序结构实现

顺序结构:自上而下,每句执行
条件结构
if结构;

if (逻辑表达式){

	语句1;

	语句2;

}

	语句3

if …else…;
循环结构
if (boolean){
​ 语句块
}

package day03;
import java.util.Scanner;

public class Cashier {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
/*        int num = 6;
        if (num % 2 == 0){
            System.out.println("七");

        }else {
            System.out.println("6");

        }*/



        System.out.println("请输入单价:");
        double untiPrice = scan.nextDouble();
        System.out.println("请输入数量:");
        double amout = scan.nextDouble();
        System.out.println("请输入金额:");
        double money = scan.nextDouble();

        //总价
        double totalPrice = untiPrice * amout;
        if (totalPrice >= 500){
            totalPrice *= 0.8;
            System.out.println("打完8折:"+totalPrice);
        }
        //找零
        else {
            System.out.println("错误");
        }
        if (money>=totalPrice){
            double change = money - totalPrice;
            System.out.println("找您"+change);

        }else {
            System.out.println("错误 还差"+((totalPrice)-(money)));

        }

        double change = money - totalPrice;
        //System.out.println("现在价为:"+totalPrice+"找零"+change);

        System.out.println("面向工资学习,加油");


    }
}

综合练习

package day04;

public class Homework {
    public static void main(String[] args) {
        //编写三个数值排序
        int a =8,b =3,c =5;
        int t;
        if (a>b){
            t =a;
            a =b;
            b =t;
        }
        if (a>c){
            t = a;
            a =c;
            c =t;
        }
        if (b>c){
            t =b;
            b =c;
            c =t;
        }
        System.out.println("a="+a);
        System.out.println("b="+b);
        System.out.println("c="+c);
        System.out.println("面向工资开发---加油");
    }
}

小练习 //避免嵌套

package day04;
import java.util.Scanner;

public class Homework {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入成绩以判断等级:");
        int score = scan.nextInt();
        if (score>=0 && score <=100){
            if (score >=90){
                System.out.println("优秀");

            }else if (score>=80 ){
                System.out.println("良好");

            }else if (score >=60 && score <70){
                System.out.println("及格");

            }else {
                System.out.println("不及格");
            }
        }else {
            System.out.println("请输入合法数字");
        }
package day04;
import java.util.Scanner;

public class Homework {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入成绩以判断等级:");
        int score = scan.nextInt();
        if (score<0 || score >=100){
            System.out.println("请输入合法数字");

        }else if (score >=90){
            System.out.println("优秀");

        }else if (score>=80 ){
            System.out.println("良好");

        }else if (score >=60 && score <70){
            System.out.println("及格");

        }else {
            System.out.println("不及格");
        }

switch 多分支结构----与break搭配

    int num =2;
        switch (num){
            case 1:
                System.out.println("111");
                break;
            case 2:
                System.out.println("222");
                break;
            case 3:
                System.out.println("333");
                break;
            case 4:
                System.out.println("444");
                break;
            default:
                System.out.println("666");
        }
循环结构
if(boolean){//执行一次
	语句块
}
while(boolean){//执行多次
	语句块
}

循环三要素:

1>循环变量的初始化

2>循环变量的条件

3>循环变量的改变

循环变量:在整个循环过程所反复改变的那个数

猜数字

package day04;
import java.util.Scanner;
public class Guessing {
    public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
     System.out.println("猜数字");
     int guess =scan.nextInt();
     int value = (int) (Math.random()*100+1);
     while (guess != value){
         if (guess ==value){
             break;
         }
         if (guess > value){
             System.out.println("大了");
         }else {
             System.out.println("小了");
         }
         System.out.println("请猜:");
         guess = scan.nextInt();

     }
     if (guess == value) {
         System.out.println("恭喜你,猜对了,离面向工资开发又近了一步");
     }else {
         System.out.println("很遗憾,下次努力");
     }
    }
}

do - while

do{//至少执行一次
	语句块
}whie()
int pwd;
do{
	System.out.print("密码")
	pwd = scanner.nextInt();
}while()

for

package day05;

public class Homework {
    public static void main(String[] args) {
        System.out.println("面向工资学习---加油");
        for (int i=1;i<=10;i++){
            System.out.println("面向工资学习---加油");
        }
    }
}

循环问题

while 当…

do while 直到

for 固定循环次数

案例1

package day05;
import java.util.Scanner;
public class Addition {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int score = 0;
        for (int i=1;i<=10;i++){//10次
            int a =(int)(Math.random()*100);
            int b =(int)(Math.random()*100);
            int result = a+b;
            System.out.println("("+i+")"+a+"+"+b+"=");
            System.out.println("算吧");
            int answer = scan.nextInt();
            if (answer == -1){
                break;
            }
            if (answer == result){

                score +=10;
                System.out.println("答对了,加油");
            }else{
                System.out.println("错了...");
            }
             }
        System.out.println("得分为:"+score);
    }
}

嵌套

多行多列 外层控制行 内层控制列

案例2

package day05;

public class MultiTable {
    public static void main(String[] args) {
        //99乘法表;

        for (int i=1;i<=9;i++){//行

           for (int j = 1;j<=i;j++){//列

               System.out.print(i+"*"+j +"="+i * j+"\t");
           }
           System.out.println();
        }
    }
}

数组

程序世界最高奖项-----图灵奖

程序 = 算法 + 数据结构

1>算法 解决问题的流程/步骤

2>数据结构:将数据安照某种特定的结构保存

​ 设计良好的数据会导致好的算法

相同数据类型的元素组成的集合

元素按线型排列

数组:

​ 是一种数据类型

​ 是相同数据类型元素的集合

​ int a;----声明一个整型变量

​ int[]b;-----声明一个整型数组变量b

int[] a = new int [5];

double[] b = new double[8];

数组的初始化:

​ int[] arr = new int[4];//0,0,0,0

​ int[] arr ={3,5,7,9};

​ int[] arr = new int[]{3,5,7,9};

案例

求数组元素的最大值

int[]arr = {23,45,78,5};

找最大值算法:

1)假设第一个元素为最大值

​ int max = arr[0];

2)遍历剩余元素,将剩余元素与max1作比较,

​ for(int i =1;i<arr.length;i++){

​ if(arr[i]>max){

​ max = arr[i]

​ }

​ }

(核心思想 同化 大吃小)

max =23 - max =45 - max =78 -

案例3

package day05;

public class Homework {//数组求最大值
    public static void main(String[] args) {
    

        int[]arr = new int[]{1,25,6,78,100,0,1,8};
        int max = arr[0];
        for (int i =1;i<arr.length;i++){
            if (arr[i]>max){
                max = arr[i];
            }
        }
        System.out.println("最大值:"+max);
    }
}

long sum =0;//求9+99+999+...+99999999
        long num =0;
        for (int i =0;i<=10;i++){//10次
            /*
            *       num =o
            * i =1  num =9	num*10+9
            * i =2  num =99
            * i =3  num =999
            * */
            num = num*10+9;
            sum +=num;
        }
        System.out.println("sum="+sum);

数组的复制

System.arryaycopy(a,1,a1,0,4);

int[] a1 = Arrays.copyOf(a,6);

import java.util.Arrays;

public class MaxOfArray {
    public static void main(String[] args) {
        int []arr = new int[10];
        int max =0;
        for (int i =0;i<arr.length;i++){
            arr[i]=(int)(Math.random()*100);
            System.out.println(arr[i]);
        }
        for (int i =0;i<arr.length;i++){
            if (max < arr[i]){
                max = arr[i];
            }
        }
        //数组扩容
        System.out.println("max="+max);

        arr = Arrays.copyOf(arr,arr.length+1);
        arr[arr.length-1] = max;
        for (int i = 0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}

数组的排序

插入排序,冒泡排序,快速排序

//JAVA: Arrays.sort(arr);

冒泡·排序

​ 特点:5个数冒四轮

​ 每一轮和第一个元素开始

​ 每一次和下一个元素比较

​ 冒出了就不参与冒泡

int[]arr = {65,26,56,7,45};
for(int i=0;i<arr.length-1;i++){//控制轮数
	for(int j=0;j<arr.length-1-;j++){//控制次数
	if(arr[j]>arr[j+1]){
	int t =arr[j];
	arr[j] =arr[j+1];
	arr[j+1] =t;
	}
	
	}
}
public class Bubbdemo {//冒泡排序!!!
    public static void main(String[] args) {
        int[] arr = new int[10];
        for (int i = 0;i<arr.length;i++){
            arr[i] = (int) (Math.random()*100);
            System.out.print(arr[i]+" ");
        }
        System.out.println("");
        for (int i =0;i<arr.length-1;i++){
            for (int j =0;j<arr.length-1-i;j++){
                if (arr[j]<arr[j+1]){
                    int t =arr[j];
                    arr[j] =arr[j+1];
                    arr[j+1] = t;
                }
            }

        }
        System.out.println("排好的数值");
        for (int i=0;i <arr.length;i++){
            System.out.print(arr[i]+" ");
        }
    }
}
方法

封装一段特定的功能

尽可能独立。一个方法只干一件事情

反复调用多次

可以减少代码重复,有利于代码维护

方法的定义:

定义方法的5个要素

修饰词:public static

返回值:

​ 无返回值void

​ 方法完成后:

​ 如果要用到某个数据,有返回值

方法名

参数列表

方法体

public static int sum(int num1,int num2){
方法体
}

public class MethodDemo {
    public static void main(String[] args) {
        say();
        sayHi("小可");
        double a = getNum();
        int b = plus(1,4);
        System.out.println(a);
        System.out.println(b);

    }
    public static int plus(int num1,int num2){//有参有返回值
        int num = num1+num2;
        return num;
    }
    public static double getNum(){
        //return ;//编译错误 return 必须跟值
        return 66.6; //结束方法的执行  返回结果
    }
    public static void sayHi(String name){//有参数无返回值
        System.out.println("大家好,我叫"+name+"!");
    }
    public static void say(){//无参无返回值
        System.out.println("hello 我叫小柯");
    }
}
方法的调用:

return ;

return 值 // 结束方法的执行 ~~~~返回结果给调用方法

方法的嵌套

方法中调用其他方法

JAVA很苦,可却是苦尽甘来
新手村很好,可还是要经历刷怪的,不然总是小白在傻白甜的
*~~

JAVA第三弹再见!

~~ *

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值