자바 반복문 (while 문)
JAVA반응형
package ch2;
public class study1 {
public static void main(String[] args)
{
int i = 1;
while (i<=100)
{
System.out.print(i+" ");
i+=2;
}
}
}
package ch2;
public class study1 {
public static void main(String[] args)
{
int i =1;
String num="", left ="";
while (i<=30)
{
num = num + String.format("%3d",i);
left = left + String.format("%3d",i%3);
i++;
}
System.out.println(num);
System.out.println(left);
}
}
계산기
package ch2;
import java.util.Scanner;
public class study1 {
public static void main(String[] args)
{
Scanner a = new Scanner (System.in);
int b,c;
char d;
while (true)
{
System.out.println("예) 1 + 2 / 두 수에 0 대입시 중단");
b= a.nextInt();
d= (a.next()).charAt(0);
c= a.nextInt();
if (b==0 && c==0)
break;
switch (d)
{
case '+':
System.out.printf("%d + %d = %d",b,c,b+c); break;
case '-':
System.out.printf("%d - %d = %d",b,c,b-c); break;
case '*':
System.out.printf("%d * %d = %d",b,c,b*c); break;
case '/':
System.out.printf("%d / %d = %d",b,c,b/c); break;
case '%':
System.out.printf("%d % %d = %d",b,c,b%c); break;
default :
System.out.println("잘못된 입력입니다.");
}
}
}
}
반응형
'JAVA' 카테고리의 다른 글
자바 배열 (0) | 2019.12.15 |
---|---|
자바를 이용한 가위바위보 게임 (0) | 2019.12.10 |
자바 랜덤 (random) (0) | 2019.12.08 |
자바 반복문 (for문) (0) | 2019.12.08 |
자바 조건문 (SWITCH ~ CASE) (0) | 2019.12.07 |