method
반환형을 정해주고 함수를 작성한다.
사용할 때는 static으로 만들었으면 class 이름을 붙여줘야한다.
import java.util.Scanner;
class Test
{
public static void menu()
{
System.out.println("****Game Menu*****");
System.out.println("1.가위 2.바위 3.보 9.종료");
System.out.println("******************");
System.out.println("입력하세요=>");
System.out.println("******************");
}
public static String show(int num){
String str = "";
switch(num){
case 1 : str="가위"; break;
case 2 : str="바위"; break;
case 3 : str="보"; break;
}
return str;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(true){
int x = (int)(Math.random()*3+1);
Test.menu();
int y = sc.nextInt();
String xx= Test.show(x);
String yy= Test.show(y);
if(y==9){
System.out.println("Bye bye~");
return;
}
System.out.println("컴: "+xx+" 나: "+yy+" ");
if(x==y){
System.out.println("비겼습니다.");
}else if((x==1&&y==2)||(x==2&&y==3)||(x==3&&y==1)){
System.out.println("이겼습니다.");
}else{
System.out.println("졌습니다.");
}
}
}
}
변수
데이터를 임시적으로 저장하는 메모리 공간
즉 변수란 값을 저장하는 메모리 공간의 위치를 의미
변수의 종류
1) 멤버변수(instance 변수)
ex) int a =10;
객체명으로 접근해야 한다.
2) 클래스변수(static 변수)
ex) static int b=10;
클래스명으로 접근해야 한다.
인스턴스변수 = > 객채명.변수 식으로 접근해야 한다.
class MyDemo
{
int b = 20;//전역변수
static int c =30; //클래스 변수 => 클래스명.변수 로 접근
public static void main(String[] args)
{
int a = 10; // 지역변수 local variable
System.out.println(a);
System.out.println(MyDemo.c);
MyDemo md = new MyDemo(); // 객체 생성
//Heap메모리 영역에 MyDemo객체가 올라가
//md ==> 객체를 참조하는 이름 (객체명)
System.out.println(md.b);
System.out.println(YourDemo.c); //your
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
class YourDemo
{
static String c="your";
}
static인 메소드 안에서 static인 변수 메소드는 바로 불러올 수 있다.
static인 메소드 안에서 인스턴스 변수를 불러오려면
객체를 생성해서 불러와야한다.
반환타입이 있는 메소드 = > 메소드 블럭 끝에서 return 문장을 작성해야 함 또한 반환타입에 해당하는 값을 return 해야 한다.
class HisDemo
{
float var = 3.45f;
static char ch='Q';
public static void main(String[] args)
{
HisDemo hd = new HisDemo();
System.out.println(hd.var);
System.out.println(ch);
HerDemo hed = new HerDemo();
System.out.println(hed.str);
System.out.println(HerDemo.info);
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
class HerDemo
{
String str ="HerDemo's str";
static String info="HerDemo's info";
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
class YourTest
{
//반환 타입이 있는 메소드
public static int giveMe(int money){
System.out.println("투자금"+money+"만원");
return 2*money;
}
public static void main(String[] args)
{
int a = YourTest.giveMe(3000);
System.out.println()
}
}
기본자료형 참조형 (Wrapper class)
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
byte Byte
short Short
char Character
int Integer
long Long
float Float
double Double
boolean Boolean
API에서 Integer을 가보면
java.lang.Integer클래스의 필드(멤버변수)
1) static int MAX_VALUE
2) static int MIN_VALUE
메소드
static int parseInt(String s) 등을 사용할 수 있다.
import java.lang.Integer;
class Wrapper
{
public static void main(String[] args)
{
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
int a = Integer.parseInt("1122");
System.out.println(a);
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
CSS 글씨 속성
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<style type="text/css">
.title{
font-family : monospace, 굴림체;
font-size : 20pt;
font-style : italic;
/*normal, italic, oblique*/
font-weight : bold;
/*수치값(100~700), lighter bolder bold 700 normal 400*/
color : maroon;
font-variant : small-caps;
/*대소문자 유형 , normal, small-caps 대문자, large-caps 소문#자*/
}
div{
font-size : 50px;
}
#basic{
text-decoration:none;
}
#over{
text-decoration : overline;
}
#under{
text-decoration : line-through;
}
table{
width : 80%;
height: 360px;
border : 1px solid blue;
text-align : center;
margin : auto;
}
table td{
border: 2px dashed red;
}
</style>
</head>
<body>
<h1 class="title">텍스트 글자 관련 스타일을 알아봅시다 (text style)</h1>
<div style="font-variant: small-caps;">Hello CSS</div>
<div id="basic">기본</div>
<div id="over">취소선</div>
<div id="under">취소선</div>
<div id="del">취소선</div>
<p></p>
<table>
<tr>
<!--vertical-align : top, bottom, middle 가 들어갈 수 있다.-->
<td style="vertical-align: top; width: 50%;" >수직정렬</td>
<!--text-align : 수평정렬 left, center, right-->
<td style="text-align: right; border-left : 1px solid green;">수평정렬</td>
</tr>
<tr>
<td>
<span style="text-transform:capitalize;">abc(첫글자를 대문자로)</span>
<span style="text-transform:uppercase;">abc(모두 대문자로)</span>
<span style="text-transform:lowercase;">abcDEF(모수 소문자로)</span>
</td>
<td>
<span >
문자간격</span> </br>
<span style="letter-spacing : 7px;">문자간격</span>
<span style="word-spacing : 20px;">단어 간격</span>
</td>
</tr>
</table>
<!--text-shadow : x축간격 y축간격 번짐크기 색상-->
<p style="font-family:cursive; font-size:3em; text-shadow: 2px 4px 6px green">
안녕 Hi
</p>
</body>
</html>
css table 속성
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>ex26배경스타일.html</title>
<style>
span.bg1{
background-color : rgba(100,150,200,0.1);
}
h2.bg1{
background-image : url("images/logo.png");
height: 200px;
width : width:50%;
}
td{
background-image : url("images/little.png");
}
td#x{
background-repeat : repeat-x;
}
td#y{
background-repeat : repeat-y;
}
td#n{
background-repeat : no-repeat;
/*배경이미지 기본 위치는 left top*/
}
td.b{
background-repeat : no-repeat;
/*배경이미지 기본 위치는 left top*/
}
</style>
</head>
<body>
<h1>배경 관련 스타일</h1>
<span class="bg1">배경색 적용 1</span>
<span class="bg2">배경색 적용 2</span>
<h2 class="bg1"></h2>
<!--2행 4열 테이블-->
<table style="width:90%;margin:auto;height:400px;" border="1">
<tr>
<td id="a" style="width:25%;">repeat</td>
<td id="x" style="width:25%;">repeat-x</td>
<td id="y" style="width:25%;">repeat-y</td>
<td id="n" style="width:25%;">no-repeat</td>
</tr>
<tr>
<td class="b">기본 위치는 left,top</td>
<td class="b" style="background-position:right bottom;">right,bottom</td>
<td class="b" style="background-position:50% 50%;">50% 50%</td>
<td class="b" style="background-position:50px 50px;">50px 50px</td>
</tr>
</table>
</body>
</html>
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
api 이용 예제 string 을 이용해 정수 스트링을 받아서 2진수와 8진수 16진수로 출력하시오
import javax.swing.JOptionPane;
class Wrapper2
{
public static void main(String[] args)
{
String str = JOptionPane.showInputDialog("정수1를 입력하세요");
System.out.println("str: " +str);
String str2 = JOptionPane.showInputDialog("정수2를 입력하세요");
System.out.println("str2: "+str2);
//두 정수값의 합을 구하시오
int sum = Integer.parseInt(str)+Integer.parseInt(str2);
//덧셈식과 결과값 출력하기
System.out.println(str+"+"+str2+"="+sum);
}
}
import javax.swing.JOptionPane;
class Wrapper3
{
public static void main(String[] args)
{
String str = JOptionPane.showInputDialog("정수1를 입력하세요");
System.out.println("str: " +str);
int num = Integer.parseInt(str);
//입력한 정수값의 2진수 값을 출력하세요 (api 이용해서 풀어라)
//static String toBinaryString(int i) 정수 i의 이진수 문자열을 반환
String b2 = Integer.toBinaryString(num);
System.out.println(b2);
//입력한 정수값의 8진수 값을 출력하세요
//static String toOctalString(int i)
String b8 = Integer.toOctalString(num);
System.out.println(b8);
//입력한 정수값의 16진수 값을 출력하세요
//static String toHexString(int i)
String b16 = Integer.toHexString(num);
System.out.println(b16);
}
}
'개발자 > 국비지원 SW' 카테고리의 다른 글
| 국비지원 10일차 - 참조유형의 배열, for each문, OOP, 추상화 (0) | 2020.04.23 |
|---|---|
| 국비지원 9일차 - eclipse 설치 및 사용, 배열, border style, 하이퍼링크 스타이, button (0) | 2020.04.22 |
| 국비지원 7일차 - dowhile 문, break 문, continue 문, JAVA API, CSS외부정의, style selector (0) | 2020.04.20 |
| 국비지원 6일차 - 반복문(for, while), html5 추가된 inputForm, CSS (0) | 2020.04.17 |
| 국비지원 5일차 - System.in() , Scanner로 char 받기, form태그(get, post) (0) | 2020.04.16 |