카테고리 없음

11.JAVA 예제 정리 [while문]

woogy99 2024. 11. 28. 18:30

//1. 구구단 중 원하는 단수 입력 받아 결과를 출력하기

 

import java.util.Scanner;

 

public class 문제1 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("원하는 구구단 단수를 입력 : ");

int dan=scan.nextInt();

int i=1;

while(i<=9){

 

System.out.println(dan+"x"+i+"="+(dan*i));

i++;

 

}

}

}


//2. 1~100사이 정수 중에서 8의 배수 출력하기

 

public class 문제2 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int i=1;

while(i<=100) {

if(i%8==0) {

System.out.print(i+" ");

}

i++;

}

}

 

}


//3. 1~100사이 정수 중에서 4의 배수이거나 7의 배수인 정수를 출력하기

 

public class 문제3 {

public static void main(String[] args) {

int i=1;

 

while(i<=100) {

 

if(i%4==0 || i%7==0) {

System.out.print(i+" ");

}

i++;

}

}

}


//4. 1~100사이 정수중에서 일의 자리 수가 3 또는 6 또는 9인 정수를 출력하기

 

public class 문제4 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int i = 1;

while (i <= 100) {

if ((i % 10 == 3) || (i % 10 == 6) || (i % 10 == 9)) {

System.out.print(i + " ");

}

i++;

}

}

 

}


//5. 사용자에게 문자열을 입력받아 모음의 개수를 출력하기 (a, e, i, o, u)

 

import java.util.Scanner;

 

public class 문제5 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("영어 문자열을 입력하세요");

Scanner scan = new Scanner(System.in);

String str = scan.nextLine();

int j = 1;

int a = 0,e=0,i=0,o=0,u=0;

// System.out.println(str.length());

while (j <= str.length()) {

 

char c=str.charAt(j-1);

if (c== 'a') {

a++;

} else if (c== 'e') {

e++;

} else if (c== 'i') {

i++;

} else if (c == 'o') {

o++;

} else if (c == 'u') {

u++;

}

j++;

}

System.out.println("모음의 개수는 " +(a+e+i+o+u));

}

 

}


//사용자에게 1 ~ 9사이의 정수를 입력받아 2자리의 자연수 중 각 자리수의 합이 입력값과 동일한 수 출력하기

//입출력예) 6 => 15 24 33 42 51 60 

 

 

import java.util.Scanner;

public class 문제6 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("1~9 사이의 정수를 입력하세요");

Scanner scan=new Scanner(System.in);

int ja=scan.nextInt();

int i=10;

while(i<=99) {

 

int a=i/10; //앞자리

int b=i%10; //뒷자리

if(a+b==ja) {

System.out.println(i+"");

}

i++;

}

}

 

}


//7. 10부터 20까지 순서대로 출력

 

 

public class 문제7 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

 

int i=10;

while(i<=20) {

System.out.print(i+" ");

i++;

}

}

 

}


//7. 8. 10부터 20까지 역순으로 출력

 

 

public class 문제8 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

 

int i=20;

while(i>=10) {

System.out.print(i+" ");

i--;

}

}

 

}


//9. 사용자가 입력한 5개의 정수 중 가장 큰 값을 출력하기

 

import java.util.Scanner;

public class 문제9 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

 

System.out.println("5개의 정수를 입력하세요");

Scanner scan=new Scanner(System.in);

 

int max=0;

int i=1;

while(i<=5) {

int num=scan.nextInt();

 

if(num>max) {

max=num;

}

i++;

}

System.out.println("가장 큰 정수는 "+max);

}

 

}


//10. 사용자에게 문자열 3개를 입력받아

//길이값의 합을 출력하고, 가장 긴 문자열을 출력하기

 

 

import java.util.Scanner;

public class 문제10 {

public static void main(String[] args) {

System.out.println("문자열 3개를 입력하세요");

Scanner scan=new Scanner(System.in);

int i=1, sum=0, num1=0;

while(i<=3) {

String st=scan.nextLine();

 

if(num1<st.length()) {

num1=st.length();

}

sum+=st.length();

i++;

}

System.out.println("길이의 합은 : "+sum);

System.out.println("가장 긴 문자열은 : "+num1);

}

}