카테고리 없음

2019-03-22_TIL

Doohwancho 2019. 3. 22. 23:17
package Java_book;
import java.util.*;
public class MM_Bingo {
public static void main(String[] args) {
final int SIZE = 10;
int x = 0, y = 0;
char[][] board = new char[SIZE][SIZE];
byte[][] shipBoard = {
{1,0,0,0,0,0,0,1,0},
{0,0,0,0,1,0,0,0,0},
{0,1,0,0,0,0,1,0,0},
{0,0,0,0,0,0,0,1,0},
{0,0,0,1,0,0,0,1,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0},
{0,0,1,0,0,1,0,0,0},
{0,0,0,0,0,0,0,0,1},
};
//1행에 행번호를, 1열에 열번호를 저장
for(int i=1; i<SIZE; i++)
{
board[0][i] = board[i][0] = (char)(i+'0');
}
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.printf("좌표를 입력하세요.(종료는 00)>");
String input = scanner.nextLine();
if(input.length()==2) {
x = input.charAt(0) - '0'; //문자를 숫자로 변환??
y = input.charAt(1) - '0';
if(x==0 && y==0)
break;
}
if(input.length()!=2 || x<=0 || x>=SIZE || y<=0 || y >= SIZE) {
System.out.println("잘못 입력하셨어요. 다시입력해주세요.");
continue;
}
board[x][y] = shipBoard[x-1][y-1]==1 ? 'O' : 'X';
for(int i=0; i<SIZE;i++)
{
System.out.println(board[i]);
}
System.out.println();
}
}
}
좌표를 입력하세요.(종료는 00)>11
package Java_book;
public class MM_multiplyMatrix {
public static void main(String[] args) {
int[][] m1 = {
{1,2,3},
{4,5,6}
};
int[][] m2 = {
{1,2},
{3,4},
{5,6}
};
final int ROW = m1.length;
final int COL = m2[0].length;
final int M2_ROW = m2.length;
int[][] m3 = new int[ROW][COL];
//행렬곱 m1 x m2의 결과를 m3에 저장
for(int i = 0; i<ROW; i++) {
for(int j = 0;j<COL; j++) {
for(int k=0; k<M2_ROW; k++)
m3[i][j] += m1[i][k] * m2[k][i];
}
}
//행렬m3를 출력
for(int i=0; i<ROW; i++) {
for(int j=0;j<COL;j++) {
System.out.printf("%3d",m3[i][j]);
}
System.out.println();
}
}
}
22 22
64 64
package Java_book;
import java.util.Scanner;
public class MM_wordGame {
public static void main(String[] args) {
String[][] words = {
{"chair","의자"},
{"computer","컴퓨터"},
{"integer","정수"}
};
Scanner scanner = new Scanner(System.in);
for(int i=0;i<words.length;i++)
{
System.out.printf("Q%d. %s의 뜻은?",i+1,words[i][0]);
String tmp = scanner.nextLine();
if(tmp.equals(words[i][1]))
{
System.out.printf("정답입니다.%n%n");
} else {
System.out.printf("틀렸습니다. 정답은 %s 입니다. %n%n", words[i][1]);
}
}
}
}
Q1. chair의 뜻은?의자
정답입니다.
Q2. computer의 뜻은?
package Java_book;
public class Tv {
//Tv의 속성(멤버변수)
String color;
boolean power;
int channel;
//Tv의 기능(메서드)
void power() {
power = !power;
}
void channelUp() {
++channel;
}
void channelDown() {
--channel;
}
}
package Java_book;
public class TvTest {
public static void main(String[] args) {
Tv t1 = new Tv();
Tv t2 = new Tv();
System.out.println("t1의 channel값은 " + t1.channel + "입니다");
System.out.println("t2의 channel값은 " + t2.channel + "입니다");
System.out.println("*************************************");
t1.channel = 7;
System.out.println("t1의 channel값은 " + t1.channel + "입니다");
System.out.println("t2의 channel값은 " + t2.channel + "입니다");
System.out.println("*************************************");
//t1의 주소값을 t2에 저장
//자신을 참조하고 있는 참조변수가 하나도 없는 기존의 t2인스턴스는 더 이상 사용되어질 수 없으므로
//garbage collector에 의해 자동적으로 메모리에서 제거됨
t2 = t1;
System.out.println("t1의 channel값은 " + t1.channel + "입니다");
System.out.println("t2의 channel값은 " + t2.channel + "입니다");
System.out.println("*************************************");
t1.channelUp();
System.out.println("t1의 channel값은 " + t1.channel + "입니다");
System.out.println("t2의 channel값은 " + t2.channel + "입니다");
System.out.println("*************************************");
}
}
t1의 channel값은 0입니다
t2의 channel값은 0입니다
*************************************
t1의 channel값은 7입니다
t2의 channel값은 0입니다
*************************************
t1의 channel값은 7입니다
t2의 channel값은 7입니다
*************************************
t1의 channel값은 8입니다
t2의 channel값은 8입니다
*************************************