import java.util.Scanner;
import java.util.Arrays;
public class AttendanceManager {
public static void main(String args[])
{
System.out.println("Enter the number of students that are to be recorded.");
Scanner studentNum = new Scanner(System.in);
int x = studentNum.nextInt();
final int number[] = new int[x];
for(int i=0; i<x; i++)
{
System.out.println("Enter 1 if the student is present and 0 if the student is not.");
final Scanner attendance = new Scanner(System.in);
int inp = attendance.nextInt();
int y = inp;
switch (inp)
{
case 1:
number[y] = 1;
y = y++;
break;
case 0:
number[y] = 2;
y = y++;
break;
default:
System.out.println("Please enter 1 or 0.");
i--;
}
}
System.out.println("Total Students: " + number.length);
for(int k=0; k<number.length; k++)
{
if (number[k] == 1)
System.out.println("Student " + (k+1) + " is " + "present.");
else if (number[k] == 2)
System.out.println("Student " + (k+1) + " is " + "absent.");
else
System.out.println("error");
}
}
출력 :
Enter the number of students that are to be recorded.
5
Enter 1 if the student is present and 0 if the student is not.
1
Enter 1 if the student is present and 0 if the student is not.
0
Enter 1 if the student is present and 0 if the student is not.
1
Enter 1 if the student is present and 0 if the student is not.
1
Enter 1 if the student is present and 0 if the student is not.
0
Total Students: 5
Student 1 is absent.
Student 2 is present.
error
error
error
왜 지난 3가 1 또는 0에 할당되지 않습니다? 잘못된 배열 인덱스를 사용
'y = y ++'는 완전히 중복됩니다. 'y ++'는'y = y + 1'과 같습니다. –
'y = y ++'는'y '를 증가시키지 않습니다. – khelwood
어쨌든'y'의 수정은 무의미합니다. 모든 반복에서'in'을'y'에 할당합니다. 반드시 ** i' **를 대신 배열 인덱스로 사용하고 싶습니다. – Holger