문제1. for을 이용한 구구단 출력문
class if_5
{
public static void main(String[] args)
{
for(int i=2 ; i<=9;i++){
for(int j = 1; j<=9;j++){
System.out.println(i+"*"+j+"="+(i*j));
}
System.out.println();
}
}
}
문제2. 수를 두개 입력 받아 구구단 출력하기(큰수를 먼저 입력하던 작은수를 먼저 입력하던 그사이의 단이 출력되도록)
예) 입력받은 수 : 8,5 이면 구구단 5단 부터 8단 출력하기.
import java.util.Scanner;
class If_Te
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("수 입력 : ");
int a = sc.nextInt();
System.out.println("수 입력 : ");
int b = sc.nextInt();
int max = a>b ? a : b;
int min = a>b ? b : a;
for(int i= min ; i<=max; i++){
for(int j =1 ; j<=9 ; j++){
System.out.println(i+"*"+j +"="+(i*j));
}
System.out.println();
}
}
}
//A와 B를 교체하고싶다면?
int A, b,
int Tem
Tem = A;
A=B;
B = Tem; // 이런형식으로 값을 서로 변경(교환) 가능
위의방법을 이용한 풀이방법
import java.util.Scanner;
class if_5
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("수 입력 : ");
int a = sc.nextInt();
System.out.println("수 입력 : ");
int b = sc.nextInt();
if(a>b){
int temp=0;
temp=a;
a=b;
b=temp;
}
for(int i=a ; i<=b; i++){
for(int j =1 ; j<=9 ; j++){
System.out.println(i+"*"+j +"="+(i*j));
}
System.out.println();
}
}
}
System.in.read()이용하는 방법
class if_Test
{
public static void main(String[] args) throws Exception
{
System.out.println("수 입력 : ");
int a = System.in.read()-'0';
System.in.read();
System.in.read();
//in.read()가 엔터값 즉 아스키코드10 ,13을 받기때문에 버퍼를 비워주는 것.
System.out.println("수 입력 : ");
int b = System.in.read()-'0';
if(a>b){
int temp=0;
temp=a;
a=b;
b=temp;
}
for(int i=a ; i<=b; i++){
for(int j =1 ; j<=9 ; j++){
System.out.println(i+"*"+j +"="+(i*j));
}
System.out.println();
}
}
}
'KH > JAVA' 카테고리의 다른 글
#8 분기문(break, continue, return) (0) | 2022.07.11 |
---|---|
#7 while문 (반복문) / do~while (0) | 2022.07.11 |
#5 Switch문,Math.random() (0) | 2022.07.08 |
#4 cmd(명령 프롬프트) (0) | 2022.07.07 |
#3 if else (0) | 2022.07.07 |