Java期末考试和2020.9.17PTA错题

7-28 统计最大数出现次数 (20分)
编写程序读取一系列整数,找出它们的最大数,然后计算该数的出现次数,假定输入以0结束。

输入格式:
在一行中输入待统计的整型数,各个数可以以一个或多个空格或回车分隔,以0结束。

输出格式:
以两行输出: 第一行格式:The largest number is 最大数 第二行格式:The occurrence count of the largest number is 出现次数

输入样例:
在这里给出一组输入。例如:

3 5 2 5 5 5 0
输出样例:
在这里给出相应的输出。例如:

The largest number is 5
The occurrence count of the largest number is 4

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        int i;
        while (true) {
            i = sc.nextInt();
            if (i == 0) {
                break;
            } else {
                list.add(i);
            }
        }
        if (list.size() == 0) {
            System.out.println("The largest number is " + 0);
            System.out.println("The occurrence count of the largest number is " + 1);
            System.exit(0);
        }
        int sum=0;
        int max=list.get(0);
        for(int j=0;j<list.size();j++){
            if(list.get(j)==max){
                sum++;
            }else if(list.get(j)>max){
                max=list.get(j);
            }else{
                continue;
            }
        }
        System.out.println("The largest number is " + max);
        System.out.println("The occurrence count of the largest number is " + sum);
    }
}
//第一次的想法
//        Object arr[] = list.toArray();
//        int max = (int) arr[0];
//        int sum = 0;
//        for (int j = 0; j < arr.length; j++) {
//            if ((int) arr[j] > max) {
//                max = (int) arr[j];
//            } else if ((int) arr[j] == max) {
//                sum++;
//            } else {
//                continue;
//            }
//        }
[不一样的做法](https://blog.csdn.net/weixin_44609958/article/details/104815206)

7-2 jmu-Java-01入门-取数字 (2分)
本题目要求读入若干个代表整数的字符串,然后将其转化为整数。

如果该数>=10000且<=20000,则依次输出其对应的二进制字符串、八进制字符串、十六进制字符串。

否则将字符串中的每个数字抽取出来,然后将所有数字加总求和。

提示:参考jdk文档的Integer,

输入样例:
123
10000
-123
314159265
输出样例:
1 2 3 6
10011100010000,23420,2710
1 2 3 6
3 1 4 1 5 9 2 6 5 36


import java.util.Scanner;

import static java.lang.Integer.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int sum = 0;
            String s = sc.nextLine();
            int sz = Integer.parseInt(s);
            Integer ss = Integer.parseInt(s);
            if (ss >= 10000 && ss <= 20000) {
                String s1 = toBinaryString(ss);
                String s2 = toOctalString(ss);
                String s3 = toHexString(ss);
                System.out.println(s1 + "," + s2 + "," + s3);
            } else {
                char[] c = s.toCharArray();
                //如何去掉负号?向后移一位并++i;
                for (int i = 0; i < c.length; i++) {
                    if (c[0] == '-') {
                        c[0] = c[i + 1];
                        i++;
                    }
                    sum += c[i] - 48;
                    System.out.print(c[i] - '0' + " ");
                }
                System.out.println(sum);
            }
        }
    }
}

错题
1.Double.valueOf(“123”).doubleValue():将一个字符串转化成一个Double对象(Double是一个类),然后再调用这个对象的doubleValue()方法返回其对应的double数值。
用Double.parseDouble()是把括号里面内容变成double类型的。
2.A在这里插入图片描述
3.C,D不选
在这里插入图片描述
4.B
在这里插入图片描述
5.
在这里插入图片描述
6.C
在这里插入图片描述

两个实践出错。。。看错了
在这里插入图片描述
选c第三行
在这里插入图片描述
选D 3-1=2
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值