- 编写一段程序,要求打印出一段时时间内的闰年年份(while循环)
public static void main(String agrs[]) {
int i=2010;
while (i<2020){
if ((i% 4 == 0 && i % 100 != 0) ||i % 400 == 0)
System.out.println(i+"是闰年");
else
System.out.println(i+"不是闰年");
i=i+1;
}
- 按照1+1+2+3+5=12的格式打印斐波那契数列(f(n)=f(n-1)+f(n-2))前8项,并求和
final int N = 8;
int sum = 0;
int[] nums = new int[N];
nums[0] = 0;
nums[1] = 1;
for (int i = 2; i < nums.length; i++) {
nums[i] = nums[i-1] + nums[i-2];
}
System.out.println("打印结果:");
for (int i = 0; i < nums.length-1; i++) {
System.out.print(nums[i] + "+");
sum += nums[i];
}
System.out.print(nums[7]+"=");
System.out.printf("%d\t",sum);
- 写一段程序,将二维数组a = {{3,0,6,4,2},{5,8,9,7,8}}行与列的数据交换,并打印输出
int i,j;
int[][]a={
{3,0,6,4,2},
{5,8,9,7,8}
};
int[][]b=new int[5][2];
System.out.println("原数组");
for(i=0;i<2;i++){
for(j=0;j<5;j++)
{
System.out.print(a[i][j]+"\t");
b[j][i]=a[i][j];
}
System.out.println();
}
System.out.println("转换后:");
for(i=0;i<5;i++){
for(j=0;j<2;j++){
System.out.print(b[i][j]+"\t");
}
System.out.println();
}
- 编写一段程序,检查一个二维数组是否对称,(即对所有i和j都有a[i][j]=a[j][i])
int arr[] = {1,2,2,1,};
boolean flag = false;
for(int i=0; i < arr.length/2; i++){
if(arr[i] == arr[arr.length -1-i]){
flag = true;
}else
flag = false;
}
//输出结果
if(flag)
System.out.println("对称");
else
System.out.println("不对称");
}
- 编写一段程序,打印7层实心的菱形。【使用数组实现】
*
***
*****
*******
*****
***
*
for (int a=1,b=3,c=1;a<=4 && b>=0 && c<=7;a++,b--,c=c+2){
for(int d=b;d>=1;d--){
System.out.print(' ');
}
for(int e=1;e<=c;e++){
System.out.print('*');
}
System.out.println();
}
for (int a=1,b=5;a<=3 && b>=1;a++,b=b-2){
for(int c=1;c<=a;c++){
System.out.print(' ');
}
for(int d=b;d>=1;d--){
System.out.print('*');
}
System.out.println();
}
- 电脑computer类
具有属性品牌(brand)、种类type(台式机、笔记本、pad)
具有开机open、工作work和关机close的方法
电脑开机输出“【brand】【type】启动。。。”。
电脑工作输出“【brand】【type】工作中。。。”
电脑关机输出“【brand】【type】关闭。。。”
电脑computer类创建时自动输出“这是一台【brand】【type】电脑。”
请 创建一个联想台式机和一个DELL笔记本电脑实例,并完成下列操作
联想台式机启动,
联想台式机工作
DELL笔记本电脑启动
联想台式机关机
DELL笔记本电脑工作
Public class Computer{
String brand,type;
public void open(){
System.out.println(brand+type+“启动”);
}
Public void work(){
System.out.println(brand+type+”工作中”);
}
Public void close()
{System.out.println(brand+type+”关闭”);
}
System.out.println(“这是一台”+brand+type+”电脑”);
}
public static void main(String agrs[]){
Computer lenovo=new Computer;
lenovo.open();
lenovo.work();
Computer dell=new Computer;
dell.open();
lenovo.close();
Dell.work();
}
- 写出下列程序的执行结果
class FatherClass{
public FatherClass() {
System.out.println("FatherClass Create;");
}
}
public class Test extends FatherClass{
public Test() {
System.out.println("ChildClass Create;");
}
public static void main(String[] args) {
FatherClass fc = new FatherClass();
Test cc = new Test();
}
}
FatherClass Create;
FatherClass Create;
ChildClass Create;
- 写出下列程序的运行结果
public class Test{
public static void main(String agrs[]) {
String[] arr = new String[3];
try {
System.out.print("arr[3]=" + arr[3]);
} catch (Exception ae) {
System.out.println("警告:出现异常!");
} finally {
System.out.println("处理数组异常!");
}
try {
System.out.print("arr[3]=" + arr[3]);
} catch (NullPointerException e) {
System.out.println("警告:出现空指针异常!");
} finally {
System.out.println("处理空指针异常!");
}
}
}
警告:出现异常!
处理数组异常!
处理空指针异常!