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");
}
}
Ausgang:
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
warum nicht die letzten 3 bis 1 oder 0 zugewiesen werden?
'y = y ++' ist völlig redundant. 'y ++' ist das Äquivalent von 'y = y + 1' –
' y = y ++ 'erhöht nicht einmal' y' – khelwood
Die Änderung von 'y' ist sowieso sinnlos. Sie weisen in jeder Iteration 'inp' zu' y' zu. Sie möchten sicher ** ** i ** als Array-Index verwenden. – Holger