栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021404209陈聪blog

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021404209陈聪blog

文章目录

==java代码==

1.书本样例2-7,计算税收2.书本样例2-8,计算每月天数3.书本样例2-94.计算程序运行时间5.书p39两个编程题6.项目实训:基于控制台设计简易的打折与累加计算器7.Arrays类相关8.静态方法与非静态方法区别

java代码 1.书本样例2-7,计算税收

补充

创建Scanner类输入数据会出现“Resource leak: 'xxx' is never closed ”的报错
原因:数据输入扫描仪(Scanner),系统就会为它分配相应的内存空间,但是在程序结时却没有释放该内存,会造成资源浪费,从而出现警告。(百度)
解决方法,用xxx.close();释放空间即可。
声明变量后有时会出现 “The value of the local variable sb7 is not used”  的错误
原因:是因为定义的变量在代码中并未使用且无法访问。java在编译的时候会出现这样的警告(scdn)
解决方法:加上“@SuppressWarnings("unused")”即可
import java.util.Scanner;
public class Ex2_7
{
	public static void main(String [] args){
		int status = scanfStatus();
		double income = scanfIncome();
		double tax = 0;
		tax = getTax(status,income);
		System.out.println("纳税人需要交税额为" + tax +"¥");
	}
	
	public static int scanfStatus(){

		int status = 0;
		System.out.println("请输入纳税人类型:0-单身,1-已婚,2,家庭");
		Scanner inStatus = new Scanner(System.in);
		if(inStatus.hasNextInt())
			 status = inStatus.nextInt();
		inStatus.close();
		return status;
			
	}

	public static double scanfIncome(){

		double income = 0;
		System.out.println("请输入可征税收入");
		Scanner in = new Scanner(System.in);
		if(in.hasNextDouble())
			 income = in.nextDouble();
		in.close();
			return income;
	}
	public static double getTax(int status , double income){
		double tax = 0;
		if(status==0){
			if(income<=6000)
				tax = income * 0.10;
			else if(income<=27950)
				tax = 6000*0.10 + (income - 6000)*0.15;
			else if(income<=67700)
				tax = 6000*0.10 + (27950 - 6000)*0.15 + (income - 27950) *0.27;
			else if(income<=141250)
				tax = 6000*0.10 + (27950 - 6000)*0.15 + (67700 - 277950) * 0.27 + (income - 67700)*0.30;
			else 
				tax = 6000*0.10 + (27950 - 6000)*0.15 + (67700 - 277950) * 0.27 + (141250 - 67700)*0.30 + (income - 141250)*0.35;

		}
		else if(status==1){
			if(income<=12000)
				tax = income * 0.10;
			else if(income<=46700)
				tax = 12000*0.10 + (income - 6000)*0.15;
			else if(income<=112850)
				tax = 12000*0.10 + + (46700 - 12000)*0.15 + (income - 46700) *0.27;
			else if(income<=171950)
				tax = 12000*0.10 + + (46701 - 12000)*0.15  + (112850 - 46701) *0.27 + (income - 112850)*0.30;
			else 
				tax = 12000*0.10 + + (46701 - 12000)*0.15  + (112850 - 46701) *0.27 + (171950 - 112850)*0.30 + (income - 171905)*0.35;
		}
		else if(status==2){
			if(income<=10000)
				tax = income * 0.10;
			else if(income<=37450)
				tax = 10000*0.10 + (income - 10000)*0.15;
			else if(income<=96700)
				tax = 10000*0.10 + (37450 - 10000)*0.15 + (income - 37450) *0.27;
			else if(income<=156600)
				tax = 10000*0.10 + (37450 - 10000)*0.15 + (96700 - 37450) * 0.27 + (income - 96700)*0.30;
			else 
				tax = 10000*0.10 + (37450 - 10000)*0.15 + (96700 - 37450) * 0.27 + (156600 - 96700)*0.30 + (income - 156600)*0.35;
		}
		return tax;
	}
}
2.书本样例2-8,计算每月天数
import java.util.Scanner;
public class Ex2_8 {
    public static void main(String[] args) {
        int year = getYear();
        int month = getMonth();
        int numday = 0;
        numday = getDay(year,month);

        System.out.println(year + " 年" + month + "月" + "有" + numday + "天");
    }
    public static int getYear(){
        int year;
        System.out.println("输入年份");
        Scanner inYear = new Scanner(System.in);
        year = inYear.nextInt();
		inYear.close();
        return year;
    }
    public static int getMonth(){
        int month;
        System.out.println("输入月份");
        Scanner inMonth = new Scanner(System.in);
        month = inMonth.nextInt();
		inMonth.close();
        return month;
    }
  
  	public static int getDay(int year , int month){
		int day = 0;
		switch (month)
		{
			case 1: 
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:day = 31;break;
			case 4:
			case 6:
			case 9:
			case 11:day = 30;break;
			case 2:
				if(year%4==0&&year%100!=0||year%400==0)
					day = 29;
				else
					day = 28;
				break;
		}
		return day;
	}

}

3.书本样例2-9

思路

简单贪心,由题意可知商品1,单个金币获得的生命最多,所以只需保证1越多越好即可
import java.util.Scanner;
public class Ex2_9
{
	static final int FGVALUE = 20;
	static final int GSVALUE = 16;
	static final int FGLIFE = 30;
	static final int GSLIFE = 20;

	public static void main(String [] args){
		int goldcoin = getGold();
		getRes(goldcoin);
	}
	
	public static int getGold(){
		System.out.println("请输入金币数量");
		Scanner inGold = new Scanner(System.in);
		inGold.close();
		return inGold.nextInt();
		
		
	}
	
