It will be first post. The author is an amateur who started programming in April. This article was written as a memorandum, but I would appreciate it if you could tell me strictly!
I ended up writing this kind of code when I was training.
samp0.java
import java.util.Arrays;
public class MyJava {
public static void main(String[] args) {
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
//Evenness judgment of the i-th element of a
if (a[i] % 2 == 0) {
b[i] = 0;
} else {
b[i] = 1;
}
}
//Although it deviates from the main subject, it is convenient to remember the printf method
// Arrays.toString(array)Is convenient because you don't have to turn for when you want to output an array.
System.out.printf("The contents of the b array%s.\n",
Arrays.toString(b));
}
}
It's unpleasant that the layer gets deeper with this kind of processing ... When I was looking into it like that ...
that? If this is in the if statement, you should continue, right? else sentence will decrease! So here's the code:
samp1.java
import java.util.Arrays;
public class MyJava {
public static void main(String[] args) {
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
//Evenness judgment of the i-th element of a
//Substitute 0 for even numbers and 1 for odd numbers
if (a[i] % 2 == 0) {
b[i] = 0;
continue;
}
b[i] = 1;
}
System.out.printf("The contents of the b array%s.\n",
Arrays.toString(b));
}
}
I was able to do it, but this time the judgment when it is an odd number is difficult to read ...
Hmm! ?? this is,,,! ??
hoge ? fuga : foo
What is this?
When hoge is True, fuga is executed, and when false, foo is executed.
I see! You don't have to make strange layers! Alright!
samp2.java
import java.util.Arrays;
public class MyJava {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = a[i] % 2 == 0 ? 0 : 1;
}
System.out.printf("The contents of the b array%s.\n",
Arrays.toString(b));
}
}
Hmm, clean!
It's my first post, so I'm sorry for the poor text. I'd like to keep a record of what I've learned little by little, so I'd appreciate it if you could say, "It's easier to read!" Thank you for reading this far.
Java way assignment operators and other operators (http://www.javaroad.jp/java_operator6.htm)
Recommended Posts