Break
package sample13_break;
public class MainClass {
public static void main(String[] args) {
for(int i=0; i<10; i++) {
System.out.println("for loop " + i);
if(i == 5) {
System.out.println("i == 5, 탈출!");
break;
}
}
int number[] = { 13, 45, 27, -36, 78 };
for(int i=0; i<number.length; i++) {
System.out.println("number[" + i + "] = " + number[i]);
if(number[i] < 0) {
break;
}
}
boolean b = false;
for(int i=0; i<10; i++) {
System.out.println("i = " + i);
for(int j=0; j<8; j++) {
System.out.println("/tj = " + j);
if(i == 5 && j ==4) {
b = true;
}
if(b == true) {
break;
}
}
if(b == true) {
break;
}
}
out : for(int i=0; i<10; i++) {
System.out.println("i = " + i);
for(int j=0; j<8; j++) {
System.out.println("/tj = " + j);
if(i == 5 && j ==4) {
break out;
}
}
}
}
}
Continue
package sample14_continue;
import java.util.Scanner;
import java.util.Arrays;
public class MainClass {
public static void main(String[] args) {
for(int i =0; i<10; i++) {
System.out.println("i = " + i);
System.out.println("for 처리1");
if(i>5) {
continue;
}
System.out.println("for 처리2");
}
int array[] = { 2, -6, 3, -4, 5 };
for(int i=0; i<array.length; i++) {
if(array[i] <= 0 ) {
continue;
}
System.out.printf("arry[%s] = %s %n", i, array[i]);
}
Scanner sc = new Scanner(System.in);
int number;
int numArr[] = new int[5];
int num, w1;
w1=0;
while(w1<numArr.length) {
System.out.print( (w1+1) +"번째 수 = ");
num = sc.nextInt();
if(num <= 0) {
System.out.println("양수만 입력하시오.");
continue;
}
System.out.println("/tnumArr[" + w1 + "] = " + num);
numArr[w1] = num;
w1++;
}
System.out.println( Arrays.toString(numArr));
}
}
과제 #2
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
int num=0;
for(int i=1; i<1001; i++ ) {
num=num+i;
}
System.out.println("1부터 1000 사이 수의 합은 : " + num);
int sum = 0;
int n = 0;
for(int i=0; i<1000; i++) {
n++;
sum = sum + n;
}
System.out.println("합계 : " + sum);
}
}
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("숫자를 입력하시오. /n >>");
int num2 = sc.nextInt();
System.out.println("number : " + num2);
for(int i = 0; i < 101; i = i+10) {
if(num2 >= i && num2 < i+10) {
System.out.printf("입력한 수는 %s 보다 크고 %s 보다 작다.", i, i+10);
}
}
int boundNum = 0;
for(int i = 0; i < 10; i++) {
boundNum = 10 * i;
if(num2 > boundNum && num2 <= (boundNum + 10)) {
System.out.println("number는 " + boundNum + "보다 크고" + (boundNum+10)+ "보다 작거나 같다");
}
}
}
}
public class MainClass {
public static void main(String[] args) {
for(int i=0; i<9; i++) {
for(int j=0; j<5; j++) {
System.out.print("*");
}
System.out.println("/tLine number" + (i+1));
}
}
}
public class MainClass {
public static void main(String[] args) {
int t = 0;
for(int i=0; i<9; i++) {
if(i<5) t++;
else t--;
for(int j=0; j<t; j++) {
System.out.print("*");
}
if(i==8) {
continue;
}
System.out.println();
}
}
}
public class MainClass {
public static void main(String[] args) {
int ans;
for(int i=1; i < 10; i++) {
for(int j=1; j<10; j++) {
ans = i * j;
System.out.printf("%s * %s = %s/t", i, j, ans);
}
System.out.println();
}
System.out.println();
}
}
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b;
System.out.print("출력하고 싶은 구구단을 입력하시오. : ");
a = sc.nextInt();
for(int i=1; i < 10; i++) {
b = a * i;
System.out.printf("%s x %s = %s /n", a, i, b);
}
}
}
public class MainClass {
public static void main(String[] args) {
int a=0, b=0;
for(int i=1; i<101; i++ ) {
if (i % 2 == 0) {
a = a + i;
}
else if(i % 2 ==1) {
b = b + i;
}
}
System.out.println("1부터 1000 사이 짝수의 합은 : " + a);
System.out.println("1부터 1000 사이 홀수의 합은 : " + b);
}
}