Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 알고리즘
- 이진수
- #알고리즘
- 데이터타입
- 팩토리얼
- java
- 복합대입연산자
- 소인수분해
- plusgame
- 로또 프로그램
- 삼항 연산자
- else if문
- 연산자
- 논리연산자
- for문 369게임
- 피보나치수열
- if문
- #Java
- 비교연산자
- #완전수구하기
- JAVA기초
- java조건문
- 2차원배열
- 증감연산자
- switch문
- #java_festival
- 별찍기
- switch-case문
- #이차원배열
- 변수의특징
Archives
- Today
- Total
숭어 개발 블로그
[JAVA] 반복문_( while 문 ) 본문
while 문
- while문 : 몇번 반복해야할 정해지지 않는 경우
while문 구조
- while (조건 t / f ) { 조건이 true일 경우 실행}
- while ( 검사조건 ) { 검사조건이 true일 동안 실행 }
ex) while문 예제 1
import java.util.Scanner;
public class Exam01_while문 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner (System.in);
int sum = 0; //합계 저장 변수 ###초기화가 되면 안되는 변수
int cnt = 0; //입력 개수 저장할 변수 ###초기화가 되면 안되는 변수
int input=0; //입력값을 저장 할 변수
while(input <=100) {
System.out.println("숫자를 입력하세요");
input = sc.nextInt();
sum += input;
cnt++;
}//while
cnt--;
sum-=input;
/////true를 이용한 while문 ////
// while(true) {
// System.out.println("숫자를 입력하세요");
// int input = sc.nextInt();
// if(input > 100) {
// break; //100이 넘는다면 while문 밖으로 탈출
// }//if
//
// sum += input; //sum=sum+input
// cnt++; // input이 100이 넘지 않는다면 값 누적
//
// }//while
//
System.out.println("합계 : " + sum);
System.out.println("평균 : " + sum /(double) cnt); // 자동 형변환 실수>>정수
sc.close();
}//
}//
ex) while문 예제 2 < Plus game >
import java.util.Random;
import java.util.Scanner;
public class Exam06_플러스게임 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//문자입력방법
//
// System.out.print("문자입력 : ");
// String text = sc.next();
//
// System.out.println(text);
//난수생성(무작위 숫자)
//Random
//r.nextInt(범위);
//plusgame
// 1.게임이 시작되면 사용자에게 두개의 숫자 합을 물어본다.
// 2.사용자는 문제를 보고 정답을 입력한다.
// 3.입력한 정답과 질제 정답이 일치하게 되면 새로운 문제를 보여준다
// 4.정답이 틀렸을떄는 다시 하시겠습니까? 라고 물어보고
// Y입력 --틀렸던 문제가 다시나오며
// N입력 -- 게임이 종료가 된다.
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("====Plus Game====");
int num1 = r.nextInt(10) + 1 ; //1부터 10까지 범위
int num2 = r.nextInt(10) + 1 ;
System.out.print(num1 + "+" + num2 +"=");
int answer = sc.nextInt();
while(true) {
if(num1+num2==answer) {
System.out.println("Success!");
num1 = r.nextInt(10) + 1 ;
num2 = r.nextInt(10) + 1 ;
System.out.print(num1 + "+" + num2 +"=");
answer = sc.nextInt();
}
if (num1+num2!=answer) {
System.out.println("Fail");
System.out.print("계속하시겠습니까? >>");
String text = sc.next();
if(text.equals("y")) {
System.out.print(num1 + "+" + num2 +"=");
answer = sc.nextInt();
}
else if (text.equals("n")) {
System.out.println("종료하겠습니다.");
break;
}
}
}
sc.close();
}
}
'JAVA > 반복문' 카테고리의 다른 글
[JAVA] for문 예제_(별찍기) (0) | 2022.09.19 |
---|---|
[JAVA] for문 예제_( 구구단 ) (0) | 2022.09.19 |
[JAVA] for문 예제_ ( 369게임) (0) | 2022.09.19 |
[JAVA] 반복문_( for 문 ) (0) | 2022.09.19 |
[JAVA]do-while문 (0) | 2022.09.19 |
Comments