1、java实验二1)使用continue语句实现:将100300之间的不能被3整除的数输出。public class zhengchu public static void main(String args) for(int i=100;i=300;i+) if(i%3=0)continue; System.out.println(i); 2)使用break语句实现记数:从1100,当数到78时程序终止。public class BreakTest public static void main(String args) int i; for (i = 1; i = 100; +i) Syste
2、m.out.println(i); if (i = 78) break; 3)分别使用for语句编程,找出所有的水仙花数并输出。public class shuixianhuapublic static void main(String args) for(int i=1; i=9; i+) for(int j=0; j=9; j+) for(int k=0; k=9; k+) if(i*i*i+j*j*j+k*k*k = 100*i+10*j+k) System.out.print(i*100+j*10+k+ ); 4)编写一程序,判断2011年份是否是闰年,并在屏幕上输出判断结果。publ
3、ic class zhengchu public static void main(String args) if(2011%4!=0)System.out.print(No); else if(2011%400!=0&2011%100=0)System.out.print(No); else System.out.print(Yes); 三1)定义一个类MyValue,其中包括:用Value来保存一属性值;方法setValue设置Value,方法getValue获取Value,定义一个类UseValue,在该类的Main方法里面完成如下功能:创建一个MyValue类的对象MyValue;为M
4、yValue对象中的Value赋值10;使用getValue方法获得MyValue对象中的数据并在屏幕上输出。class MyValue private int value; public void setvalue(int x ) value=x; public int getValue() return value; public class UseValue public static void main(String args) MyValue MyV=new MyValue(); MyV.setvalue(10); System.out.println(MyV.getValue();
5、 2)编写Java代码实现一个计数器类Computer,其中包括:用CountValue来保存计数器的当前值。方法Computer(int a)是构造方法并给CountValue赋初值。方法increment()计数器加一方法decrement()计数器减一方法reset()计数器清零使用计数器类创建一对象,该计数器对象当前值为10,调用三次increment(),输出计数器当前值,调用一次decrement(),输出计数器当前值,调用reset(), 输出计数器当前值.public class Computer public int CountValue; Computer (int a)
6、CountValue=a; public void increment () CountValue+; public void decrement () CountValue-; public void reset () CountValue=0; public static void main(String args) Computer MyV=new Computer(10); MyV.increment(); MyV.increment(); MyV.increment(); System.out.println(MyV. CountValue); MyV.decrement (); S
7、ystem.out.println(MyV. CountValue); MyV. reset (); System.out.println(MyV. CountValue); 3)定义一个名字为MyRectangle的矩形类,类中有4个私有的整型成员变量,分别是矩形的左上角坐标(xUp,yUp)和右下角坐标(xDown,yDown);类中定义了无参数的构造方法和有4个int参数的构造方法,用来初始化类对象。类中还有以下方法:getW() 计算矩形的宽度;getH() 计算矩形的高度;area() 计算矩形的面积;toString() 把矩形的宽、高和面积等信息作为一个字符串返回。编写应用程序使
8、用MyRectangle类。class MyRectangle private int xUp,yUp,xDown,yDown; MyRectangle() xUp = 0; yUp = 0; xDown = 0; yDown = 0; MyRectangle(int x1,int y1,int x2,int y2 ) xUp = x1;yUp = y1;xDown = x2; yDown = y2; public int getW() return xDown - xUp; public int getH() return yDown - yUp; public int area() ret
9、urn getW() * getH(); public String toString() return 矩形宽: + getW() +矩形高: + getH() + 矩形面积:+area(); public class testpublic static void main(String args) MyRectangle rectangle = new MyRectangle(1,2,7,8); System.out.println(rectangle.toString(); 1)定义一个接口,接口中有四个抽象方法:求面积方法、求周长方法、显示面积方法及显示周长方法。定义Circle类和R
10、ectangle类分别实现接口,在主类中实现显示圆和矩形的面积和周长。interface J_Shape public abstract double mb_getArea(); public abstract double mb_getPerimeter();public class J_Circle implements J_Shape public double m_radius; public J_Circle(double r) m_radius=r; public double mb_getArea() return(Math.PI*m_radius*m_radius); publ
11、ic double mb_getPerimeter() return(Math.PI*2*m_radius); public static void main(String args) J_Shape a=new J_Circle(5); System.out.println(the area of circle is: + a.mb_getArea(); System.out.println(the perimeter of circle is + a.mb_getPerimeter(); interface J_Shape public abstract double mb_getArea
12、(); public abstract double mb_getPerimeter();public class Rectangle implements J_Shape public double longs,wides; public Rectangle(double l,double w) longs=l; wides=w; public double mb_getArea() return(longs*wides); public double mb_getPerimeter() return (2*(longs+wides); public static void main(Str
13、ing args) J_Shape a=new Rectangle(2,3); System.out.println(the area of rectangle is:+a.mb_getArea(); System.out.println(the perimeter of rectangle is:+a.mb_getPerimeter(); 2)重写上面的程序,要求矩型,圆的类放到不同的包中,用包的技术组织程序的设计。同时要求程序能从键盘上接受数据以便求解不同的几何图形的周长面积。提示:从键盘上输入双精度数的一种方法(程序片段)package Mytest; interface J_Shape
14、 public abstract double mb_getArea(); public abstract double mb_getPerimeter();public class J_Circle implements J_Shape public double m_radius; public J_Circle(double r) m_radius=r; public double mb_getArea() return(Math.PI*m_radius*m_radius); public double mb_getPerimeter() return(Math.PI*2*m_radiu
15、s); package Mypackage;interface J_Shape public abstract double mb_getArea(); public abstract double mb_getPerimeter();public class Rectangle implements J_Shape public double longs,wides; public Rectangle(double l,double w) longs=l; wides=w; public double mb_getArea() return(longs*wides); public doub
16、le mb_getPerimeter() return (2*(longs+wides); import Mypackage.Rectangle;import Mytest.J_Circle;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class J_test public static void main(String args)throws IOException BufferedReader input=new BufferedReader
17、(new InputStreamReader(System.in); System.out.print(请输入圆半径::); int number=Integer.parseInt(input.readLine(); J_Circle a=new J_Circle(number); System.out.println(the area of circle is:+a.mb_getArea(); System.out.println(the perimeter of circle is:+a.mb_getPerimeter(); BufferedReader input1=new Buffer
18、edReader(new InputStreamReader(System.in); System.out.print(请输入长方的一边:); int number1=Integer.parseInt(input.readLine(); System.out.print(请输入长方的另一边:); int number2=Integer.parseInt(input1.readLine(); Rectangle b=new Rectangle(number1,number2); System.out.println(the area of rectangle is:+b.mb_getArea()
19、; System.out.println(the perimeter of rectangle is:+b.mb_getPerimeter(); 1、编写一程序,随机生成5个不大于100的整数存入一数组,计算该数组的平均值,输出该5个整数及平均值。public class Average public static void main(String args) int intNumber; int i; intNumber = new int5; for ( i = 0 ; i intNumber.length ; i+) intNumberi = (int)(Math.random()*10
20、0); System.out.print( intNumberi + ); System.out.println( nThe Average is : +GetAverage(intNumber) ); static double GetAverage(int intNumber) int i,intTotal = 0; double dblResult; for ( i = 0 ; i =0;k-) buffer.append(s.charAt(k); System.out.println(原来为+s); System.out.println(现在为+buffer); 3、编写一程序,计算矩
21、阵A=7,9,4,5,6,8与矩阵B=9,5,2,8,5,9,7,2,4,7,5,8相乘,把结果存入矩阵C,并在屏幕输出结果。public class MatrixMultiply public static void main( String args ) int intMatrixA = 7,9,4 , 5,6,8 ; int intMatrixB = 9,5,2,8 , 5,9,7,2 , 4,7,5,8 ; int intMatrixC = new int24; Multiply( intMatrixA , intMatrixB , intMatrixC ); System.out.p
22、rintln(n* Matrix A *); OutputMatrix( intMatrixA ); System.out.println(n* Matrix B *); OutputMatrix( intMatrixB ); System.out.println(n* Matrix C *); OutputMatrix( intMatrixC ); static void Multiply(int intMatrixA , int intMatrixB , int intMatrixC ) int i,j,k; for( i = 0 ; i intMatrixA.length ; i+ )
23、for( j = 0; j intMatrixBi.length ; j+ ) intMatrixCij = 0; for( k = 0; k intMatrixB.length ; k+ ) intMatrixCij += intMatrixAik * intMatrixBkj; static void OutputMatrix(int intMatrix) int i,j; for ( i = 0 ; i intMatrix.length ; i+) for ( j = 0 ; j intMatrixi.length ; j+) System.out.print( intMatrixij
24、+ ); System.out.println(); 1、编写程序,该程序将整型变量n=20、实型变量b=3.14和字符型变量c=a显示在屏幕上。public class Welcome public static void main(String args) int n; n=3; System.out.println(n); float b; b=3.14f; System.out.println(b); char c; c=a; System.out.println(c); 2、编写程序,该程序实现打开一个文本文件,每次读取一行内容,将每行作为一个字符串读入,并将字符串输出显示到屏幕上。
25、import java.io.*;public class Printlnt public static void main(String args) throws IOException File filePath = new File(D:123.txt); BufferedReader br; String s = null; try br = new BufferedReader(new FileReader(filePath); while (s = br.readLine() != null) System.out.println(s + rn); catch (FileNotFo
26、undException e) e.printStackTrace(); 3、编写一个程序,在控制台中窗口中提示输入两个整数,然后接收这两个整数,并输出它们的和。下面是运行过程的示例:请输入第一个整数:45请输入第二个整数:23计算结果:45+23=68上面的两个黑斜体整数是用键盘输入的,其余字符是程序输出的。import java.util.Scanner;public class PrintInt public static void main(String args) String a,b; Scanner scan = new Scanner(System.in); System.out.print(输入第一个整数 : ); a = scan.next(); System.out.print(输入第二个整数 :); b = scan.next(); try int c=(Integer.parseInt(a); int d=(Integer.parseInt(b); int e; e=c+d; System.out.println(计算结果:+c+d+=+e); catch(NumberFormatException e) System.out.println(请输入整数!);1) 运用布局管理器,
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1