用java面向对象思想求三角形的面积和周长

问题:用户输入三角形的三条边,当三角形不成立的时候,给用户重新再次输入的时候,假设第二次输入正确,因为使用了函数嵌套,那么会出现递归的问题?

最终解决方法:强制抛出一个异常,就可以解决这个问题

以下是我的代码:

domain类

package com.itxiaojia.domain;
//实体类
public class Triangle {
	//三角形的三条边
	private double side1;
	private double side2;
	private double side3;
	public void setSide1(double side1){
		this.side1=side1;
	}
	public double getSide1(){
		return side1;
	}
	public void setSide2(double side2){
		this.side2=side2;
	}
	public double getSide2(){
		return side2;
	}
	public void setSide3(double side3){
		this.side3=side3;
	}
	public double getSide3(){
		return side3;
	}
	
}


工具类,主要是验证数据

package com.itxiaojia.utils;
import java.util.regex.*;
/**
 * 检查工具类
 * @author wu
 *
 */
public class Check {
	/**
	 * 判断是否为三角形
	 * @param a1
	 * @param a2
	 * @param a3
	 * @return
	 */
	public static boolean isTriangle(double a1,double a2,double a3){
		if((a1+a2)>a3 && (a1+a3)>a2 && (a2+a3)>a1){
			return true; //如果符合三角形成立的条件,返回true
		}
		return false;
	}
//判断用户输入是否是数字字符串
	public static boolean isNumber(String str){
		Pattern p=Pattern.compile("^\\d+[0-9 .]*");
		Matcher m=p.matcher(str);
		if(!m.matches()){
			return false;  //如果用户输入的不是数字,返回false
		}
		return true;
	}
}

包含main函数的类

package com.itxiaojia.test;
import java.util.Scanner;
import com.itxiaojia.utils.Check;
public class Demo1 {
	public static void main(String[] args){
			show();
	}
	/**
	 * 显示界面
	 * @throws Exception 
	 */
	public static void show(){
		System.out.println("*************************************\n" +
				"***需求: 用户输入三角形的三条边,求其周长和面积***\n************************************");
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入三角的第一条边:");
		String str=scan.nextLine();
		while(!Check.isNumber(str)){
			System.out.println("你输入的格式不正确,请重新输入:");
			str=scan.nextLine();
		}
		double a1=Double.parseDouble(str);
		
		//第二条边
		System.out.println("请输入三角形的第二条边:");
		str=scan.nextLine();
		while(!Check.isNumber(str)){
			System.out.println("你输入的格式不正确,请重新输入:");
			str=scan.nextLine();
		}
		double a2=Double.parseDouble(str);
		
		//第三条边
		System.out.println("请输入三角形的第三条边:");
		str=scan.nextLine();
		while(!Check.isNumber(str)){
			System.out.println("你输入的格式不正确,请重新输入:");
			str=scan.nextLine();
		}
		double a3=Double.parseDouble(str);
		//判断三角形是否成立
		while(!Check.isTriangle(a1, a2, a3)){
			System.out.println("你输入的三条边不符合三角形成立的条件\n请重新开始输入:");
			show();
		}
		System.out.println("该三角形的周长为:"+getPerimeter(a1,a2,a3)+" 面积为:"+getArea(a1,a2,a3));
		
	}
	/**
	 * 三角形的周长
	 * @param a1
	 * @param a2
	 * @param a3
	 * @return
	 */
	public static double getPerimeter(double a1,double a2,double a3){
		return a1+a2+a3;
	}
	/**
	 * 三角形的面积
	 * @param a1
	 * @param a2
	 * @param a3
	 * @return
	 */
	public static double getArea(double a1,double a2,double a3){
		double d=(a1+a2+a3)/2;
		return Math.sqrt(d*(d-a1)*(d-a2)*(d-a3));
	}
}

以下是运行后输出结果截图

这个是一次性并且值正常的结果



下面这个是问题,当输出三角形的面积和周长的时候并没有结束,而是进入了递归



解决方法:1,试过用return;但没有效果

   2,通过抛出一个异常强制结束递归

最初想到在show方法最后一行加上return;但是由于采用的递归,所以没有达到我要的效果。如下:

