求简单的JAVA小程序的源代码
1个回答
展开全部
《Java就业培训教程》P34源码
程序清单:Promote.java
class Promote
{
public static void main(String args[])
{
byte b = 50;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
}
《Java就业培训教程》P35源码
程序清单:TestScope.java
public class TestScope
{
public static void main(String[] args)
{
int x = 12;
{
int q = 96; // x和q都可用
System.out.println("x is "+x);
System.out.println("q is "+q);
}
q = x; /* 错误的行,只有x可用, q 超出了作用域范围 */
System.out.println("x is "+x);
}
}
《Java就业培训教程》P37源码
程序清单:TestVar.java
public class TestVar
{
public static void main(String [] args)
{
int x;//应改为int x=0;
x=x+1; //这个x由于没有初始化,编译会报错。
System.out.println("x is "+x);
}
}
程序清单:Func1.java
public class Func1
{
public static void main(String [] args)
{
/* 下面是打印出第一个矩形的程序代码*/
for(int i=0;i<3;i++)
{
for(int j=0;j<5;j++)
{
System.out.print("*");
}
System.out.println(); //换行
}
System.out.println();
//下面是打印出第二个矩形的程序代码
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();
//下面是打印出第三个矩形的程序代码
for(int i=0;i<6;i++)
{
for(int j=0;j<10;j++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();
}
}
《Java就业培训教程》P39源码
程序清单:Func2.java
public class Func2
{
public static void drawRectangle(int x,int y)
{
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
System.out.print(" * ");
}
System.out.println(); //换行
}
System.out.println();
}
public static void main(String [] args)
{
drawRectangle(3,5);
drawRectangle(2,4);
drawRectangle(6,10);
}
}
《Java就业培训教程》P40源码
程序清单:Func3.java
public class Func3
{
public static int getArea(int x,int y)
{
return x*y;
}
public static void main(String [] args)
{
int area = getArea(3,5);
System.out.println("first Acreage is " + area);
System.out.println("second Acreage is "+ getArea(2,4));
getArea(6,10);
}
}
《Java就业培训教程》P41源码
程序清单:Func4.java
public class Func4
{
public static int getArea(int x,int y)
{
if(x<=0 || y<=0)
{
return -1;
}
return x*y;
}
public static void main(String [] args)
{
int area = getArea(3,5);
System.out.println("first Acreage is " + area);
System.out.println("second Acreage is "+ getArea(2,4));
getArea(6,10);
}
}
《Java就业培训教程》P43源码
程序清单:Test.java
public class Test
{
public static void main(String [] args)
{
int isum;
double fsum;
isum=add(3,5);
isum=add(3,5,6);
fsum=add(3.2,6.5);
}
public static int add(int x,int y)
{
reutrn x+y;
}
public static int add(int x,int y,int z)
{
return x+y+z;
}
public static double add(double x,double y)
{
return x+y;
}
}
《Java就业培训教程》P47源码
public class TestAnd
{
public static void main(String[] args)
{
int x=0;
int y=0;
if(x!=0 && y==y/x)
System.out.println("y = "+y);
}
}
《Java就业培训教程》P48源码
程序清单:ShiftTest.java
public class ShiftTest
{
public static void main(String [] args)
{
int x=0x80000000;
int y=0x80000000;
x=x>>1;
y=y>>>1;
System.out.println("0x80000000>>1 = " + Integer.toHexString(x));
System.out.println("0x80000000>>>1 = " + Integer.toHexString(y));
}
}
《Java就业培训教程》P61源码
程序清单:TestDo.java
public class TestDo
{
public static void main(String[] args)
{
int x=3;
while(x==0)
{
System.out.println("ok1");
x++;
}
int y=3;
do
{
System.out.println("ok2");
y++;
}
while(y==0);
}
}
《Java就业培训教程》P64源码
程序清单:PrintOddNum.java
public class PrintOddNum
{
public static void main(String [] args)
{
for(int i=0;i<10;i++)
{
if(i%2==0)
continue;
System.out.println(i);
}
}
}
《Java就业培训教程》P67源码
程序清单:TestArray.java
public class TestArray
{
public static void main(String [] args)
{
int x[];
x=new int[100];
for(int i=0;i<100;i++)
{
System.out.println("x"+i+" is "+x[i]);
}
}
}
《Java就业培训教程》P73源码
程序清单:TestArrayCopy.java
public class TestArrayCopy
{
public static void main(String [] args)
{
int ia[]=new int[]{1,2,3,4,5};
int ib[]=new int[]{9,8,7,6,5,4,3};
System.arraycopy(ia,0,ib,0,3);
// 复制源数组中从下标0开始的3个元素到目的数组,从下标0的位置开始存储。
for(int i=0;i<ia.length;i++)
System.out.print(ia[i]);
System.out.println();
for(int j=0;j<ib.length;j++)
System.out.print(ib[j]);
System.out.println();
}
}
程序清单:Promote.java
class Promote
{
public static void main(String args[])
{
byte b = 50;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
}
《Java就业培训教程》P35源码
程序清单:TestScope.java
public class TestScope
{
public static void main(String[] args)
{
int x = 12;
{
int q = 96; // x和q都可用
System.out.println("x is "+x);
System.out.println("q is "+q);
}
q = x; /* 错误的行,只有x可用, q 超出了作用域范围 */
System.out.println("x is "+x);
}
}
《Java就业培训教程》P37源码
程序清单:TestVar.java
public class TestVar
{
public static void main(String [] args)
{
int x;//应改为int x=0;
x=x+1; //这个x由于没有初始化,编译会报错。
System.out.println("x is "+x);
}
}
程序清单:Func1.java
public class Func1
{
public static void main(String [] args)
{
/* 下面是打印出第一个矩形的程序代码*/
for(int i=0;i<3;i++)
{
for(int j=0;j<5;j++)
{
System.out.print("*");
}
System.out.println(); //换行
}
System.out.println();
//下面是打印出第二个矩形的程序代码
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();
//下面是打印出第三个矩形的程序代码
for(int i=0;i<6;i++)
{
for(int j=0;j<10;j++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();
}
}
《Java就业培训教程》P39源码
程序清单:Func2.java
public class Func2
{
public static void drawRectangle(int x,int y)
{
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
System.out.print(" * ");
}
System.out.println(); //换行
}
System.out.println();
}
public static void main(String [] args)
{
drawRectangle(3,5);
drawRectangle(2,4);
drawRectangle(6,10);
}
}
《Java就业培训教程》P40源码
程序清单:Func3.java
public class Func3
{
public static int getArea(int x,int y)
{
return x*y;
}
public static void main(String [] args)
{
int area = getArea(3,5);
System.out.println("first Acreage is " + area);
System.out.println("second Acreage is "+ getArea(2,4));
getArea(6,10);
}
}
《Java就业培训教程》P41源码
程序清单:Func4.java
public class Func4
{
public static int getArea(int x,int y)
{
if(x<=0 || y<=0)
{
return -1;
}
return x*y;
}
public static void main(String [] args)
{
int area = getArea(3,5);
System.out.println("first Acreage is " + area);
System.out.println("second Acreage is "+ getArea(2,4));
getArea(6,10);
}
}
《Java就业培训教程》P43源码
程序清单:Test.java
public class Test
{
public static void main(String [] args)
{
int isum;
double fsum;
isum=add(3,5);
isum=add(3,5,6);
fsum=add(3.2,6.5);
}
public static int add(int x,int y)
{
reutrn x+y;
}
public static int add(int x,int y,int z)
{
return x+y+z;
}
public static double add(double x,double y)
{
return x+y;
}
}
《Java就业培训教程》P47源码
public class TestAnd
{
public static void main(String[] args)
{
int x=0;
int y=0;
if(x!=0 && y==y/x)
System.out.println("y = "+y);
}
}
《Java就业培训教程》P48源码
程序清单:ShiftTest.java
public class ShiftTest
{
public static void main(String [] args)
{
int x=0x80000000;
int y=0x80000000;
x=x>>1;
y=y>>>1;
System.out.println("0x80000000>>1 = " + Integer.toHexString(x));
System.out.println("0x80000000>>>1 = " + Integer.toHexString(y));
}
}
《Java就业培训教程》P61源码
程序清单:TestDo.java
public class TestDo
{
public static void main(String[] args)
{
int x=3;
while(x==0)
{
System.out.println("ok1");
x++;
}
int y=3;
do
{
System.out.println("ok2");
y++;
}
while(y==0);
}
}
《Java就业培训教程》P64源码
程序清单:PrintOddNum.java
public class PrintOddNum
{
public static void main(String [] args)
{
for(int i=0;i<10;i++)
{
if(i%2==0)
continue;
System.out.println(i);
}
}
}
《Java就业培训教程》P67源码
程序清单:TestArray.java
public class TestArray
{
public static void main(String [] args)
{
int x[];
x=new int[100];
for(int i=0;i<100;i++)
{
System.out.println("x"+i+" is "+x[i]);
}
}
}
《Java就业培训教程》P73源码
程序清单:TestArrayCopy.java
public class TestArrayCopy
{
public static void main(String [] args)
{
int ia[]=new int[]{1,2,3,4,5};
int ib[]=new int[]{9,8,7,6,5,4,3};
System.arraycopy(ia,0,ib,0,3);
// 复制源数组中从下标0开始的3个元素到目的数组,从下标0的位置开始存储。
for(int i=0;i<ia.length;i++)
System.out.print(ia[i]);
System.out.println();
for(int j=0;j<ib.length;j++)
System.out.print(ib[j]);
System.out.println();
}
}
参考资料: 选自Java视频教程[张孝祥]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询