CodeGym 错题集 1 LEVEL 3-4 键盘录入,IDE,分支和循环

罗列和记录用 CodeGym 时做错的题, 虽然很多内容学过, 但还是有很多细微的点出问题

Level3 键盘录入, IDE

L3L6 转义字符(句子中有“\”和引号再输出)

显示以下文本(两行):这是一个 Windows 路径:"C:\Program Files\Java\jdk1.8.0_172\bin"这是一个 Java 字符串:\"C:\\Program Files\\Java\\jdk1.8.0_172\\bin\"

提示:\” – 在文本中的此位置插入双引号字符。\ – 在文本中的此位置插入反斜杠字符。

Requirements:

  • 该程序应输出文本。
  • 必须显示两行。
  • 第一行文本应为:这是一个 Windows 路径:“C:\Program Files\Java\jdk1.8.0_172\bin”
  • 第二行文本应为:这是一个 Java 字符串:“C:\Program Files\Java\jdk1.8.0_172\bin”
public class Solution {
    public static void main(String[] args) {
        //在此编写你的代码
        String s1 = "这是一个 Windows 路径:\"C:\\Program Files\\Java\\jdk1.8.0_172\\bin\"";
        String s2 = "这是一个 Java 字符串:\\\"C:\\\\Program Files\\\\Java\\\\jdk1.8.0_172\\\\bin\\\"";
        System.out.println(s1);
        System.out.println(s2);
    }
}

引号, 引号前加\

\,\前加\

除了开头结尾的引号不用

Level 4 分支和循环

L4L1 this的使用 1

考虑一下程序该怎么做。修复编程错误,以使 person.age 更改值。

**提示:**仔细检查 adjustAge 方法。

Requirements:

  • 该程序应在屏幕上显示文本。
  • Person 类应该包含一个名为 age 的 public int 字段。
  • Person 类的 adjustAge 方法应在屏幕上显示文本。
  • Person 类的 AdjustAge 方法只能包含一个名为 age 的 int 参数,并且该方法必须为 void。
  • main 方法只能调用一次 adjustAge 方法。
  • Person 类的 adjustAge 方法应将 Person 的 age 增加 20。
public class Solution {

    public static void main(String[] args) {
        Person person = new Person();
        System.out.println("年龄:" + person.age);
        person.adjustAge(person.age);
        System.out.println("调整后的年龄:" + person.age);
    }

    public static class Person {
        public int age = 20;

        public void adjustAge(int age) {
            this.age +=  20;
            System.out.println("adjustAge() 中的年龄为 " + this.age);
        }
    }
}

L4L1 this 的使用 2

计算苹果的总费用。苹果的总费用与 public static int applePrice 相对应。

Requirements:

  • 该程序应在屏幕上显示文本。
  • Apple 类的 addPrice 方法不应在屏幕上显示文本。
  • Apple 类的 applePrice 变量必须是初始化为零的 static int。
  • main 方法只能调用两次 addPrice 方法。
  • Apple 类的 addPrice 方法应将传入值加到苹果的价格上。
public class Solution {

    public static void main(String[] args) {
        Apple apple = new Apple();
        apple.addPrice(50);
        Apple apple2 = new Apple();
        apple2.addPrice(100);
        System.out.println("苹果的价格为 " + Apple.applePrice);
    }

    public static class Apple {
        public static int applePrice = 0;

        public static void addPrice(int applePrice) {
            //在此编写你的代码
           Apple.applePrice += applePrice;
        }
    }
}

static里的数量增加是Apple里非当前类的applePrice

L4L2 宇宙中的猫(静态初始块)

编写代码以正确对所创建的猫的数量进行计数 (count),并在屏幕上显示猫的正确数量。

Requirements:

  • 该程序应在屏幕上显示文本。
  • 不要更改负责屏幕输出的行。
  • Cat 类只能包含一个 count 变量。
  • Cat 类的变量 count 必须为 static int、包含一个 public 访问修饰符,并且被初始化为零。
  • main 方法只能有两个已被初始化的 Cat 变量。
  • 变量 count 必须存储已创建的 cat 对象的实际数量。
public class Solution {

    public static void main(String[] args) {
        Cat cat1 = new Cat();
        //在此编写你的代码
        Cat.count++;
        Cat cat2 = new Cat();
        //在此编写你的代码
        Cat.count++;
        System.out.println("猫的计数为 " + Cat.count);
    }

    public static class Cat {
        public static int count = 0;
    }
}

L4L6 四数最大

使用键盘输入四个数字,然后显示其中的最大值。如果最大值出现多次,则只显示一次

Requirements:

  • 程序应从键盘读取这些数字。
  • 程序必须在屏幕上显示一个数字。
  • 程序应显示四个数字中的最大值。
  • 如果有多个最大数字,则显示其中任意一个。
public class Solution {
    public static void main(String[] args) throws Exception {
        //在此编写你的代码
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt(reader.readLine());
        int b = Integer.parseInt(reader.readLine());
        int c = Integer.parseInt(reader.readLine());
        int d = Integer.parseInt(reader.readLine());
        System.out.println( Math.max(compare(a,b),compare(c,d)));
    }
    public static  int compare(int i, int j){
        return Math.max(i, j);
    }
}

L4L10 while 的乘法表

示例输出:1 2 3 4 5 6 7 8 9 102 4 6 8 10 12 14 16 18 203 6 9 12 15 18 21 24 27 304 8 12 16 20 24 28 32 36 405 10 15 20 25 30 35 40 45 506 12 18 24 30 36 42 48 54 607 14 21 28 35 42 49 56 63 708 16 24 32 40 48 56 64 72 809 18 27 36 45 54 63 72 81 9010 20 30 40 50 60 70 80 90 100

Requirements:

  • 程序不得从键盘读取文本。
  • 该程序应在屏幕上显示文本。
  • 程序应显示 10x10 乘法表。
  • 程序必须使用 while 循环。
public class Solution
{
    public static void main(String[] args) throws Exception
    {
        int i = 1;

        while (i < 11) {
            int j = 1;
            while (j < 11) {
                System.out.print(i * j);
                System.out.print(" ");
                j++;
            }
            System.out.println();
            i++;
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值