728x90
1. ํฅ์๋ loop
public class App {
public static void main(String[] args) throws Exception {
//[1] Input : n๋ช
์ ๊ตญ์ด ์ ์
int[] scores = {100, 75, 3 ,5 ,3, 22, 34};
int sum = 0;
//[2] Process
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
}
//[3] Output
System.out.println(sum);
int sum1 = 0;
for (int x : scores) {
sum1 += x;
}
System.out.println(sum1);
}
}
int[] scores = {100, 75, 3 ,5 ,3, 22, 34};
int sum = 0;
//[2] Process
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
}
int sum1 = 0;
for (int x : scores) { //x๊ฐ ๊ณง scores[์์๋๋ก] ์ ๊ฐ์ด ๋๋... ๋๋จํ ๊ฐํธํ ์๋ฐ์!
sum1 += x;
}
2. Arrays.fill
import java.util.Arrays;
public class App {
public static void main(String[] args) throws Exception {
int[] scores = new int[5];
for(int x : scores){
System.out.println(x);
}
Arrays.fill(scores,100);
for(int x : scores){
System.out.println(x);
}
}
}
์ถ๋ ฅ๊ฒฐ๊ณผ
0
0
0
0
0
100
100
100
100
100
3. Arrays.equal
import java.util.Arrays;
public class App {
public static void main(String[] args) throws Exception {
int[] englishScores = new int[5];
int[] mathScores = {34,45,56,22,18};
int[] artScores = {34,45,56,22,18};
System.out.println( Arrays.equals(mathScores,artScores)); // true
System.out.println( Arrays.equals(mathScores,englishScores)); // false
}
}
4. Arrays.sort
import java.util.Arrays;
public class App {
public static void main(String[] args) throws Exception {
int[] mathScores = {34,45,56,22,18};
Arrays.sort(mathScores);
for(int x : mathScores){
System.out.println(x); // 18 22 34 45 56
}
}
}
728x90
'Java & Spring > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Tomcat] 8080 ํฌํธ ๊ฐ์ ์ข ๋ฃ (1) | 2022.12.25 |
---|---|
[Java] ์ธํฐํ์ด์ค (0) | 2022.12.14 |
[Java] ์๋ฐ List ์ธํฐํ์ด์ค ๊ตฌํ ArrayList vs LinkedList (0) | 2022.12.09 |
[Java] ์๋ฐ List.of()๋ก ๋ง๋ ๋ถ๋ณ ๋ฆฌ์คํธ๋ฅผ ๊ฐ๋ณ ๋ฆฌ์คํธ๋ก ๋ฐ๊พธ๊ธฐ (0) | 2022.12.09 |
[Java] ๊ฐ์ฒด๋ ์์์ ์ต์์ ์์ (0) | 2022.11.23 |