我的学习Java的前期几个程序
public static void main(String[] args)
{
System.out.println("I like java");
}
输出*号三角形
public static void main(String[] args) {
int x,a,b;
int n=8;//n为三角形行数
for(x=1;x<=n;x++)
{
for(a=0;a<n-x;a++){
System.out.printf(" ");
}
for(b=0;b<2*x-1;b++){
System.out.printf("* ");
}
System.out.println();
}
}
输入两个数根据数的大小输出结果
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int a;
int b;
System.out.print("输入第一个数:");
a=input.nextInt();
System.out.print("输入第二个数:");
b=input.nextInt();
if(a==b)
{
System.out.printf("%d==%d",a,b);
System.out.println();
}
if(a!=b)
{
System.out.printf("%d!=%d",a,b);
System.out.println();
}
if(a<b)
{
System.out.printf("%d<%d",a,b);
System.out.println();
}
if(a>b)
{
System.out.printf("%d>%d",a,b);
System.out.println();
}
}
输出半径为5的圆,长为5宽为3的矩形和上底为2下底为4高为6的梯形的面积
double area(double r)
{
return(Math.PI*r*r);
}
double area(double width,double height)
{
return(width*height);
}
double area(double shangdi,double xiadi,double gao)
{
return(gao*(shangdi+xiadi)/2);
}
public static void main(String[] args)
{
Test61 areaclucate=new Test61();
System.out.println("圆的面积:"+areaclucate.area(5.0));
System.out.println("矩形的面积:"+areaclucate.area(3,5));
System.out.println("梯形的面积:"+areaclucate.area(2,4,6));
Scanner input=new Scanner(System.in);
}
输入两个数的出俩个数的和
import java.util.Scanner;
public class Test65
{
double xC;
double yC;
//public Test65(double x,double y)
//{
// xC=x;
// yC=y;
//}
//因为在方法实例化时会报错,于是注释我自己的构造方法后我发现可以运行了,具体不知道为什么
double add(double xC,double yC)
{
return(xC+yC);
}
public static void main(String[] args)
{
double a,b;
Test65 insadd=new Test65();
Scanner input=new Scanner(System.in);
System.out.print("输入第一个数:");
a=input.nextInt();
System.out.print("输入第二个数:");
b=input.nextInt();
System.out.println("两数之和为:"+insadd.add(a,b));
}
}