java刷题day 06

一. 单选题:

 解析:最终类也叫密封类,是被final修饰的类,不能被继承

 解析:

A:6入,5 入,5出,4入,4出,3入,3出,6出,2入,2出,1入,1出

B:654入,4出,5出,3入,3出,21入,1出,2出,6出

D:65432入,2出,3出,4出,1入,1出,5出,6出

 解析:静态代码块优先执行,再执行构造方法,先有父类再有子类

  解析:10+10+10=30

不要在finally中加return,如果加了,try里的return会失效

 解析:

二、不定项选择

 解析:

A,B:abstract 和final不能同时出现

C:抽象方法不能有具体方法实现

C:构造方法可以重载

D:子类中可以使用super来调用父类构造方法

A:八进制

C:布尔类型不能和int型强转

E:超出范围:-128~127

三、编程题

不要二__牛客网 (nowcoder.com)

 

import java.util.*;
public class Main{
public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int w = sc.nextInt();
        int h = sc.nextInt();
        int[][] array = new int[w][h];
        int count = 0;
        for(int i = 0;i < w; i++){
            for(int j = 0;j < h; j++){
                if(array[i][j] == 0){
                    count++;
                    if(i+2 < w){
                        array[i+2][j] = 1;
                    }
                    if(j+2 < h){
                        array[i][j+2] = 1;
                    }
                }
            }
        }
        System.out.println(count);
    }
}

把字符串转换成整数__牛客网 (nowcoder.com)

 

public class Solution {
    public int StrToInt(String str) {
        //如果字符串为空,直接返回0
        if(str==null || str.length()==0){
            return 0;
        }
        int len=str.length();
        int status=1;//保留字符串的正负性
        int end=0;//截至位
        int res=0;//保存返回值
        int index =1; 
        if(str.charAt(0)=='+'){//若第一位为符号位,令截止位为1,且status=1
            end=1;
        }else if(str.charAt(0)=='-'){
            status=-1;
            end=1;
        }
        for(int i=len-1; i>=end; i--){//倒着遍历
            int temp=str.charAt(i)-'0';
            if(temp>=0 && temp<=9){//如果是数字则加入res
                res+=temp*index;
                index*=10;
            }else{//不是数字直接结束,返回0
                return 0;
            }
        }
        return res*status;//成功转换为数字,乘上符号位。
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值