반응형
문제
2중 중첩된 for문을 이용하여 구구단을 출력하시오.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class gugudan { | |
public static void main(String[] args) { | |
int ggdan; | |
int num; | |
for (ggdan = 1; ggdan <=9; ggdan++) | |
{ | |
for (num = 2; num <=9; num++) | |
{ | |
System.out.print(num + "*" + ggdan + " = " + ggdan * num); | |
System.out.print("\t"); | |
} | |
System.out.println(); | |
} | |
} | |
} |
출력결과
문제
5개의 정수를 입력받고 양수 합을 구하여 출력하는 프로그램을 작성하라
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class sumEx { | |
public static void main(String[] args) { | |
int num = 0; | |
Scanner scanner = new Scanner(System.in); | |
int sum = 0; | |
for(;num<5;num++){ | |
System.out.print("정수를 입력: "); | |
int n=scanner.nextInt(); | |
if(n<=0){ | |
continue; | |
}else{ | |
sum+= n; | |
} | |
} | |
System.out.println("양수의 합은 " + sum + "입니다."); | |
scanner.close(); | |
} | |
} |
출력결과
문제
양수 5개를 입력 받아 배열에 저장하고, 가장 큰 수를 출력하는 프로그램을 작성하라
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import org.omg.CORBA.SystemException; | |
public class Biger_number_02 { | |
public static void main(String[] args) { | |
int[] n = new int[5]; | |
int max = 0; | |
Scanner scanner = new Scanner(System.in); | |
for(int i = 0;i < 5; i++){ | |
System.out.print("정수 입력: "); | |
n[i] = scanner.nextInt(); | |
if(i == 0){ | |
max = n[i]; | |
} | |
if(max < n[i]){ | |
max = n[i]; | |
} | |
} | |
System.out.println("최대값" + max + "입니다."); | |
} | |
} |
출력결과
문제
2차원 배열에 학년별로 1, 2학기 성적을 저장하고, 4년 전체 평점 평균을 출력하라
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class score { | |
public static void main(String[] args) { | |
double[][] score = { | |
{4.5, 4.2}, | |
{2.5, 3.5}, | |
{3.5, 1.5}, | |
{3.2, 2.5} | |
}; | |
System.out.print("\t\t"); | |
for(int i = 0; i < 2; i++){ | |
System.out.print((i+1) + "학기 학점" + "\t\t"); | |
} | |
for(int i = 0; i < 4; i++){ | |
System.out.println(); | |
System.out.print((i+1) + "년"); | |
System.out.print("\t"); | |
for(int j=0; j<2; j++){ | |
System.out.print("\t"); | |
System.out.print(score[i][j]); | |
System.out.print("\t"); | |
System.out.print("\t"); | |
} | |
} | |
System.out.println(); | |
double temp = 0; | |
for(int i = 0; i < 2; i++){ | |
for(int j = 0; j < 1; j++){ | |
temp =+ score[j][i]; | |
} | |
} | |
System.out.println(); | |
System.out.print("전체평균: "); | |
System.out.print(temp/8); | |
} | |
} |
반응형
'공부 > 개발노트' 카테고리의 다른 글
[C/C++] 유클리드의 최대공약수 알고리즘 (0) | 2016.09.01 |
---|---|
[C/C++] 재귀 함수를 활용한 알고리즘(팩토리얼 예제) (0) | 2016.09.01 |
[JAVA] switch문 활용해보기 (0) | 2016.08.30 |
[JAVA] if문 else if문 활용하기 (0) | 2016.08.30 |
[JAVA] 논리연산자 알아보기 (0) | 2016.08.30 |