What's new

Help Bakit po nag iiba yung order ng output sa 1st and 2nd?

najamjam

Honorary Poster
Java:
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;

public class StudentList {
    public static void main(String[] args) {
        Map<String, String> students = new HashMap<>();
        Scanner sc = new Scanner(System.in);

        String num;
        String name;
            System.out.print("Enter student number 1: ");
            num = sc.nextLine();
            System.out.print("Enter first name 1: ");
            name = sc.nextLine();
            students.put(num, name);
            System.out.print("Enter student number 2: ");
            num = sc.nextLine();
            System.out.print("Enter first name 2: ");
            name = sc.nextLine();
            students.put(num, name);
            System.out.print("Enter student number 3: ");
            num = sc.nextLine();
            System.out.print("Enter first name 3: ");
            name = sc.nextLine();
            students.put(num, name);

            System.out.println("Student List:");
        for (Map.Entry x : students.entrySet()) {
            System.out.println(x.getKey() + " " + x.getValue());
        }
            students.remove(num, name);

            System.out.print("Enter your student number: ");
            String num3 = sc.nextLine();
            System.out.print("Enter your first name: ");
            String name3 = sc.nextLine();
            students.put(num3, name3);

            System.out.println("Student List:");
        for (Map.Entry x : students.entrySet()) {
            System.out.println(x.getKey() + " " + x.getValue());
        }
    }
}
 

Attachments

Java:
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

public class StudentList {
    public static void main(String[] args) {

        HashMap<String, String> students = new HashMap<>();

        Scanner sc = new Scanner(System.in);

        String num = "";
        String name = "";

        for(int i = 0 ; i < 3 ; i++){
            System.out.print("Enter student number "+(i+1)+": ");
            num = sc.nextLine();
            System.out.print("Enter first name "+(i+1)+": ");
            name = sc.nextLine();
            students.put(num, name);
        }
            
            System.out.println("Student List:");


        for (Iterator<Map.Entry<String, String>> it = students.entrySet().iterator(); it.hasNext();) {
            HashMap.Entry x = it.next();
            System.out.println(x.getKey() + " " + x.getValue());
        }

            students.remove(num, name);

            System.out.print("Enter your student number: ");
            String num3 = sc.nextLine();

            System.out.print("Enter your first name: ");
            String name3 = sc.nextLine();
            students.put(num3, name3);

            System.out.println("Student List:");


        for (Map.Entry x : students.entrySet()) {
            System.out.println(x.getKey() + " " + x.getValue());
        }
    }
}
1642171508868.png
 

Attachments

Last edited:

Similar threads

Back
Top