JAVA 160601-3
package pack;
import java.util.Scanner;
public class Test12while {
@SuppressWarnings("resource")
public static void main(String[] args) {
// 반복문 while
int w = 1;
while(w <= 5){
System.out.println("w: " + w + " ");
w++; //반복문을 벗어날 수 있는 명령문 꼭 필요!
}
System.out.println("\n수행 후 w는 " + w);
//1 ~ 10까지의 합
int su = 0, tot = 0;
while(su < 10){
su++;
//System.out.println(su);
tot += su;
}
System.out.println("합은 " + tot);
System.out.println();
w = 0;
while(true){ //무한 looping
w++;
if(w == 5) continue;
System.out.println("w=>" + w);
if(w == 10) break;
}
System.out.println();
w = 1;
do{ //최소 1회 이상 수행\\조건 실행 전 실행-
System.out.println("w는" + w);
w++;
}while(w <= 5);
//문제1) 1~100 사이의 숫자 중 3의 배수이지만 2의 배수가 아닌 수를 출력하고 합과 갯수도 출력
System.out.println("\n\n");
int count = 0;
int hap = 0;
int n = 0;
while(n < 100){
n++;
if((n%3==0) && (n%2!=0)){
System.out.print(n + " ");
hap = hap + n;
count++;
}
}
System.out.println();
System.out.println("합은 " + hap + " 갯수는 " + count);
//문제2) -1, 3, -5, 7, -9, 11 ~ 99까지의 합 출력
// 2 4 6 8 10
int m2 = 3;
int hap2 = 0;
int hap3 = 0;
int m = -1;
System.out.println();
while(m >= -99){
hap2 = hap2 + m;
m-=4;
}
while(m2 <= 99){
hap3 = hap3 + m2;
m2+=4;
}
System.out.println("합은 " + (hap3 + hap2));
//문제3) 1 ~ 1000 사이의 숫자 중 소수들을 출력하고 그 갯수를 출력
System.out.println();
int k = 1;
int l = 1;
int tem = 0;
int sosucount =0;
while(l <= 1000)
{
while(k <= l)
{
if(l%k==0)
{
tem++;
}
k++;
}
if(tem==2)
{
sosucount++;
System.out.print(l + " ");
}
tem=0;
k=1;
l++;
}
System.out.println("합은: " + sosucount);
// 소수 : 1보다 크며 1과 그 수 자체 이외의 다른 수로는 나누어 떨어지지 않는 수
int yung = 65;
int gong = 0;
int gong2 = 10;
int i = 1;
int j = 1;
int yuc = 0;
while(i <= 5){
while(gong <=gong2){
System.out.print(" ");
gong++;
}
while(j <= i){
System.out.print((char)yung);
yuc = yung;
yung++;
j++;
}
while(1 < j){
System.out.print((char)yuc);
yuc--;
j--;
}
gong2 -=1;
gong = 0;
yung=65;
j=1;
i++;
yuc=yung;
System.out.println();
}gong2 = gong2+1;
int i2 = 5;
while(i2 >= 0){
while(gong <= gong2){
System.out.print(" ");
gong++;
}
while(j <= i2){
System.out.print((char)yung);
yuc = yung;
yung++;
j++;
}
while(1 < j){
System.out.print((char)yuc);
yuc--;
j--;
}
gong2 +=1;
gong = 0;
yung=65;
j=1;
i2--;
yuc=yung;
System.out.println();
}
/* aa:for (int i=1; i<=3; i++){
bb:for (int j=1; j<=5; j++){
if(j == 3) continue bb; //기본값
System.out.print(i + " " + j + " ");
}
System.out.println("****************");
}
}*/
//문제4)
/*
*
* AA
* ABBA
* ABCCBA
* ABCDDCBA
* ABCDEEDCBA
* ABCDEEDCBA
* ABCDDCBA
* ABCCBA
* ABBA
* AA
*
*/
//문제5) 키보드로 부터 숫자를 입력받아 1부터 그 수 까지의 합을 출력
// 입력되는 숫자는 2 이상만 허용
// 예) 숫자를 입력: 5
// 계속할까요?(y/n)
// y를 입력하면 다시 숫자를 입력하도록 한다. 그 이외는 반복 처리 종료.
while(true){
Scanner sc3 = new Scanner(System.in);
System.out.print("숫자를 입력하시오. ( 단 2이상의 숫자만 허용!) : ");
int sut = sc3.nextInt();
int tot2 = 0;
int chogi = 1;
if(sut < 2){
System.out.println("2이상의 숫자를 입력해주세요.");
continue;
}else{
while(chogi <= sut){
// System.out.println(chogi);
tot2 += chogi;
chogi++;
}
System.out.println("1부터 입력한 수 " + sut + "까지의 합은 ?" + tot2);
System.out.print("계속 할까요? y/n");
String ip = sc3.nextLine();
if(ip == "y"){
continue;
}else{
System.exit(0);
}
}
}
}
}
'📁 공부정리 > JAVA' 카테고리의 다른 글
JAVA 160602-1 Array (0) | 2016.06.02 |
---|---|
ㄱㅇㅅ님 참고자료 arr (0) | 2016.06.02 |
JAVA 160601-2 for (0) | 2016.06.02 |
JAVA 160601-1 for (0) | 2016.06.02 |
ㄱㅇㅅ님 자료 참고(while) (0) | 2016.06.01 |