美文网首页
Java学习第3天

Java学习第3天

作者: _Raye | 来源:发表于2016-12-06 19:22 被阅读0次

Java中使用的快捷键:
格式化代码的快捷键 crtl + shift + f
自动导入 crtl + shift + o

Java中后置的++或者--,先赋值,再做自加自减
前置的++或--,先运算,再赋值

public static void main(String[] args) {
    int a = 5;
    int b = a++;// ++把自身加1
    System.out.println(b);

    // 一个数乘以8,最快的方式
    int x = 3;
    System.out.println(x << 3);// 左移3位,相当于3*2^3

    // x*31最快的方法
    x = (x << 5) - x;

    int y = 90;
    System.out.println(y >> 3);// 右移3位,相当于90/2^3

}

练习:输入三角形三条边,判断能否构成三角形,并计算出周长及面积

  • java中求根号的方法:Math.sqrt()
public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入三角形的三条边的长度:");
        
        if(input.hasNextDouble()){
            double a = input.nextDouble();
            double b = input.nextDouble();
            double c = input.nextDouble();
            if (a > 0 && b > 0 && c > 0 && a + b > c && a + c > b && b + c > a) {
                
                System.out.println("周长为:"+(a+b+c));
                double d = (a+b+c)/2;
                System.out.printf("面积为:%.2f",Math.sqrt(d*(d-a)*(d-b)*(d-c)));
            }
            else{
                System.out.println("不能构成三角形");
        }
        }
        else{
            System.out.println("请输入正确的数字");
        }
        input.close();
    }

练习:输入身高体重,判断身体状况

  • 字符串不能用==来判断,需用equals
  • 若想用""号的话,必须加\,转义字符
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入你的名字:");
        String name = input.nextLine();
        System.out.print("请输入你的身高(cm):");               
        double height = input.nextDouble();
        System.out.print("请输入你的体重(kg):");       
        double weight = input.nextDouble();
        System.out.print("请输入性别(\"男\"或\"女\"):");
            String gender = input.next();
            int refValue = gender.equals("男") ? 110 : 120;
            if (height - refValue >= weight) {
            System.out.println(name + "的身体状况良好!");
            }
            else{
            System.out.println(name + "属于肥胖人群!");
            }                   
        input.close();
    }

练习:输入年份,判断是否为闰年

  • 三元条件运算符(a > b ? a : b),相当于判断a>b,结果为真,输出a,否则输出b
public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入年份:");
        //分支结构(选择结构)-可以让程序有多条执行路径
        if (input.hasNextInt()){
            int year = input.nextInt();
            if(year > 0){
                boolean isLeapYear = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
                System.out.println(year + "年" + (isLeapYear ? "是" : "不是") + "闰年");  
            }       
            else{
                System.out.println("年份必须是正整数!");
            }
        }
        
        else{
            System.out.println("请输入正确的年份!");
        }
        input.close();
    }

练习:个人所得税计算器(if - else if分支结构)

import java.util.Scanner;
public class Homework02 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("输入你的工资:");//salary定义为工资
        double salary = input.nextDouble();
        System.out.print("输入你的五险一金:");//money定义为五险一金
        double money = input.nextDouble();
        final int TAX = 3500;//个人所得税的起征点为3500元
        double income = salary - TAX;//含税级距(工资-起征点).超过3500的部分
        double pertax ;//个人所得税
        if (income <= 1500) {
            pertax = (income - money) * 0.03;
        }
        else if (income <= 4500) {
            pertax = (income - money) * 0.1 - 105;
        }
        else if (income <= 9000) {
            pertax = (income - money) * 0.2 - 555;
        }
        else if (income <= 35000) {
            pertax = (income - money) * 0.25 - 1005;
        }
        else if (income <= 55000) {
            pertax = (income - money) * 0.3 - 2755;
        }
        else if (income <= 80000) {
            pertax = (income - money) * 0.35 - 5505;
        }
        else {
            pertax = (income - money) * 0.45 - 13505;
        }
        System.out.println("个人所得税为:" + pertax);
        if (pertax <= 0) {
            System.out.println("实际到手工资:" + (salary - money));
        }
        else {
            System.out.println("实际到手工资:" + (salary - pertax - money));
        }       
        input.close();
    }
}

相关文章

网友评论

      本文标题:Java学习第3天

      本文链接:https://www.haomeiwen.com/subject/nfotmttx.html