public static void show(){
		System.out.println("*************************************\n" +
				"***需求: 用户输入三角形的三条边,求其周长和面积***\n************************************");
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入三角的第一条边:");
		String str=scan.nextLine();
		while(!Check.isNumber(str)){
			System.out.println("你输入的格式不正确,请重新输入:");
			str=scan.nextLine();
		}
		double a1=Double.parseDouble(str);
		
		//第二条边
		System.out.println("请输入三角形的第二条边:");
		str=scan.nextLine();
		while(!Check.isNumber(str)){
			System.out.println("你输入的格式不正确,请重新输入:");
			str=scan.nextLine();
		}
		double a2=Double.parseDouble(str);
		
		//第三条边
		System.out.println("请输入三角形的第三条边:");
		str=scan.nextLine();
		while(!Check.isNumber(str)){
			System.out.println("你输入的格式不正确,请重新输入:");
			str=scan.nextLine();
		}
		double a3=Double.parseDouble(str);
		//判断三角形是否成立
		while(!Check.isTriangle(a1, a2, a3)){
			System.out.println("你输入的三条边不符合三角形成立的条件\n请重新开始输入:");
			show();
		}
		System.out.println("该三角形的周长为:"+getPerimeter(a1,a2,a3)+" 面积为:"+getArea(a1,a2,a3));
		return;
	}

运行结果如下图,显然没有达到要求



后来想到了这个方法,也就是可以抛出一个异常来结束后面的代码执行。只要修改两个地方 :第1个地方是在main方法中:

public class Demo1 {
	public static void main(String[] args){
			try {
				show();
			} catch (Exception e) {
			}
	} 

第2个地方是在show方法中:

public static void show() throws Exception{
		System.out.println("*************************************\n" +
				"***需求: 用户输入三角形的三条边,求其周长和面积***\n************************************");
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入三角的第一条边:");
		String str=scan.nextLine();
		while(!Check.isNumber(str)){
			System.out.println("你输入的格式不正确,请重新输入:");
			str=scan.nextLine();
		}
		double a1=Double.parseDouble(str);
		
		//第二条边
		System.out.println("请输入三角形的第二条边:");
		str=scan.nextLine();
		while(!Check.isNumber(str)){
			System.out.println("你输入的格式不正确,请重新输入:");
			str=scan.nextLine();
		}
		double a2=Double.parseDouble(str);
		
		//第三条边
		System.out.println("请输入三角形的第三条边:");
		str=scan.nextLine();
		while(!Check.isNumber(str)){
			System.out.println("你输入的格式不正确,请重新输入:");
			str=scan.nextLine();
		}
		double a3=Double.parseDouble(str);
		//判断三角形是否成立
		while(!Check.isTriangle(a1, a2, a3)){
			System.out.println("你输入的三条边不符合三角形成立的条件\n请重新开始输入:");
			show();
		}
		System.out.println("该三角形的周长为:"+getPerimeter(a1,a2,a3)+" 面积为:"+getArea(a1,a2,a3));
		throw new Exception();  //强制退出递归
	}

最终运行的结果为:





  • 7
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,让我来回答你的问题。即使我没有办法确定你是谁或者你的具体要求,我还是可以为你提供一个Java面向对象的解决方案。 首先,我们需要定义一个Shape作为基,然后在其上派生三角形,圆和矩形的子。每个子都有自己的方法来计算面积周长,这些方法将在其父Shape上进行实现。 以下是样例代码: ``` abstract class Shape { abstract double getArea(); abstract double getPerimeter(); } class Triangle extends Shape { double base; double height; double side1; double side2; double side3; public Triangle(double base, double height, double side1, double side2, double side3) { this.base = base; this.height = height; this.side1 = side1; this.side2 = side2; this.side3 = side3; } public double getArea() { return 0.5 * base * height; } public double getPerimeter() { return side1 + side2 + side3; } } class Circle extends Shape { double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { return Math.PI * radius * radius; } public double getPerimeter() { return 2 * Math.PI * radius; } } class Rectangle extends Shape { double width; double height; public Rectangle(double width, double height) { this.width = width; this.height = height; } public double getArea() { return width * height; } public double getPerimeter() { return 2 * (width + height); } } public class Main { public static void main(String[] args) { Triangle triangle = new Triangle(5, 3, 4, 3, 5); System.out.println("Triangle area: " + triangle.getArea()); System.out.println("Triangle perimiter: " + triangle.getPerimeter()); Circle circle = new Circle(3); System.out.println("Circle area: " + circle.getArea()); System.out.println("Circle perimiter: " + circle.getPerimeter()); Rectangle rectangle = new Rectangle(5, 4); System.out.println("Rectangle area: " + rectangle.getArea()); System.out.println("Rectangle perimiter: " + rectangle.getPerimeter()); } } ``` 在这段代码中,我们定义了Shape抽象和其子Triangle,Circle和Rectangle。 一个Shape只是一个抽象概念,不能实际被创建。 Shape子都重写了getArea()和getPerimeter()方法。 在main方法中,我们创建了一个Triangle对象,一个Circle对象和一个Rectangle对象,并调用它们各自的getArea()和getPerimeter()方法来显示它们的面积周长。 提醒:记得在你自己的代码中对错误进行处理,确保输入有合理的值以计算正确的结果。加油!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值