Java练习题二【分支结构】

目录

练习题8.代数:解一元二次方程

练习题9.代数:求解2×2线性方程

练习题10.回文数字

练习题11.游戏:剪刀、石头、布

练习题12.计算三角形的周长

练习题13.几何:点是否在圆内?

练习题14.几何:点是否在矩形内?

练习题15.几何:点是否在三角形内?

练习题16.几何:两个矩形

练习题17.几何:两个圆


练习题8.代数:解一元二次方程

Java练习题二【分支结构】_第1张图片

/*
    1.提示用户输入a,b,c
    2.计算判别式delt的值
    3.对delt进行判断
        3.1 如果delt大于0 则表示有两个实数解
            分别计算两个解,并输出
        3.2 如果delt等于0 则表示有两个相等的实数解
            计算一个解,并输出
        3.3 如果delt小于0 则表示没有实数解 
            输出无解
*/
import java.util.Scanner;
public class Demo08 {
	public static void main(String[] args) {
                //1.
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a,b,c:");
		double a=scanner.nextDouble();
		double b=scanner.nextDouble();
		double c=scanner.nextDouble();
                //2.
		double delt=b*b-4*a*c;
                //3.
		if(delt>0){
                        //3.1
			double x1=(-b+Math.sqrt(delt))/(2*a);
			double x2=(-b-Math.sqrt(delt))/(2*a);
			System.out.print("The equation has two roots "+x1+" and "+x2);
		}else if(delt==0){
                        //3.2
			double x1=-b/(2*a);
			System.out.println("The equation has one root "+x1);
		}else{
                        //3.3
			System.out.println("The equation has no real roots");
		}
	}
}

练习题9.代数:求解2×2线性方程

Java练习题二【分支结构】_第2张图片

/*
    1.提示用户输入a,b,c,d四个系数,和e,f两个常数
    2.计算判别式delt的值
    3.对delt进行判断
        3.1 如果delt不等于0 则方程组有解
            分别计算两个解并输出
        3.2 如果delt等于0 则方程组无解
            输出无解
*/        
import java.util.Scanner;
public class Demo09 {
	public static void main(String[] args) {
                //1.
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a,b,c,d,e,f:");
		double a=scanner.nextDouble();
		double b=scanner.nextDouble();
		double c=scanner.nextDouble();
		double d=scanner.nextDouble();
		double e=scanner.nextDouble();
		double f=scanner.nextDouble();
                //2.
		double delt=a*d-b*c;
                //3.
		if(delt!=0){
                        //3.1
			double x=(e*d-b*f)/delt;
			double y=(a*f-e*c)/delt;
			System.out.println("x is "+x+" , and y is "+y);
		}else{
                        //3.2
			System.out.println("has no solution");
		}
	}
}

练习题10.回文数字

Java练习题二【分支结构】_第3张图片

/*
    1.输入一个三位数
    2.解析出这个数字的个位
    3.解析出这个数字的百位
    4.如若该三位数是个回文数字,则个位和百位相等
*/
import java.util.Scanner;
public class Demo10 {
	public static void main(String[] args) {
                //1.
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a number:");
		int number=scanner.nextInt();
                //2.
		int a=number%10;
                //3.
		int b=number/100;
                //4.
		if(a==b){
			System.out.println("是回文数字");
		}else{
			System.out.println("不是!");
		}
	}
}

练习题11.游戏:剪刀、石头、布

Java练习题二【分支结构】_第4张图片

/*
    1.提示用户输入选择
    2.随机生成电脑的选择
    3.计算输赢,result作为标记
    4.拼接用户和电脑的选择
    5.打印输赢结果 
        5.1 result为0 说明平局
        5.2 result为1 说明玩家输
        5.3 result为2 说明玩家赢
*/
import java.util.Scanner;
public class Demo11 {
	public static void main(String[] args) {
                //1.
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter :");
		int p=scanner.nextInt();
                //2.
		int c=(int) (Math.random()*3);//[0,1)*3 [0,3)
		System.out.println(c);
		int result=-1;
		String cStr="";
		String pStr="";
                //3.
		if(p==0){
			result=(p+c+3)%3;
		}
		if(p==1){
			result=(p+c+1)%3;
		}
		if(p==2){
			result=(p+c+2)%3;
		}
                //4.
		if(p==0){
			pStr="scissor";
		}else if(p==1){
			pStr="rock";
		}else{
			pStr="paper";
		}

		if(c==0){
			cStr="scissor";
		}else if(c==1){
			cStr="rock";
		}else{
			cStr="paper";
		}
                //5.
		System.out.print("The computer is "+cStr+". You are "+pStr+".");
		switch (result) {
			case 0:
				System.out.println(" too! It is draw.");
				break;
			case 1:
				System.out.println("You lose.");
				break;
			case 2:
				System.out.println("You won.");
				break;
		}
	}
}

