C4Java 基础-02:程序逻辑

1.

GloryofKings.java

package com.csdn.c4;
import com.csdn.c4.Player;
public class GloryofKings {
        
    public static void main(String [] args){
        Player A = new Player();
        A.setName("playerA");
        A.setScore(1760);
        
        Player B = new Player();
        B.setName("playerB");
        B.setScore(9527);
        
        Player C = new Player();
        C.setName("playerC");
        C.setScore(16800);

        change(A);
        change(B);
        change(C);
        
        
    }
    
    
    public static void change(Player X) {
        int a =X.getScore();
        if(a>=0 && a<=100){
            System.out.println(X.name+":倔强青铜");
        }
        if(a>=101 && a<=500){
            System.out.println(X.name+":秩序白银");
        }
        if(a>=501 && a<=2000){
            System.out.println(X.name+":荣耀黄金");
        }
        if(a>=2001 && a<=5000){
            System.out.println(X.name+":尊贵铂金");
        }
        if(a>=5001 && a<=10000){
            System.out.println(X.name+":永恒钻石");
        }
        if(a>=10001 && a<=20000){
            System.out.println(X.name+":至尊黑曜");
        }
        if(a>20000){
            System.out.println(X.name+":最强王者");

        }
    }
}

Player.java

package com.csdn.c4;

public class Player {
    
    String name;
    int score;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }

}
 

2.

int y = 0;
do {
y++;
} while (y < 0);
System.out.println("y = " + y);

先+再判断,y = 1

3.用多种循环结构计算并打印出100 的阶乘(for、while、do...while)

直接拷贝了网上的。。。

package com.csdn.c4;

        public class jiec {
             
            public void forf(){
                double sum = 1;
                for (int i = 1; i < 100; i++) {
                    sum = sum*i;
                }
                System.out.println(sum);
            }
             
            public void whiles(){
                double sum = 1;
                int i=1;
                while(i<100){
                    sum = sum*i;
                    i++;
                }
                System.out.println(sum);
            }
             
            public void dowhile(){
                double sum =1;
                int i=1;
                do{
                    sum = sum*i;
                    i++;
                }while(i<100);
                 
                System.out.println(sum);
            }
         
            public static void main(String[] args) {
             
                jiec j = new jiec();
                j.forf();
                j.whiles();
                j.dowhile();
            }
         
    }

4.递归

        1.斐波那契数列之和(N = 50)

指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,斐波纳契数列以如下被以递推的方法定义:F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)(n>=3,n∈N*)。

package com.csdn.c4;

public class Fibonacci {
    
    
    public static void main(String [] args){
        
        int n=6;
        int a;
        a=FibonacciSum(n);
        System.out.println(a);
    }
    
    /*
     * 求第n项的值
     */
    public static int Fibonacci(int n) {

        if(n < 0){
            return 0;
        }
        if(n == 0){
            return 0;
        }
        if(n == 1){
            return 1;
        }
        return Fibonacci(n-1)+ Fibonacci(n-2);

    }
    
    /*
     * 求前n项的和
     */
    public static int FibonacciSum(int n) {
        int sum = 0;
        for(int i=1;i<=n;i++) {
            sum += Fibonacci(i);  //累加前n项
        }
        return sum;
    }
}
 

其他题目省略

5.回调实现下面这两种常见的生活场景

张伟在超市买了一瓶快乐水和一些吃的,使用支付APP 完成支付后,过了几秒,APP
通知他已经支付成功

ICallBack.java

public interface ICallBack {
    
    public void run();
}
 

Program.java

public class Program {
    
    public static void main(String[] args) throws InterruptedException{
        
        //创建控制器对象,将提供给它的回调对象传入
         Controller obj = new Controller(new CallBackClass());
         //启动控制器对象运行
         obj.Begin();
         }
    }

CallBackClass.java

public class CallBackClass implements ICallBack{
    
    public void run(){
        
        //支付成功
        System.out.println("支付成功" );
    }
 }

Controller.java

import java.util.Scanner;

public class Controller {
    
    public ICallBack CallBackObject = null;// 引用回调对象
    
    Scanner input = new Scanner(System.in); //读取命令行输入
    
    public Controller(ICallBack obj){
        this.CallBackObject = obj;
    }
    
    public void Begin() throws InterruptedException{
        System.out.println("请付款" );
        while(input.next() != null){
            Thread.sleep(3000);
            CallBackObject.run();
        }
    }

}

李雷周一在某某宝APP 上买了两张周五晚上的电影票。在周五晚上电影即将放映的前
一小时,APP 给他发来了观影通知

思路同上

6.第五章 控制流

基本就是指,if-else,while,do-while,for,return,break,switch。

除了switch,所有判断都关注true和false。switch可以根据表达式的值,去判断具体的分支。有点像是sql中的when...case...。

单向执行:if-else

循环语句:while,do-while,for

传统for循环和for-in循环:传统for循环需要创建int变量控制循环次数,for-in循环即直接遍历对象中的所有元素。

returnbreakcontinue 和跳转到带标签语句的方法,类似于其他语言中的 goto。

return,跳出当前方法,在方法返回类型非 void 的情况下,会返回一个值。

break跳出当前循环体,continue表示停止本次循环,直接开始下一次循环。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
*{ margin: 0; padding: 0; } html,body{ height: 100%; } body{ width: 100%; background-image:radial-gradient(#22002a,#01001f) } a{ text-decoration: none; } #movie_info{ width: 990px; margin: 0 auto; padding-bottom: 60px; box-sizing: border-box; } #movie_info .info{ width: 100%; padding-bottom: 15px; border-bottom: 1px dotted rgb(255,255,255,0.3); } #movie_info .info img{ float: left; box-shadow: 0 0 6px #C4C4C4; margin-top: 36px; } table{ margin-top: 20px; margin-right: 10px; float: right; color: white; font-size: 13px; } table,tr,td,th{ border: none; } th{ font-size: 22px; text-align: left; height: 38px; line-height: 38px; color: #fdfed2; } td{ width: 240px; line-height: 24px; } table a{ display: block; width: 100px; height: 30px; line-height: 30px; border-radius: 5px; color: #FFFFFF; text-align: center; margin-top: 15px; } table .lookInfo{ background-image:linear-gradient(#9ac534,#427e22) ; } table .buy{ background-image:linear-gradient(#ff8b45,#ff5200) ; } table .score{ color: #f2a32e; } table .score .star{ margin-left: 15px; display: inline-block; width: 103px; height: 20px; background: url(../images/star_big.png) left -80px no-repeat; } table .score em{ font-size: 28px; color: #f2a32e; } #movie_info .info:after,#movie_info ul:after{ content: ""; display: block; clear: both; } /*猜你喜欢*/ #movie_info .like{ position: relative; } #movie_info .like span{ position: absolute; top: 10px; left: 25px; display: block; width: 100px; height: 30px; line-height: 30px; border-radius: 2px; background:rgb(220,216,216, 32%); color: #FFFFFF; text-align: center; font-size: 12px; } #movie_info .like ul{ width: 990px; height: 190px; position: absolute; top: 50px; left: 25px; overflow: hidden; } #movie_info .like ul:after{ content: ""; display: block; clear: both; } #movie_info .like li{ width: 119px; text-align: center; float: left; margin-right: 20px; font-size: 12px; color: #e1ab5b; line-height: 20px; cursor: pointer; } #movie_info .like li img{ width: 119px; height: 168px; }。每行代码加上注释
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值