ํ์ต ๋ด์ฉ
- while, do while๋ฌธ
public class mainClass {
public static void main(String[] args) {
//while๋ฌธ ------------------------------------------------------------
int w;
w = 0; //์ด๊ธฐํ
while(w<10) {
System.out.println("while loop " +w); //์ฒ๋ฆฌ
w++; //์ฐ์ฐ์( ์์ผ๋ฉด ๋ฌดํ๋ฃจํ)
}
//2์ค while๋ฌธ ----------------------------------------------------------
int w1, w2;
w1 = 0;
while(w1<5) {
System.out.println("w1 = " +w1);
w2 = 0; //์์ชฝ while๋ฌธ์ ์ด๊ธฐํ ์์น ์ ์ ์ ํ๊ธฐ
while(w2<3) {
System.out.println("w2 = " +w2);
w2++;
}
w1++;
//do-while๋ฌธ -----------------------------------------------------------
int w3;
w3 = 100;
do {
System.out.println("do while " +w3); //while๋ฌธ๊ณผ ๋ค๋ฅด๊ฒ ์กฐ๊ฑด์์ด ์๋์ ์์ด์ *์กฐ๊ฑด์ด ์๋ง์๋ ๋ฌด์กฐ๊ฑด 1ํ ์คํ๋จ*
w3++; //๊ทธ๋์ ์ฌ์ฉ ๋น๋๊ฐ ๋ฎ์
}while(w3<5);
}
}
}
์ด๋ ค์ ๋ ๋ฌธ์
package work05;
import java.util.Arrays;
public class MainClass6 {
public static void main(String[] args) {
// ๋ค์์ ์ํ๋ฒณ๊ณผ ์ซ์๋ฅผ ์๋์ ์ฃผ์ด์ง ์ํธํ๋ก ์ํธํํ๋ ํ๋ก๊ทธ๋จ์ด๋ค.
// (1)์ ์๋ง์ ์ฝ๋๋ฅผ ๋ฃ์ด์ ์์ฑํ์์ค.
// a b c d e f g h i j k l m n o p q r s t u v w x y z
// ` ~ ! @ # $ % ^ & * ( ) - _ + = | [ ] { } ; : , . /
// 0 1 2 3 4 5 6 7 8 9
// q w e r t y u i o p
char[] abcCode =
{ '`','~','!','@','#','$','%','^','&','*', // abcdefghijklmnopqrstuvwxyz
'(',')','-','_','+','=','|','[',']','{',
'}',';',':',',','.','/'};
char[] numCode =
{'q','w','e','r','t','y','u','i','o','p'}; // 0 1 2 3 4 5 6 7 8 9
String src = "abc123";
String result = "";
//์ํธํ ----------------------------------------------
for(int i=0; i < src.length();i++) {
char ch = src.charAt(i); // ๋ฌธ์์ด src์ ๋ฌธ์๋ฅผ charAt()์ผ๋ก ํ๋์ฉ ์ฝ์ด์ ๋ณํ ํ result์ ์ ์ฅ
if(ch >= 97 && ch <= 122 ) {
result += abcCode[ch-97];
} else if(ch >= 48 && ch <= 57){
result += numCode[ch-48];
}
}
System.out.println("์๋ณธ : "+src);
System.out.println("์ํธํ : "+result);
//๋ณตํธํ -----------------------------------------------
String baseCode = "";
for (int i = 0; i < result.length(); i++) {
char c = result.charAt(i); //์ํธ ํ๋๋ง๋ค ์บ๋ฆญํฐํ
int asc = (int)c; //์บ๋ฆญํฐ ๋ฌธ์ ์ ์(์์คํค์ฝ๋)๋ก ๋ณํ
int index = 0;
if(asc >= 97 && asc <= 122) { //๋ฌธ์๊ฐ 97~122(์์๋ฌธ์ ๋ฒ์) ์ผ ๋,
for (int j = 0; j < numCode.length; j++) {
if(c == numCode[j]) { //numCode์ j๋ฒ์งธ ์์น๊ฐ๊ณผ ๋ฌธ์ ๋์กฐ
index = j; //numCode์์ ์ธ๋ฑ์ค ๋ฒํธ ์ฐพ์์ผ๋ฉด
break; // ๋๊ฐ
}
}
index += 48; // ์ธ๋ฑ์ค ๋ฒํธ์ 48์ ๋ํด์ฃผ๋ฉด ์ซ์๋ฒ์ ์์คํค์ฝ๋
baseCode += (char)index; //๋ณตํธํ ๋ฌธ์์ ๋ํด์ค
} else {
for (int j = 0; j < abcCode.length; j++) { // ์์๋ฌธ์ ๋ฒ์ ์ด์ธ์ ๋ฌธ์
if(c == abcCode[j]) { //abcCode์์ j๋ฒ์งธ ์์น๊ฐ๊ณผ ๋์กฐ
index = j; //๊ฐ์ผ๋ฉด ์ธ๋ฑ์ค๋ฒํธ ๋ฝ์๋ด๊ณ
break; //ํ์ถ
}
}
index += 97; // ์ธ๋ฑ์ค ๋ฒํธ์ 97 ๋ํ๋ฉด ์์๋ฌธ์ ๋ฒ์
baseCode += (char)index;
}
}
System.out.println("๋ณตํธํ : "+baseCode);
// [์คํ๊ฒฐ๊ณผ]
// src:abc123
// result:`~!wer
// ์
๋ ฅ ๋ฐ์ ๋ฌธ์์ด์ ์ํธํ ํ๋๋ก ์ฝ๋๋ฅผ ์
๊ทธ๋ ์ด๋ ํ๋ค.
}
}
์์ง ํด๊ฒฐ ๋ชปํ ๋ฌธ์
-์ ๋ฒ์ ๋ง๋ ๊ณ์ฐ๊ธฐ ๋ถ๋ถ์ ์ซ์ ์ ๋ ฅ ๋ถ๋ถ์ ์ ์ดํ๋ ๋ถ๋ถ์ ์ ๊ทธ๋ ์ด๋ ํ๋ ๋ฐ์ ์์ด ๋งํ๋ค..
๋ด์ผ ๊ฐ์์๊ฐ์ ๋ ์ง์คํด์ ์์๋ฌ์ผ๊ฒ ๋ค.
// ๊ณ์ฐ๊ธฐ
String num1 ; // ์
๋ ฅ์ฉ
String num2 ; // ์
๋ ฅ์ฉ
int iNum1, iNum2;
String oper; // ์
๋ ฅ์ฉ
int result = 0; // ๊ฒฐ๊ณผ ์ถ๋ ฅ์ฉ
boolean b = false;
// 1.์
๋ ฅ
// ์ซ์1
System.out.print("์ซ์1 = ");
num1 = sc.next();
for (int i = 0; i < num1.length(); i++) {
char c = num1.charAt(i);
int asc = (int) c;
if(asc<48 || asc>57) {
System.out.println("์ซ์๊ฐ ์๋๋๋ค.");
}
else if(asc>=48 && asc<=57) {
num1 =
break;
}
}
System.out.println(num1);
'๐ป ๊ฐ๋ฐ > ๐ TIL (Today I Learned)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
21.01.22 TIL (D+10 JAVA ์บ๋ฆฐ๋, ์ฃผ์๋ก ๋ง๋ค๊ธฐ) (0) | 2021.01.22 |
---|---|
21.01.21 TIL (D+9 JAVA sorting, ๋ฉ์๋) (0) | 2021.01.21 |
21.01.15 TIL (D+5 JAVA ์ ์ด๋ฌธ) (0) | 2021.01.15 |
21.01.14 TIL (JAVA ๋ฐฐ์ด Array) (0) | 2021.01.14 |
21.01.13 TIL(๊ตญ๋น 3์ผ์ฐจ - JAVA ์ฐ์ฐ) (0) | 2021.01.13 |