全都没有问题(二)

发布时间:2023年12月20日

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

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格式化字符串

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()

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

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+ " ");
        }
    }

}

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

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

文章来源:https://blog.csdn.net/weixin_73185295/article/details/135098830
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。