全都没有问题(二)

java复习敲代码,基础基础基础

输出有格式的图样

java 复制代码
package com.book;

public class Test2 {
    public static void main(String args[]){
        String s="*  ";
        for(int i=1;i<=5;i++) {
            for (int j = 1; j <= i; j++)
                System.out.print(s);
            System.out.println();
        }

    }
}

Math库的pow,PI

java 复制代码
package com.book;

public class Test2 {
    public static void main(String args[]){
        double r=5.5;
        double s,c;
        s=Math.PI*Math.pow(r,2);
        c=2*Math.PI*r;
        System.out.println("r= "+r+",s= "+s+",c= "+c);
     



    }
}

Scanner类,从键盘输入数据

printf格式化字符串

java 复制代码
package com.book;

import java.util.Scanner;
import java.lang.Math;

class Chapter1Game {
    public static void main(String args[]) {
        int years;
        double rate, sum, payBackMonth, payBackTotal;
        System.out.println("月利率,总金额,年数:");
        Scanner sc = new Scanner(System.in);
        rate = sc.nextDouble();
        sum = sc.nextDouble();
        years = sc.nextInt();

        payBackMonth = sum * rate / (1 - 1 / Math.pow(1 + rate, years * 12));
        payBackTotal = 12 * payBackMonth * years;
        System.out.printf("payBackMonth:%.2f%n",payBackMonth);
        System.out.printf("payBackTotal:%.2f%n",payBackTotal);
    }//main
}

import static

switch break

Math.random()

java 复制代码
package com.book;

import static java.lang.Math.*;
import static java.lang.System.*;
import java.util.Scanner;
public class Test2 {
    public static void main(String args[]){
        out.println("2:石头 1:剪刀 0:布 ,输入你的选择");
        Scanner sc=new Scanner(in);
        int user=sc.nextInt();
        int computer = (int)(3*random());
        switch(user){
            case 0:
                switch(computer){
                    case 0:
                        out.println("computer 0 equal");
                        break;
                    case 1:
                        out.println("computer 1 computer win");
                        break;
                    case 2:
                        out.println("computer 2 user win");
                }
                break;
            case 1:
                switch(computer){
                    case 0:
                        out.println("computer 0 user win");
                        break;
                    case 1:
                        out.println("computer 1 equal");
                        break;
                    case 2:
                        out.println("computer 2 computer win");
                }
                break;
            case 2:
                switch(computer){
                    case 0:
                        out.println("computer 0 computer win");
                        break;
                    case 1:
                        out.println("computer 1 user win");
                        break;
                    case 2:
                        out.println("computer 2 equal");
                }
        }
    }
}

i%j==0

java 复制代码
package com.book;

public class Test2 {
    public static void main(String args[]){
        for(int i=1;i<=1000;i++){
            int sum=0;
            for(int j=1;j<i;j++){       //找因子
                if(i%j==0)
                    sum+=j;
            }
            if(sum==i)
                System.out.print(i+ " ");
        }
    }

}

内部类,面向对象,字符串,基本数据类型转换

java 复制代码
package com.book;

public class Test2 {
    class Stock{        //内部类
        private String symbol,name;
        private double previousPrice,currentPrice;
        public Stock(String symbol,String name){
            this.symbol=symbol;
            this.name=name;
        }
        public void setPreviousPrice(double previousPrice){
            this.previousPrice=previousPrice;
        }
        public void setCurrentPrice(double currentPrice) {
            this.currentPrice = currentPrice;
        }
        public String getChangePercent(){
            double change=(currentPrice-previousPrice)/previousPrice;
            int little=(int)(change*100*100)%100;        //15
            int number=(int)(change*100);         //12
            return number+"."+ little+"%";   //12.15%
        }
    }//Stock
    public static void main(String args[]){
        Test2.Stock s=new Test2().new Stock("浦发银行","600000");
        s.setPreviousPrice(25.5);
        s.setCurrentPrice(28.6);
        System.out.println(s.getChangePercent());
        int a=(int)(2.6);
        System.out.println(a);  //整除直接舍小数
    }//main
}//Test
相关推荐
JH307329 分钟前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
Coder_Boy_2 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
invicinble2 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟2 小时前
使用ASM和agent监控属性变化
java
黎雁·泠崖2 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
qq_12498707534 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_4 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.4 小时前
Day06——权限认证-项目集成
java
瑶山4 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard
abluckyboy4 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法