1. 반복문
package basic_html;
public class oop11 {
public static void main(String[] args) {
oop11_box ob = new oop11_box();
ob.box1(); //for문 올림차순
ob.box2(); //for문 내림차순
ob.box3(); //for문 무한루프
ob.box4(); //무한루프를 이용한 응용(if)
ob.box5();
}
}
class oop11_box {
int f; // 반복문에서 사용하는 변수
public void box1() {
int sum = 0;
int number = 0;
Integer number = null;
for (this.f = 1; this.f < 5; this.f++) { // 1~4
number = 100; // number 변수 for문 안에 있는 지역변수
System.out.println(this.f);
sum += this.f;
}
System.out.println(sum);
System.out.println(number);
}
public void box2() {
for (this.f = 20; this.f > 5; this.f--) {
if (this.f <= 10) {
break; // 반복문 정지
}
System.out.println(f);
}
}
public void box3() {
//for(;;) 무한루프
for (this.f = 1;; f++) { //1~무한루프
System.out.println(this.f);
if (this.f >= 50) { //조건문으로 해당 조건이 완료되었을 때 정지
break;
}
}
}
/*
응용편(무한루프 + 조건문)
80이상이 되었을 경우 무한루프는 정지
80까지 숫자중 짝수의 개수 최종 출력
*/
public void box4() {
int cnt = 0;
for(this.f=1;;this.f++) {
if(this.f>=80) {
break;
}
if(this.f%2==0) {
cnt++;
}
}
System.out.println(cnt);
}
public void box5() { //반복문 : break; continue;
for(this.f=1;this.f<=10;this.f++) {
if(this.f ==4 || this.f == 8 || this.f == 9) {
continue; //반복문 실행시 조건에 맞을 경우 건너뛰기
}
System.out.println(this.f);
}
}
}
- break : 반복문 중단
- continue : 반복문 건너뛰기
package basic_html;
public class oop12 {
public static void main(String[] args) {
new oop12_box().box1(); //while, do~while 기본형태
new oop12_box().box2(); //while 무한루프
new oop12_box().box3(); // do~while 무한루프
}
}
class oop12_box {
int w;
public void box1() {
this.w = 0;
while (this.w <= 5) {
System.out.println(this.w);
this.w++;
}
this.w = 0;
do {
System.out.println(this.w);
this.w++;
} while (this.w <= 5); // do~while은 마지막에;찍기
}
public void box2() {
this.w = 0;
boolean ok = true;
while (ok) { // true 무한
System.out.println(this.w);
if (this.w == 20) {
ok = false; // true와 false를 이용하여 무한, 정지 가능
// break;
}
this.w++;
}
}
public void box3() {
this.w = 0;
do {
System.out.println(this.w);
if (this.w == 20) {
break;
}
this.w++;
} while (true);
}
}
- while 무한반복 : 조건에 boolean 이용하여 true로 무한반복
- 조건에 false를 이용하거나 반복문 안에 break를 이용해 중단
2. 일반 변수와 메모리 지정 변수
public class oop13 {
public static void main(String[] args) {
oop13_box ob = new oop13_box();
ob.aaa();
ob.aaa();
oop13_box ob2 = new oop13_box();
ob2.aaa();
oop13_box ob3 = new oop13_box();
ob3.aaa();
}
}
class oop13_box{
int a = 1; //메소드가 실행시 적용되는 변수
static int b = 1; //메모리에 저장되는 변수
public void aaa() {
this.a++;
this.b++;
System.out.println("일반변수 : " + this.a);
System.out.println("메모리변수 : " + this.b);
}
}
- 일반 변수 : 메소드가 실행시 적용되는 변수
- 메모리 지정 변수 : 메모리에 저장되는 변수
- new => 클래스를 호출시 일반 변수는 새롭게 인식, static 변수는 기존값을 기억시킴
3. Scanner
import java.util.Scanner;
public class oop13 {
public static void main(String[] args) {
oop13_box ob = new oop13_box();
ob3.bbb();
ob3.ccc();
}
}
class oop13_box{
public void bbb() {
Scanner sc = new Scanner(System.in); //가상으로 사용자가 입력을 받을 수 있는 클래스
System.out.println("고객명을 입력하세요 : ");
// String user = sc.next();
String user = sc.nextLine();
System.out.println(user);
}
public void ccc() {
Scanner sc = new Scanner(System.in);
System.out.println("숫자를 입력하세요 : ");
int number = sc.nextInt();
System.out.println(number);
System.out.println("소수점을 입력하세요 : ");
float sosu = sc.nextFloat();
System.out.println(sosu);
System.out.println("소수점을 입력하세요 : ");
double sosu2 = sc.nextDouble();
System.out.println(sosu2);
sc.close();
}
}
- .next() : 문자나 숫자 입력값을 받는 역할 // 공백X
- .nextLine() : 문자나 숫자 입력값을 받는 역할 // 공백O
- .nextInt() : 숫자 입력값을 받는 역할
- //이외 Float, Double 등등 모두 따로있음
- close() : 입력 클래스 라이브러리이기 때문에 끝에 반드시 사용
⭐️ Scanner 사용시 주의사항
1. close를 사용시 System.in이 dead가 되면서 더 이상 사용하지 못함
마지막으로 스캐너를 사용한 메소드의 마지막부분에 close 사용 (수동)
2. nextLine을 사용 후 자료형이 변한경우 다시 nextLine 사용 불가능 (버그)
nextLine을 쓸거면 계속 nextLine만 써야함
nextLine 쓰고 nextInt쓰면서 자료형이 바뀌면 이후에 nextLine을 쓸때 오류가남
nextLine쓰고 nextInt쓰고 next는 가능 다다르게쓰면 가능
3. equals : 사용자가 직접 값을 입력하거나 Front쪽에서 값을 이관받을 때
== : 변수에 직접 개발자가 값을 넣을때
intern() : 연산기호로 핸들링한다는 메소드
package basic_html;
import java.util.Scanner;
public class oop14 {
public static void main(String[] args) {
oop14_box ob = new oop14_box();
ob.login();
ob.member();
}
}
class oop14_box{
String id; //전역변수
Scanner sc = new Scanner(System.in); //모든 메소드에 해당 클래스에 대한 객체 활용할 수 있음
public void login() {
System.out.println("아이디를 입력하세요 : ");
this.id = this.sc.nextLine();
//==쓰려면 아래처럼 intern 사용
//this.id = this.sc.nextLine().intern();
if(this.id.equals("hong")) { //여기서 ==쓰면 오류남 입력받은 문자는 ==를 안먹어서 equals써야함!!!
System.out.println("가입된 사용자");
}else {
System.out.println("미가입자");
}
//숫자는 연산기호 모두 핸들링
System.out.println("사용자의 나이를 입력하세요 : ");
int age = this.sc.nextInt();
if(age < 14) {
System.out.println("14세 미만은 가입이 불가능합니다.");
}else {
System.out.println("14세 이상입니다.");
}
}
public void member() {
System.out.println("인증번호를 입력하세요 : ");
int code = this.sc.nextInt();
System.out.println(code);
this.sc.close();
}
}
😊응용문제
사용자가 숫자 하나 입력하면 해당 구구단 출력
예시)
구구단 수를 입력하세요 : 4
public class oop15 {
public static void main(String[] args) {
oop15_box ob = new oop15_box();
ob.gugu();
}
}
class oop15_box {
public void gugu() {
int n = 0; // 입력된수 단
int i = 1; // 1~9
Scanner sc = new Scanner(System.in);
System.out.println("구구단 수를 입력하세요 : ");
n = sc.nextInt();
while (i <= 9) {
System.out.println(n + " * " + i + " = " + n * i);
i++;
}
sc.close();
}
}
4. 더블반복문
package basic_html;
import java.util.Scanner;
public class oop16 {
public static void main(String[] args) {
oop16_box ob = new oop16_box();
ob.fors();
ob.whiles();
ob.dowhiles();
}
}
class oop16_box {
int f, ff;
Scanner sc = null;
public void fors() {
for (this.f = 2; this.f <= 9; this.f++) {
for (this.ff = 1; this.ff <= 9; this.ff++) {
System.out.println(f + " * " + ff + " = " + f * ff);
}
}
}
public void whiles() {
this.f = 2;
while (this.f <= 9) {
this.ff = 1;
while (this.ff <= 9) {
System.out.println(f + " * " + ff + " = " + f * ff);
this.ff++;
}
this.f++;
}
}
public void dowhiles() {
this.f = 2;
do {
this.ff = 1;
do {
System.out.println(f + " * " + ff + " = " + f * ff);
this.ff++;
} while (this.ff <= 9);
this.f++;
} while (this.f <= 9);
}
}
😊응용문제
사용자가 구구단 단수를 입력
"시작 단수 : "
"종료 단수 : "
"구구단 범위 정수 : "
public void users() {
int n = 0; // 입력된수 시작단
int n2 = 0; // 입력된수 종료단
int i = 1; // 뒷범위
this.sc = new Scanner(System.in);
System.out.println("시작 단수 : ");
n = sc.nextInt();
System.out.println("종료 단수 : ");
n2 = sc.nextInt();
System.out.println("구구단 범위 정수 : ");
i = sc.nextInt();
while (n <= n2) {
this.ff = 1;
while (this.ff <= i) {
System.out.println(n + " * " + this.ff + " = " + n * this.ff);
this.ff++;
}
n++;
}
}
1. 잔액 확인
2. 입금
3. 출금
4. 종료
package basic_html;
import java.util.Scanner;
public class oop18 {
public static void main(String[] args) {
oop18_bank ob = new oop18_bank();
Scanner sc = new Scanner(System.in);
System.out.printf("1. 잔액확인%n2. 입금%n3. 출금%n4. 종료%n번호를 입력하세요 : ");
int no = sc.nextInt();
if (no == 1 || no == 2 || no == 3 || no == 4) {
switch (no) {
case 1:
ob.check();
break;
case 2:
ob.deposit();
break;
case 3:
ob.withdraw();
break;
case 4:
System.out.println("은행 업무 종료");
sc.close();
break;
default:
System.out.println("은행 업무 종료");
sc.close();
break;
}
} else {
System.out.println("번호 잘못 입력하셨습니다. 종료힐게");
}
}
}
class oop18_bank {
int money = 100000; // 통장 잔고
Scanner sc2 = null;
public void check() {
System.out.println("현재 잔액 : " + this.money);
}
public void deposit() {
this.sc2 = new Scanner(System.in);
System.out.println("입금하실 금액을 입력하세요 : ");
int money2 = this.sc2.nextInt(); // 사용자가 입금하는 금액
this.money = this.money + money2;
System.out.println("입금이 완료되었습니다.");
System.out.println("현재 잔액 : " + this.money);
}
public void withdraw() {
this.sc2 = new Scanner(System.in);
System.out.println("출금하실 금액을 입력하세요 : ");
int money2 = this.sc2.nextInt(); // 사용자가 입금하는 금액
if (money2 > this.money) {
System.out.println("잔액이 부족합니다.");
} else {
this.money -= money2;
System.out.println("현재 잔액 : " + this.money);
}
}
}