JX405기_비트/Java
Day02-3 학생 관리 프로그램 작성
_하루살이_
2023. 1. 12. 19:33
무한 루프를 사용하여
사용자가 입력을 누를 때마다 새로운 학생의 정보가 입력이 되고
출력을 누를때마다 맨 마지막으로 입력된 학생의 정보가 출력되는 프로그램을
작성해보시오.
Student 클래스
package day0110;
public class Student {
public int id;
public String name;
public int korean;
public int english;
public int math;
}
1. ScannerUtil 활용하기
package util;
import java.util.Scanner;
//Scanner 클래스를 통해 입력을 받을 시에 도움이 될만한 static 메소드를 모아둔 클래스
public class ScannerUtil {
// 1. 입력시 사용자에게 보여줄 메세지 출력을 담당할 메소드
public static void printMessage(String message){
System.out.println(message);
System.out.println("> ");
}
// 2. 스캐너 버그를 미리 방지하는 nexLine()
public static String nextLine(Scanner scanner, String message){
printMessage(message);
String temp = scanner.nextLine();
if(temp.isEmpty()){
temp = scanner.nextLine();
}
return temp;
}
// 3. 사용자로부터 정수 입력을 담당하는 nexInt()
public static int nextInt(Scanner scanner, String message){
printMessage(message);
int temp = scanner.nextInt();
return temp;
}
//파라미터의 순서가 중요 파라미터의 순서가 다른 경우 같은 함수이름을 사용할수 있음
// 4. 사용자로부터 특정 범위의 정수 입력을 담당하는 nextInt()
public static int nextInt(Scanner scanner, String message, int min, int max){
int temp = nextInt(scanner, message);
while (temp < min || temp > max) {
System.out.println("잘못 입력하셨습니다.");
temp = nextInt(scanner, message);
}
return temp;
}
}
2. while를 통한 무한루프 풀기
package day0110;
import util.ScannerUtil;
import java.util.Scanner;
public class Ex04Gradebook02 {
public static void main(String[] args) {
Student student = new Student();
Scanner scanner = new Scanner(System.in);
boolean inputSwitch = false;
while (true){
String message = "1. 입력 2. 출력 3. 종료";
int userChoice = ScannerUtil.nextInt(scanner, message);
if(userChoice == 1){
insertInfo(scanner, student);
inputSwitch = true;
} else if (userChoice == 2){
if(inputSwitch){
printInfo(student);
} else {
System.out.println("아직 입력된 학생의 정보가 존재하지 않습니다.");
}
} else if (userChoice == 3) {
System.out.println("사용해주셔서 감사합니다.");
break;
}
}
scanner.close();
}
public static void insertInfo(Scanner scanner, Student student){
String message;
message = "학생의 번호를 입력하시오.";
student.id = ScannerUtil.nextInt(scanner, message);
message = "학생의 이름을 입력하시오.";
student.name = ScannerUtil.nextLine(scanner, message);
message = "학생의 국어 점수를 입력하시오.";
student.korean = ScannerUtil.nextInt(scanner, message, 0 , 100);
message = "학생의 영어 점수를 입력하시오.";
student.english = ScannerUtil.nextInt(scanner, message, 0 , 100);
message = "학생의 수학 점수를 입력하시오.";
student.math = ScannerUtil.nextInt(scanner, message, 0, 100);
}
public static void printInfo(Student student){
System.out.printf("번호 : %d번 이름: %s\n", student.id, student.name);
System.out.printf("국어 : %d점 영어 : %d점 수학 : %d점\n", student.korean, student.english, student.math);
System.out.printf("총점 : %d점 평균 : %.2f점\n", calculateSum(student), caculateAverage(student));
}
public static int calculateSum(Student s){
return s.korean + s.english + s.math;
}
public static double caculateAverage(Student s){
return (double)calculateSum(s) / 3;
}
}
while를 활용한 무한루프
while (true){
String message = "1. 입력 2. 출력 3. 종료";
int userChoice = ScannerUtil.nextInt(scanner, message);
// 사용자가 누르는 숫자를 받는 변수 userChoice
if(userChoice == 1){ // 1번을 누를때 학생의 정보를 입력하는 메소드 실행
insertInfo(scanner, student);
inputSwitch = true;
} else if (userChoice == 2){
if(inputSwitch){ // inputSwitch 가 true 일 경우 출력 메소드 실행
printInfo(student);
} else {
System.out.println("아직 입력된 학생의 정보가 존재하지 않습니다.");
}
} else if (userChoice == 3) {
System.out.println("사용해주셔서 감사합니다.");
break;
}
}