三角形java编程代码 用java编写三角形星号

用Java语言编写,要求申明三角形类,继承图形抽象类,计算三角形的周长和面积!图形抽象类的代码:
abstract class MyShape {
abstract int calGirth();//求周长
abstract double calSquare();//求面积
}
三角形类的实现:
public class Triangle extends MyShape{
int borderA, borderB, borderC;
Triangle(int a, int b, int c){borderA = a; borderB = b; borderC = c;}
Triangle(){borderA = borderB = borderC = 0;}
@Override
int calGirth() {
return borderAborderBborderC;
}
@Override
double calSquare() {
double p = calGirth() / 2;
return Math.sqrt(p * (p - borderA) * (p - borderB) * (p - borderC));
}
public static void main(String[] args) {
Triangle test = new Triangle(3, 4, 5);
System.out.println("The girth of the triangle is "test.calGirth());
System.out.println("The square of the triangle is "test.calSquare());
}
}
实现两个抽象函数,测试结果正确 , 输出为:
The girth of the triangle is 12
The square of the triangle is 6.0
用java程序实现三角形的输出 , 要求如下class NoParamException extends Exception{
public NoParamException(String message)
{super(message);}
}
class InputDecimalException extends Exception{}
public class HOMEWORK
{
public static float getHeight(String args[])throws NoParamException,InputDecimalException
{
float m;
if(args.length==0)
throw new NoParamException("NoParamException occures!");
m=Float.parseFloat(args[0]);
if((int)m!=m)throw new InputDecimalException();
return m;
}
public static void main(String args[])
{
float H=0;
try{
H=getHeight(args);
}
catch(NoParamException e){
System.out.println("NoParamException occures,please input again!");
}
catch(InputDecimalException e){
System.out.println("InputDecimalException occures,please input again!");
}
catch(Exception e){
System.out.println("NoParamException occures,please input again!");
}
for(int i=1;i=H;i)
{
for(int j=0;jH-i;j)
System.out.print(" ");
for(int k=0;k2*i-1;k)
System.out.print("*");
System.out.print("\n");
}
}
}
可以进行两种异常控制三角形java编程代码,一种是无参数异常三角形java编程代码,一种是输入小数的异常
这是运行过程三角形java编程代码,记得程序中的publi class名字改过来,与文件名一样
用JAVA编写程序实现在控制台用“*”输出一个倒立的等边三角型 , 并写到文件triangle.txt中1、创建java类,TestTriangle,
2、编写java代码,实现倒立等边三角形的打印 ,
public void print() {
String tri_str = "";
for (int i = 0; ia; i) {
for (int m = 0; mi; m) {
tri_str= " ";
System.out.print(" ");
}
for (int j = 0; ja - i; j) {
tri_str= "* ";
System.out.print("* ");
}
tri_str= "\r\n";
System.out.println();
}
outPrint(tri_str);
}
3、再编写输出triangle.txt文件的代码 ,
public void outPrint(String str1) {
try {
PrintStream ps = new PrintStream("triangle.txt");
ps.append(str1);
ps.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
4、执行java代码,控制台输出*号倒三角,
5、查看triangle.txt文件 , 同样是*号倒三角,
用Java编写一个判断三角形的程序import java.io.*;
public class Test{
public static void main(String[] args)throws IOException{
int[] a=new int[3];
System.out.println("请从小到大输入");
for(int i=0;i3;i){
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
a[i]=Integer.parseInt(in.readLine());
}
double temp,theat,pi=3.1415;
if(a[0] a[1]a[2]||a[1] a[2]a[0]||a[0] a[2]a[1]){
System.out.println("三角形java编程代码你输入三角形java编程代码的不能构成三角形");
}
else if(a[0]==a[1]) {
System.out.println("三角形java编程代码你输入的三角形是等腰三角形");
}
else {
if(0==a[0]*a[0] a[1]*a[1]-a[2]*a[2]){
System.out.println("你输入的三角形是直角三角形");
}
temp=(double)(a[0]*a[0] a[1]*a[1]-a[2]*a[2])/(2*a[0]*a[1]);
System.out.println(temp);
theat=Math.acos(temp);
//theat=(180*theat)/pi;
if(theat90theat180){
System.out.println("你输入的三角形是钝角三角形");
}
else if(theat90theat0){
System.out.println("你输入的三角形是锐角三角形");
}
}
}}做的不是很好 可以将就下 哈
我是Java语言编程初学者,希望大家能够列举用if条件语句和for循环语句结合开发出实心三角形的代码做个例子,希望有所帮助 。
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PlotSolidTriangle {
// 绘制实心三角形
static void solidTriangle(int n) {
for (int i = 1; i = n; i) {
for (int k = 1; k = n - i; k) {
System.out.print(" ");
}
for (int j = 1; j = 2 * (i - 1)1; j) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) throws IOException {
int n;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入三角形的行数:");
n = Integer.parseInt(br.readLine());
solidTriangle(n);
}
}
结果
【三角形java编程代码 用java编写三角形星号】关于三角形java编程代码和用java编写三角形星号的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读