首页 > 代码库 > Java学习笔记10

Java学习笔记10

31.
编写当年龄age大于13且小于18时结果为true的布尔表达式
age > 13 && age < 18

32.
编写当体重weight大于50或身高大于160时结果为true的布尔表达式
weight > 50 || height > 160

33.
编写当体重weight>50且身高height大于160时结果为true的布尔表达式
weight > 50 && height > 160

34.
编写当体重weight大于50或身高height大于160,但不能同时满足这两个条件时,结果为true的布尔表达式
weight > 50 ^ height > 160

switch语句支持byte,short,char,int,enum,String,不支持long数据类型

 

package welcome;

import java.util.Scanner;

/*
 * 解一元二次方程
 */

public class ComputeEquation {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter a, b, c: ");
        double a = in.nextDouble();
        double b = in.nextDouble();
        double c = in.nextDouble();
        
        double judgeExpression = b * b - 4 * a * c;
        
        double r1 = 0;
        double r2 = 0;
        if(judgeExpression > 0){
            r1 = (-b + Math.pow(judgeExpression, 0.5)) / (2 * a);
            r2 = (-b - Math.pow(judgeExpression, 0.5)) / (2 * a);
            System.out.println("The roots are " + r1 + " and " + r2);
        }else if(judgeExpression == 0){
            r1 = (-b + Math.pow(judgeExpression, 0.5)) / (2 * a);
            System.out.println("The root is " + r1);
        }else{
            System.out.println("The equation has no real roots.");
        }
    }
}

 

package welcome;

import java.util.Scanner;
/*
 * 检查一个数字是否是偶数
 */
public class CheckEven {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter an integer: ");
        int number = in.nextInt();
        
        System.out.printf("Is %d an even number? %b ", number, (number % 2 == 0));
    }
}

 

Java学习笔记10