13)
package temp;
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num[]= new int[5]; // 배열 생성
/*int num[];
num=new int[5];
*/
System.out.println("정수 5개 입력>>");
for(int i=0; i<5; i++) {
num[i]=sc.nextInt(); // 초기화
}
System.out.println("배열이 원소 출력");
for(int i=0; i<5; i++) {
System.out.println(i+"번째 원소 :"+num[i]);
}
}
}
14) .length()
package temp;
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String name[]= new String[5];
String max= "";
System.out.print("5명의 학생 이름 입력>>");
for(int i=0; i<5; i++) {
name[i]=sc.next();
if(name[i].length()>max.length()) max=name[i]; // 문자열 길이 비교하여 가장 긴 문자열을 저장
}
System.out.println("가장 길이가 긴 이름은 "+ max);
}
15) for-each
package temp;
public class Temp {
public static void main(String[] args) {
// 뒤에 있는 변수명은 앞에 있는 변수명에 있는 만큼 대입
// for-each는 읽기 전용
int[] n= {1,2,3,4,5};
int sum=0;
for(int k:n) { // k는 n[0~4]
System.out.print(k+" ");
sum+=k;
}
System.out.println("합은"+sum);
String f[]= {"사과", "배", "바나나", "체리", "딸기", "포도"};
for(String s:f) System.out.print(s+" "); // s는 f[0~5]
}
}
16) 2차원배열
package temp;
public class Temp {
public static void main(String[] args) {
// 2차원 배열 입력시 괄호를 쓰고 행구분을 해야함
double score[][]= {{3.3, 3.4},
{3.5, 3.6},
{3.7, 4.0},
{4.1, 4.2}};
double sum=0;
for( double k[]:score) //각 학년별로 반복, 행번호
for(double j: k) // 각 학년의 학기별로 반복
sum+=j; // 전체 평점 합
/*
for(int i=0; i<score.length; i++)
for(int j=0; j<score[0].length; j++)
sum+= score[i][j];
*/
int n=score.length; // 배열 행 개수, 4
int m=score[0].length; // 배열의 열 개수, 2
System.out.println("4년 전체 평점 평균은 "+ sum/(n*m));
}
}
17)
package temp;
public class Temp {
public static void main(String[] args) {
int intArray[][];
intArray=new int[3][4]; // 3행 4열
for(int i=0; i<3; i++) //행번호
for(int j=0; j<4; j++) //열번호
intArray[i][j]=(int)(Math.random()*10);
for(int i=0; i<intArray.length; i++) { // intArray.length는 2차원 배열의 행의 개수
for(int j=0; j< intArray[0].length; j++) //int Array[0].length는 0번째 행의 열의 개수
System.out.print(intArray[i][j]);
System.out.println();
}
int i=0, sum=0;
while(i<3) {
/*for(int k[]:intArray)
for(int j:k)
sum+=j;
*/
for(int j=0; j<intArray[0].length; j++)
sum+=intArray[i][j];
i++;
}
System.out.println("합은 "+sum);
}
}
18)
package temp;
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("행 번호, 열 번호 >> ");
int a=sc.nextInt();
int b=sc.nextInt();
int sum=0, sum2=0, total=0;
int num[][]=new int[a+1][b+1]; // a와 b는 개수여야 하므로 +1
for(int i=0; i<2; i++) {
for(int j=0; j<b+1; j++) {
num[i][j]=(int)(Math.random()*10);
sum+=num[i][j];
System.out.print("\t"+"\t"+num[i][j]+" ");
}
System.out.println(" >> 행의 합은"+ sum);
total+=sum;
sum=0;
}
for(int i=2; i<a+1; i++) {
for(int j=0; j<b+1; j++) {
num[i][j]=i;
sum+=num[i][j];
System.out.print("\t"+"\t"+num[i][j]+" ");
}
System.out.println(" >> 행의 합은"+ sum);
total+=sum;
sum=0
;
}
System.out.print("열의 합은 >> ");
for(int i=0; i<b+1; i++) {
for(int j=0; j<a+1; j++) {
sum2+=num[j][i];
}
System.out.print("\t"+sum2+"\t");
sum2=0;
}
System.out.println(); // System.out.print();은 컴파일 에러 발생
System.out.println("총 합 >> "+total);
}
}
19)
package temp;
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("행번호 입력>>"); int row= in.nextInt();
System.out.print("열번호 입력>>"); int col= in.nextInt();
int [][] arr =new int[row][col];
for(int i=0; i<arr.length; i++) { // 행의 길이
for(int j=0; j<arr[0].length; j++) { // 열의 길이
if(i<2)arr[i][j]=(int)(Math.random()*10)+1; //배열 초기화
else arr[i][j]=i;
}
}
int total=0;
System.out.print("배열\t");
for(int i=0; i<arr[0].length; i++)System.out.print(i+"\t");
System.out.print("행합\n");
for(int i=0; i<arr.length; i++) {// 행
int r_sum=0; System.out.print(i+"\t");
for(int j=0; j<arr[0].length; j++) { //열
System.out.print(arr[i][j]+"\t"); r_sum+=arr[i][j];
}total+=r_sum; System.out.print(r_sum+"\n");
}
System.out.print("열합\t");;
for(int j=0; j<arr[0].length; j++) { //열
int c_sum=0;
for(int i=0; i<arr.length; i++)c_sum+=arr[i][j];
System.out.print(c_sum+"\t");
}System.out.print(total+"\n");
}
}
20) static
package temp;
public class Temp {
static int[] makeArray() {
int[] temp=new int[4];
for(int i=0; i<temp.length; i++)
temp[i]=i;
return temp;
}
public static void main(String[] args) {
int[] intArray;
intArray=makeArray();
for(int i=0; i< intArray.length; i++)
System.out.print(intArray[i]+" ");
}
}
22) try-catch-finally문
package temp;
import java.util.InputMismatchException; // try-catch-fianlly문을 사용하기 위해 작성
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int num;
for(;;) {
try {
System.out.print("정수를 입력하세요 >> ");
num= sc.nextInt();
if(num<0) {
System.out.println("프로그램을 종료합니다");
break;
}
String[] day= {"일", "월", "화", "수", "목", "금", "토"}; // 선언과 동시에 초기화
int count=num%7;
System.out.println(day[count]); //syso+Tab키
}catch(InputMismatchException e) {
System.out.println("경고! 수를 입력하지 않았습니다.");
sc.next();
}
/* String[] day;
day = new String[]{"일", "월", "화", "수", "목", "금", "토"};
*/
}
sc.close();
}
}
23)
package temp;
public class Temp {
int sum(int x[]) { // 매개변수가 배열일 경우
int n, s=0;
for(n=0; n<x.length; n++)
s += x[n];
return s;
}
/*
static int sum(int x[]) {
int n, s=0;
for(n=0; n<x.length; n++)
s += x[n];
return s;
} */
public static void main(String[] args){
int a[] ={1,2,3,4,5};
Temp temp = new Temp();
int n = temp.sum(a);
//int n = sum(a);
}
}
'자바' 카테고리의 다른 글
| 업캐스팅, 다운캐스팅, instance of (0) | 2024.05.02 |
|---|---|
| 접근지정자, super() (0) | 2024.05.02 |
| this(), this. , 배열 객체 래퍼런스 (0) | 2024.05.02 |
| 자바 기본 (1) | 2024.05.02 |
| 시작하며 (0) | 2024.05.02 |