DoWhile
package kr.s03.operation;
public class DoWhileMain {
public static void main(String[] args) {
int su = 0;
String str = "Hello world";
//선 처리, 후 비교 실행 후 조건 체크
do {
System.out.println(str);
}while(su++<5);
System.out.println("=====================");
int su2 = 0;
//선 비교, 후 처리
while(su2++ <5) {
System.out.println(str);
}
}
}
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
=====================
Hello world
Hello world
Hello world
Hello world
Hello world
실습
package kr.s03.operation;
public class ScoreMain {
public static void main(String[] args) {
java.util.Scanner input =
new java.util.Scanner(System.in);
int korean,english,math,sum;
char grade;
float avg;
do {
System.out.print("국어:");
korean = input.nextInt();
}while(korean <0 || korean>100);
do {
System.out.print("영어:");
english = input.nextInt();
}while(english <0 || english>100);
do {
System.out.print("수학:");
math = input.nextInt();
}while(math <0 || math>100);
//총점 구하기
sum = korean+english+math;
//평균 구하기
avg = sum /3.0f ;
input.close();
//등급 구하기
switch((int)(avg/10)) {//avg/10가 연산의 결과 int(캐스트 연산자)를 사용해 정수로 형변환을 해야지 값이 나옴.
case 10:
case 9:
grade = 'A'; break;
case 8:
grade = 'B'; break;
case 7:
grade = 'C'; break;
case 6:
grade = 'D'; break;
default:
grade = 'F';
}
System.out.println("\\n");//단순 줄바꿈
System.out.printf("총점 = %d%n",sum);
System.out.printf("평균 = %,2f%n", avg);
System.out.printf("등급 =%c학점 ", grade);
input.close();
}
}
국어:86
영어:65
수학:80
총점 = 231
평균 = 77.000000
등급 =C학점