	public static void getRes(int goldcoin){
		int fgnum = 0;
		int gsnum = 0 ;
		int maxlife = 0;
		
		if(goldcoin-FGVALUE>=0)
		{
			fgnum = goldcoin/FGVALUE;
			maxlife += fgnum*FGLIFE;
			goldcoin = goldcoin - fgnum*FGVALUE;
		}
		if(goldcoin-GSVALUE>=0)
		{
			gsnum = goldcoin/GSVALUE;
			maxlife += gsnum*GSLIFE;
			goldcoin = goldcoin - gsnum*GSVALUE;
		}
		System.out.printf("购买宝物最多能增加%d你的生命力n",maxlife);
		System.out.println("购买仙草数量为"+ fgnum);
		System.out.println("购买银河锁的对数量为"+gsnum);
		
	}

}
4.计算程序运行时间
//计算程序运行时间
@SuppressWarnings("unused")
public class P35_2_5_4 {
    public static void main(String [] args){
        //毫秒时间
        System.out.println(currentTimeMillis()+"ms");

        //纳秒时间
        System.out.println(nanoTime()+"ns");
    }
    public static long currentTimeMillis(){
        long startTime = System.currentTimeMillis();

        int sum = 0;
        for(int i = 0 ; i < 100000000 ; i ++){
            sum += i;
        }
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }
    
    public static long nanoTime(){
        long startTime = System.nanoTime();
        int sum = 0;
        for(int i = 0 ; i < 100000000 ; i ++){
            sum += i;
        }
        long endTime = System.nanoTime();
        return endTime - startTime;
    }
}

5.书p39两个编程题

输出三位数的水仙花数

//输出水仙花数
public class P39 {
    public static void main(String [] args){
        narcissisticNumber();
        
    }
    public static void narcissisticNumber(){
        for(int i = 100 ; i <= 999 ; i ++){
            int unit = i % 10;
            int ten = i / 10 % 10;
            int  hundredth =i / 100;
            //调用Math类的pow方法处理三次方
            if(Math.pow(unit, 3) + Math.pow(ten, 3) + Math.pow(hundredth, 3)==i)
                
                System.out.println(i);
               
        }
    }
}

交换两个数

注意

如果想构造方法交换两个数,不能直接传实参交换,因为实参与形参互不干扰(与C语言一样)
可以传一个数组进行交换
public class P39_2 {
   
        public static void main(String[] args) {
            int a = 10;
            int b = 1;
            //交换前
            System.out.println(a+" "+b);
            int temp = a; a = b; b = temp;
            //交换后
            System.out.println(a+" "+b);
        } 
}

6.项目实训:基于控制台设计简易的打折与累加计算器
 //基于控制台设计简易的打折与累加计算器、
 import java.util.Scanner;
public class Project01 {
   
    public static void main(String [] args){
        System.out.println("欢迎使用计算器");

        System.out.println("请输入0或1,1-计算,0-退出");
        Scanner inx = new Scanner(System.in);
        int bool = inx.nextInt();
        while(true){
            if(bool == 1){
                input_output();
            }
            else if(bool == 0){
                System.out.println("谢谢使用!");
                break;
            }
            System.out.println("请输入0或1,1-计算,0-退出");
            bool = inx.nextInt();
        }
        inx.close();
    }
    public static void input_output(){
        System.out.println("请输入第一个数:");
        Scanner in = new Scanner(System.in);
        int data1 = in.nextInt();
        System.out.println("请输入运算符:");
        String sign = in.next();
        System.out.println("请输入第二个数:");
        int data2 = in.nextInt();
        int res = countData(data1,sign,data2);
        if(res == -99999999)
            System.out.println("数据错误");
        else
            System.out.println("答案为"+res);
        
    }

    public static int countData(int data1 , String sign ,int data2){
        int data = 0;
        if(sign.equals("+"))
            data = data1 + data2;
        else if(sign.equals("-"))
            data = data1 - data2;
        else if(sign.equals("*"))
            data = data1 * data2;
        else if(sign.equals("/"))
            data = data1 / data2;
        else
            data = -99999999;
        return data;
    }
}

7.Arrays类相关
//数组的相关操作
import java.util.Arrays;
public class Arr {
    public static void main(String[] args) {
        int [] arr = new int [33];
        int []  b = {88,92,9};
       
        //复制
        System.arraycopy(b, 1, arr, 0, 1);
        //大小
        System.out.println(arr.length);
        //遍历输出
        System.out.println(Arrays.toString(arr));
        //比较
        System.out.println(Arrays.equals(b,arr));
        //填充
        int [] a = new int [4];
        Arrays.fill(a,1);
        System.out.println(Arrays.toString(a));
        //排序
        Arrays.sort(b);
        System.out.println(Arrays.toString(b));
    }
}

8.静态方法与非静态方法区别
1.静态方法是属于类的,可以直接通过类加函数名调用(类名.静态方法);
  非静态方法,是属于实例化后类的方法,调用前需要实例化,才能调用;
public class Function {
    public static void main(String [] agrs){
        //调用静态方法
        Test.function1();
        //调用非静态方法
        Test a = new Test();//实例化类
        a.function2();
    }
}

class Test{

    public static void function1(){
        System.out.println("HelloWorld");
    }
    public void function2(){
        System.out.println("HelloWorld");
    }
}
2.静态方法只能只允许访问静态成员(即静态成员变量和静态方法),不允许访问实例成员(即实例成员变量和实例方法),而实例方法不存在这个限制。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756363.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号