全都没有问题(二)

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
相关推荐
程序员谷美几秒前
Redis 性能优化:利用 MGET 和 Pipeline 提升效率
java·redis·性能优化
Heavydrink16 分钟前
JSP内置对象、Servlet与MVC
java·servlet·mvc
Lucky_Turtle29 分钟前
【SpringSecurity】二、自定义页面前后端分离
java
雨 子30 分钟前
SpringBoot环境和Maven配置
java·spring boot·后端·java-ee·maven
zyplanke30 分钟前
Spring配置文件中:密码明文改为密文处理方式(通用方法)
java·后端·spring
暮湫33 分钟前
集合源码的常见问题
java
计算机毕设指导634 分钟前
基于Springboot的景区民宿预约系统【附源码】
java·开发语言·spring boot·后端·mysql·spring·intellij idea
计算机毕设指导637 分钟前
基于Springboot美食推荐商城系统【附源码】
java·前端·spring boot·后端·spring·tomcat·美食
web1508509664137 分钟前
程序包org.springframework.boot不存在
java·spring boot·spring
zhangxueyi43 分钟前
MySQL之企业面试题:InnoDB存储引擎组成部分、作用
java·数据库·mysql·面试·innodb