CodeGym学生最好的朋友是编程任务、Java 和任务验证器。尽管如此,每个 padawan 开发人员都必须开始走出人迹罕至的道路,发明自己的迷你项目,并为面试做好准备。人们会认为面试中的实际 Java 任务与本课程中的任务完全相同。在大多数情况下都是如此,但有些公司喜欢问一些技巧性的问题或一些不熟悉的问题。为避免在紧张的面试中紧张不安,尝试在家中自己解决类似的 Java 任务会很有帮助。
在本文中,我们将探索 5 个这样的棘手任务。我们建议您先阅读条件并尝试自己解决。还有一件事:不要忘记每天完成本课程中的 Java 任务!

任务 1:从头开始创建无限循环
提供了一个代码块。添加到它使循环无限。
class ToInfinity {
public static void main(String[] args) {
// Insert code here
for (int i = start; i <= start + 1; i++) {
/* This should be an infinite loop, but don't change anything here */
}
}
}
“这不难, ”你说。您很可能多次发现自己处于这种情况:在完成 Java 任务时,您创建了一个无限循环并且不得不考虑如何摆脱它。好吧,这是相反的方式。这是棘手的部分:您不能更改循环本身或其退出条件。只有两次迭代。但是,它们足以创建一个无限循环。看起来它应该只执行两次迭代,但您可以使用溢出使其无限。你已经猜到怎么做了吗?
解决方案
由于溢出,是Java 中Integer.MAX_VALUE
an 可以存储的最大值。int
如果您达到Integer.MAX_VALUE
并增加该值,您将回滚到Integer.MIN_VALUE
,或者更确切地说,回滚到可能的最低Integer
值。int
因此,要完成此 Java 任务,我们只需将变量的最大值减 1 即可start
。此任务的 Java 代码:
class ToInfinity {
public static void main(String[] args) {
int start = Integer.MAX_VALUE - 1;
for (int i = start; i <= start + 1; i++) {
// Infinite loop
System.out.println(i); // Be sure we are in an infinite loop
}
}
}
会发生什么?我们从 start=2147483645 (Integer.MAX_VALUE-1) 开始。在下一次迭代中,该值变为 2147483645、2147483646、-2147483648、-2147483647...等等。
任务 2:创建可执行注释
好吧,我们到了!从第一节课开始,我们就听说评论不会被执行。这就是它们被称为评论的原因。我们认为,对于 Java 程序员,即使是有经验的程序员来说,这个任务的解决方案并不总是显而易见的。然而,有一种棘手(但“合法”)的方法可以让 JVM 执行注释。你能感觉到我们要去哪里吗?猜猜看!解决方案
此任务的 Java 代码:
public class ExecutableComment {
public static void main(String[] args) {
// The comment below will be executed!
// \u000d System.out.println("executable comment");
}
}
如果我们将这段 Java 代码输入到 IDE 中,我们将得到以下结果:
executable comment
这是因为 Java 编译器将 Unicode 字符解释\u000d
为换行,并像这样读取我们的代码:此任务的 Java 代码,正如编译器所解释的那样:
public class ExecutableComment {
public static void main(String[] args) {
// The comment below will be executed!
// \u000d
System.out.println("comment executed");
}
}
任务 3:创建命名循环
这是题为“球形奶牛的实用 Java 编程问题”系列的另一个成员。从某种意义上说,它甚至不清楚为什么这是必要的:循环不太可能因为它没有被赋予名称而受到冒犯。无论如何,它之所以重要还有另一个原因:该语言允许您命名一个循环。解决方案
注意:这些“名称”被某些人称为“标签”,不建议在实践中使用。用于演示命名循环的 Java 代码
public class NamedLoop {
public static void main(String[] args) {
loop1:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 3)
break loop1;
System.out.println("i = " + i + " j = " + j);
}
}
}
}
如果您运行该程序,将输出以下内容:
i = 0 j = 0
i = 0 j = 1
i = 0 j = 2
i = 0 j = 3
i = 0 j = 4
i = 1 j = 0
i = 1 j = 1
i = 1 j = 2
i = 1 j = 3
i = 1 j = 4
i = 2 j = 0
i = 2 j = 1
i = 2 j = 2
i = 2 j = 3
i = 2 j = 4
您还可以使用关键字 continue 返回到命名循环的开头。如有必要,您可以在嵌套循环中使用( break
or continue
)来使用. 这将有助于避免创建大量标志并在语句中检查它们以确定是继续还是退出内部循环。 if-else
for
if-else
if-else
任务 4:在整数数组中查找一个重复项
给你一个整数数组(或一个ArrayList
,如果你愿意的话),其中包含Integer
从 1 到 100 的每一个元素。该数组有一个且只有一个重复元素。你是怎么找到它的?与前三种相比,Java 程序员更容易获得此类任务。因为它是关于逻辑的,而不是你对语言中很少使用的微妙之处的了解。当您的大脑启动或您意识到“我是一名程序员。我很聪明”时,您使用蛮力的第一个肆无忌惮的冲动会很快消失。唯一的问题是,在面试期间,当你处于压力之下时,这可能不会发生。因此,在查看解决方案之前请先思考一下!
这是算法
计算从 1 到 100 的所有数字的总和。我们认为您知道如何做到这一点(例如,使用高斯的著名方法)。现在计算数组或 中元素的总和ArrayList
。并且...从第二个总和中减去第一个总和。答对了!结果数字是重复元素的值。 此任务的 Java 代码使用ArrayList
.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class FindDuplicate {
private static void findDuplicate(List<Integer> elements) {
int distinctSum = elements.stream().distinct().mapToInt(e -> e).sum();
// Find the sum of all list elements
int totalSum = elements.stream().mapToInt(e -> e).sum();
System.out.println("The repeated element is: " + (totalSum - distinctSum));
}
public static void main(String[] args) {
// Create a list of sequential elements in the interval [1..101).
List <Integer> elements = IntStream.range(1, 101).boxed().collect(Collectors.toList());
// Add the value 23 at index 53
elements.set(53, 23);
findDuplicate(elements);
}
}
另一种解决方案
import java.util.List;
import java.util.ArrayList;
public class Duplicate {
public int findDuplicateNumber(List<Integer> numbers) {
int highestNumber = numbers.size() - 1;
int total = getSum(numbers);
int duplicate = total - (highestNumber * (highestNumber + 1) / 2);
return duplicate;
}
public int getSum(List<Integer> numbers) {
int sum = 0;
for (int num : numbers) {
sum = sum + num;
}
return sum;
}
public static void main(String a[]) {
List <Integer> numbers = new ArrayList <Integer>();
for (int i = 1; i < 100; i++) {
numbers.add(i);
}
// Add a duplicate to the list
numbers.add(25);
Duplicate dn = new Duplicate();
System.out.println("The repeated element is: " + dn.findDuplicateNumber(numbers));
}
}
任务 5:在整数数组中查找非唯一重复项
如果上一个任务对你来说太简单了,试试这个:给你一个从 1 到 100 的整数列表。其中有重复项(不止一个)。你如何找到出现多次的元素(找到元素并指出它们出现了多少次)?解决方案
这里合乎逻辑的解决方案是使用类似 HashMap 的东西,因为它将数据存储在键值对中。 此任务的 Java 代码:
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class SomeDuplicates {
private static void findDuplicates(List<Integer> elements) {
HashMap <Integer, Integer> duplicates = new HashMap <>();
// Use the following rule to populate the Map duplicates:
// the key is the element value, and the value is the number of times it occurs
elements.forEach(e -> duplicates.put(e, duplicates.get(e) == null ? 1 : duplicates.get(e) + 1));
// Remove any elements that don't occur more than once from the Map duplicates
// and store the result as a list (to make it easy to work with in the next step)
List <Map.Entry <Integer, Integer> >
result = duplicates.entrySet().stream().filter(d -> d.getValue() > 1).collect(Collectors.toList());
// Write the results for all elements to the result list
result.forEach(e -> System.out.println(String.format("Element %d occurs %d times", e.getKey(), e.getValue())));
}
public static void main(String[] args) {
List <Integer> elements = IntStream.range(1, 101).boxed().collect(Collectors.toList());
elements.set(97, 23);
elements.set(27, 51);
elements.set(99, 23);
findDuplicates(elements);
}
}
GO TO FULL VERSION