练习题12.计算三角形的周长

/*
    1.输入三个边
    2.判断三边的可行性
        2.1 可行则计算周长
        2.2 不可行则输出非法三边
*/
import java.util.Scanner;
public class Demo12 {
	public static void main(String[] args) {
                //1.
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter the three side of a triangle:");
		double side1=scanner.nextDouble();
		double side2=scanner.nextDouble();
		double side3=scanner.nextDouble();
                //2.
		if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1){
                        //2.1
			double l=side1+side2+side3;
			System.out.println("The length of triangle is "+l);
		}else{
                        //2.2
			System.out.println("The side is Illegal");
		}
	}
}

练习题13.几何:点是否在圆内?

Java练习题二【分支结构】_第5张图片

/*
    1.输入坐标点
    2.计算坐标点与圆点间的距离
    3.判断距离与半径的关系
        3.1 距离大于半径 点在圆外
        3.2 距离小于半径 点在圆内
*/
import java.util.Scanner;
public class Demo13 {
	public static void main(String[] args) {
                //1.    
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter a point with two coordinates:");
		double x=scanner.nextDouble();
		double y=scanner.nextDouble();
                //2.
		double deltX=x-0;
		double deltY=y-0;
		double distance=Math.sqrt(deltX*deltX+deltY*deltY);
                //3.
		if(distance>10){
                        //3.1
			System.out.println("Point("+x+","+y+") is not in the circle.");
		}else{
                        //3.2
			System.out.println("Point("+x+","+y+") is in the circle.");
		}
	}
}


练习题14.几何:点是否在矩形内?


import java.util.Scanner;
public class Demo14 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.输入坐标点
		System.out.print("Enter a point:");
		double x=Math.abs(scanner.nextDouble());
		double y=Math.abs(scanner.nextDouble());
		//2.判断该坐标点x的量和y的量
		if(x>5||y>2.5){
			System.out.println("矩形外");
		}else{
			System.out.println("矩形内");
		}
	}
}

练习题15.几何:点是否在三角形内?

Java练习题二【分支结构】_第6张图片

import java.util.Scanner;
public class Demo15 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.print("Enter x,y:");
		double x=scanner.nextDouble();
		double y=scanner.nextDouble();
		if(x>=0&&x<=200){
			if((200-x)/y>=2){
				System.out.println("in");
				return;//直接结束函数 谨慎使用
			}
		}
		System.out.println("out");
	}
}

练习题16.几何:两个矩形

Java练习题二【分支结构】_第7张图片

import java.util.Scanner;


public class Demo16{
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.输入两个矩形的宽高和中心坐标
		System.out.print("Enter r1:");
		double xo=scanner.nextDouble();
		double yo=scanner.nextDouble();
		double W=scanner.nextDouble();
		double H=scanner.nextDouble();
		System.out.print("Enter r2:");
		double x=scanner.nextDouble();
		double y=scanner.nextDouble();
		double w=scanner.nextDouble();
		double h=scanner.nextDouble();
		//2.计算是否r2在r1内
		if(x<=(W/2-w/2+xo)&&x>=(xo-W/2+w/2)&&y<=(H/2-h/2+yo)&&y>=(yo-H/2+h/2)){
			System.out.println("r2 in r1");
			//计算是否r2在r1外
		}else if(x>=(W/2+w/2+xo)||x<=(xo-W/2-w/2)||y>=(H/2+h/2+yo)||y<=(yo-H/2-h/2)){
			System.out.println("r2 out r1");
		}else{
			System.out.println("r2 overlaps r1");
		}
	}
}

练习题17.几何:两个圆

import java.util.Scanner;
public class Demo17 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//1.输入两个圆的信息 圆心坐标 半径
		System.out.print("Enter circle1:");
		double x1=scanner.nextDouble();
		double y1=scanner.nextDouble();
		double r1=scanner.nextDouble();
		System.out.print("Enter circle2:");
		double x2=scanner.nextDouble();
		double y2=scanner.nextDouble();
		double r2=scanner.nextDouble();
		//2.计算两圆心之间的距离
		double distance=Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
		//3.判断距离所表示的两圆之间的关系
		if(distance<=Math.abs(r1-r2)){
			System.out.println("c2 in c1");
		}else if(distance>=r1+r2){
			System.out.println("c2 out c1");
		}else{
			System.out.println("c2 overlaps c1");
		}
	}
}

 

你可能感兴趣的:(Java练习题)