Java 打印出奇数 0-100,不包括 X、Y、Z
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20314818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print out odd numbers 0-100, excluding X, Y, Z
提问by Littlev
This is obviously a "Homework style" task, but I have issues with grasping how I can exclude a certain set of numbers. I understand how I can write out the odd numbers from 0-100, but I fail to comprehend any answers I've found, or tried to find, about how to exclude pre-set numbers of choice.
这显然是一项“家庭作业风格”任务,但我在掌握如何排除特定数字集方面存在问题。我知道如何写出 0-100 的奇数,但我无法理解我找到或试图找到的关于如何排除预设数字的任何答案。
Basically, what I want to happen is the following: The program writes all odd numbers from 1-100. Lets say I don't want the numbers 5,9 or 11 to be displayed, but I want my program to continue, how do I make that happen?
基本上,我想要发生的事情如下:程序写入 1-100 中的所有奇数。假设我不希望显示数字 5,9 或 11,但我希望我的程序继续,我该如何实现?
I have two different sets of code (I don't know which is easier to use in this context, so I'm including both of them):
我有两组不同的代码(我不知道在这种情况下哪个更容易使用,所以我将它们都包括在内):
for (int i=0; i<100; i++)
if (i % 2 !=0)
System.out.println(i)
and
和
for (int i=1; i<100; i=i+2)
System.out.println(i)
采纳答案by mb21
for (int i=0; i<100; i++) {
if ( (i % 2 !=0) && i!=5 && i!=9 && i!=11) {
System.out.println(i)
}
}
It's bad practice to write it without the brackets since it's too easy to make an error when adding more lines later on. Also, you're probably writing Java and not JavaScript.
不带括号编写它是不好的做法,因为以后添加更多行时很容易出错。此外,您可能正在编写 Java 而不是 JavaScript。
回答by Mik378
(with JDK 7)
(使用 JDK 7)
Set<Integer> exclusions = new HashSet<>(Arrays.asList(5, 9, 11)); //syntactic sugar from JDK 7
for (int i=0; i<100; i++) {
if(i % 2 == 0 || exclusions.contains(i)) // if pair OR in exclusions list, isn't interesting
continue; // continue allows to say: "isn't interesting, take me to the next value
System.out.println(i); // Oh yeah, this one is interesting! It's an odd! print it!
}
(with JDK 5/6)
(使用 JDK 5/6)
Set<Integer> exclusions = new HashSet<Integer>(Arrays.asList(5, 9, 11));
for (int i=0; i<100; i++) {
if(i % 2 == 0 || exclusions.contains(i))
continue;
System.out.println(i);
}
of course, there is other ways to achieve the tasks, but I enjoy this one.
当然,还有其他方法可以完成任务,但我喜欢这个。
Besides, the use of Arrays.asList
is a good tips to [initialize the
Setinline][1]
.
此外,使用SetArrays.asList
是一个很好的技巧。[initialize the
inline][1]
It replaces this traditional boilerplate code:
它取代了这个传统的样板代码:
Set<Integer> exclusions = new HashSet<Integer>();
exclusions.add(5);
exclusions.add(9);
exclusions.add(11);
回答by Deximat
If you have constant numbers to exclude it is just enough to do it with ifs
如果您有常量数字要排除,则只需使用 ifs 即可
for (int i=1; i<100; i=i+2) {
if ( i != 1 && i !=5 && i != 10 ) {
System.out.println(i);
}
}
But if you are having some list of numbers, which are read from input or something, ifs will just fail to do it. What you need is set of numbers to exclude and Java has a lot of implementations of set which are also efficient in checking does some set contains given element. So you would do something like this:
但是,如果您有一些从输入或其他内容中读取的数字列表,则 ifs 将无法执行此操作。您需要的是要排除的数字集,Java 有很多 set 的实现,它们在检查某些集合是否包含给定元素方面也很有效。所以你会做这样的事情:
// you can use hashset
Set<Integer> setOfElementsToExclude = new HashSet<Integer>();
// add as many elements as you want from input or manually
setOfElementsToExclude.add(1);
setOfElementsToExclude.add(5);
setOfElementsToExclude.add(10);
for (int i=1; i<100; i=i+2) {
if ( !setOfElementsToExclude.contains(i) ) {
// so print only the numbers not present in exclusion set
System.out.println(i);
}
}
If you want you can read about set here: http://docs.oracle.com/javase/7/docs/api/java/util/Set.html
如果您愿意,可以在此处阅读有关设置的信息:http: //docs.oracle.com/javase/7/docs/api/java/util/Set.html
回答by Fr0z3n7
StringBuilder sb = new StringBuilder();
Set<Integer> set = new HashSet<>();
set.add(5);
set.add(9);
set.add(11);
for(int i = 0 ; i <= 100 ; i++) {
if(!set.contains(i) && i%2 != 0) {
sb.append(i).append(" ");
}
}
System.out.println(sb.toString());