-
5일 차 JAVA 중간점검(배열을 이용한 거스름 돈, 입력한 숫자의 위치 등)삼성SDS_멀티캠퍼스/Java 2015. 9. 11. 12:52반응형
정수형 배열 odd을 만들고 1,3,5,7을 입력한 후
그 값을 모두 더한 값
소스코드
--------------------------------------
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
int[] odd = {1,3,5,7};
for(int i=0; i<odd.length; i++)
System.out.println(odd[i]);
}
}
문자형 배열 alpha를 만들고 A~Z까지 입력한 후 순서대로 출력
소스코드
--------------------------------------
import java.util.Scanner;
public class Test8 {
public static void main(String[] args) {
char[] alpha = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'N', 'M', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
for (int i = 0; i < alpha.length; i++)
System.out.println(alpha[i]);
}
}
문자형 배열 alpha를 만들고 Z~A까지 입력한 후 역 순서로 출력
소스코드
--------------------------------------
import java.util.Scanner;
public class Test9 {
public static void main(String[] args) {
char[] alpha = { 'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'm', 'n', 'l', 'k', 'j', 'i', 'h',
'g', 'f', 'e', 'd', 'c', 'b', 'a' };
for (int i = alpha.length -1; i > -1; i--)
System.out.println(alpha[i]);
}
}
정수형 배열 arr을 만들고 38,29,86,55,19를 모두 더한 값
소스코드
--------------------------------------
import java.util.Scanner;
public class Test10 {
public static void main(String[] args) {
int[] arr = { 38, 29, 86, 55, 19 };
int temp=0;
for(int i=0; i<arr.length; i++)
temp += arr[i];
System.out.println(temp);
}
}
정수형 배열 coinUnit에 500,100,50,10을 기입하고
2680을 모두 사용하려면
500,100,50,10 이 몇번 사용해야 하는지
ex) 1320
500 = 2
100 = 3
50 = 0
10 = 2
소스코드
--------------------------------------
import java.util.Scanner;
public class Test11 {
public static void main(String[] args) {
int[] coinUnit = { 500, 100, 50, 10 };
int money = 2680;
int temp = 0;
int result500 = 0;
int result100 = 0;
int result50 = 0;
int result10 = 0;
for (int i = 0; i < coinUnit.length; i++) {
temp += coinUnit[i];
// System.out.println(temp);
if (money > 0) {
result500 = money / coinUnit[0];
result100 = money % coinUnit[0] / coinUnit[1];
result50 = money % coinUnit[0] % coinUnit[1] / coinUnit[2];
result10 = money % coinUnit[0] % coinUnit[1] % coinUnit[2] / coinUnit[3];
}
}
System.out.println(money + "원을 사용하기 위해 필요한 돈은");
System.out.println("500원" + result500 + "개");
System.out.println("100원" + result100 + "개");
System.out.println("50원" + result50 + "개");
System.out.println("10원" + result10 + "개");
}
}
이렇게 길게 써도 되고import java.util.Scanner;public class Test14 {public static void main(String[] args) {int[] coinUnit = { 500, 100, 50, 10 };int money = 2680;System.out.println(money + "원을 내려면");for (int i = 0; i < coinUnit.length; i++) {System.out.println(coinUnit[i] + "원 : " + money / coinUnit[i]+"개");money = money % coinUnit[i];}}}이렇게 짧게 할 수도 있다정수형 배열 arr에 5,3,1,2,8,7,0,9,6,4를 기입하고
사용자가 0~9까지의 숫자를 랜덤으로 입력했을 때
해당 숫자가 몇번째에 있는지 출력
소스코드
--------------------------------------
import java.util.Scanner;
public class Test12 {
public static void main(String[] args) {
int[] arr = { 5, 3, 1, 2, 8, 7, 0, 9, 6, 4 };
int num1;
int count = 1;
Scanner scan = new Scanner(System.in);
System.out.println("0~9까지의 숫자를 입력하세요");
num1 = scan.nextInt();
for (int num2 : arr) {
if (num2 == num1) {
break;
}
count++;
}
System.out.println("입력하신 값은 " + count + "번 째에 있습니다");
}
}
이렇게 해도 되고
import java.util.Scanner;
public class Test15 {
public static void main(String[] args) {
int[] arr = { 5, 3, 1, 2, 8, 7, 0, 9, 6, 4 };
int num1;
Scanner scan = new Scanner(System.in);
System.out.println("0~9까지의 숫자를 입력하세요");
num1 = scan.nextInt();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == num1)
System.out.println(i + "번 인덱스에 숫자가 있다");
}
}
}
이렇게 해도 된다.
출처: 삼성SDS멀티캠퍼스
강사: 홍승길
Email : iccack70@gmail.com
반응형'삼성SDS_멀티캠퍼스 > Java' 카테고리의 다른 글
5일 차 JAVA 배열의 값 중 가장 작은 수와 배열의 첫번째 값의 위치를 바꾸기 (0) 2015.09.11 5일 차 JAVA 배열을 이용하여 제일 큰 숫자를 출력 (0) 2015.09.11 5일 차 JAVA for each루프 문 활용하여 배열 출력(향상된 for문) (0) 2015.09.11 5일 차 JAVA 배열의 참조 (0) 2015.09.11 5일 차 JAVA 배열을 이용한 평균성적 출력 (1) 2015.09.11