import java.util.*;
public class CF1 {
public static void main(String[] args) {
Stack st = new Stack();
Queue q = new LinkedList();
st.push(0);
st.push(1);
st.push(2);
q.offer(0);
q.offer(1);
q.offer(2);
while (!st.empty()) {
System.out.println(st.pop());
}
while (!q.isEmpty()) {
System.out.println(q.poll());
}
}
}
public class CF1 {
public static void main(String[] args) {
Stack st = new Stack();
Queue q = new LinkedList();
st.push(0);
st.push(1);
st.push(2);
q.offer(0);
q.offer(1);
q.offer(2);
while (!st.empty()) {
System.out.println(st.pop());
}
while (!q.isEmpty()) {
System.out.println(q.poll());
}
}
}
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Queue;
import java.util.Scanner;
class prc1 {
static Queue q = new LinkedList();
static final int Max_Size = 5;
public static void main(String[] args) {
System.out.println("help를 입력하면 도움말을 볼 수 있습니다.");
while (true) {
System.out.println(">>");
try {
Scanner s = new Scanner(System.in);
String input = s.nextLine().trim();
if("".equals(input)) continue;
if(input.equalsIgnoreCase("q")) {
System.exit(0);
} else if (input.equalsIgnoreCase("help")) {
System.out.println("help - 도움말을 보여줍니다");
System.out.println("q 또는 Q - 프로그램을 종료합니다");
System.out.println("histroy - 최근에 입력한 명령어를" + Max_Size + "개 보여줍니다");
} else if (input.equalsIgnoreCase("history")) {
int i = 0;
save(input);
LinkedList tmp = (LinkedList)q;
ListIterator it = tmp.listIterator();
while (it.hasNext())
System.out.println(++i + "." + it.next());
} else {
save(input);
System.out.println(input);
}
}catch(Exception e) {
System.out.println("입력 오류입니다");
}
}
}
public static void save(String input) {
if (!"".equals(input))
q.offer(input);
if (q.size() > Max_Size)
q.poll();
}
}
import java.util.ListIterator;
import java.util.Queue;
import java.util.Scanner;
class prc1 {
static Queue q = new LinkedList();
static final int Max_Size = 5;
public static void main(String[] args) {
System.out.println("help를 입력하면 도움말을 볼 수 있습니다.");
while (true) {
System.out.println(">>");
try {
Scanner s = new Scanner(System.in);
String input = s.nextLine().trim();
if("".equals(input)) continue;
if(input.equalsIgnoreCase("q")) {
System.exit(0);
} else if (input.equalsIgnoreCase("help")) {
System.out.println("help - 도움말을 보여줍니다");
System.out.println("q 또는 Q - 프로그램을 종료합니다");
System.out.println("histroy - 최근에 입력한 명령어를" + Max_Size + "개 보여줍니다");
} else if (input.equalsIgnoreCase("history")) {
int i = 0;
save(input);
LinkedList tmp = (LinkedList)q;
ListIterator it = tmp.listIterator();
while (it.hasNext())
System.out.println(++i + "." + it.next());
} else {
save(input);
System.out.println(input);
}
}catch(Exception e) {
System.out.println("입력 오류입니다");
}
}
}
public static void save(String input) {
if (!"".equals(input))
q.offer(input);
if (q.size() > Max_Size)
q.poll();
}
}
import java.util.*;
public class CF3 {
public static void main(String[] args) {
Object[] objA = {1,4,3,4,8,9,5,6,8};
Set set = new HashSet();
for(int i = 0; i < objA.length; i++) {
set.add(objA[i]);
}
System.out.println(set);
HashSet info = new HashSet();
info.add(new Person("지이츄", 25));
info.add(new Person("지이츄", 25));
System.out.println(info);
}
}
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString () { return name + ":" + age; }
public boolean equals(Object obj) {
if(!(obj instanceof Person)) return false;
Person p = (Person) obj;
return name.equals(p.name) && age == p.age;
}
public int hashCode() {
return Objects.hash(name, age);
}
// HashSet의 add메서드는 새로운 요소를 추가하기전에 기존에 저장된 요소와 같은 것인지 판별하기 위해 추가하려는 요소의 equals()와 hashCode()
// 를 목적에 맞게 오버라이딩 해야함
}
public class CF3 {
public static void main(String[] args) {
Object[] objA = {1,4,3,4,8,9,5,6,8};
Set set = new HashSet();
for(int i = 0; i < objA.length; i++) {
set.add(objA[i]);
}
System.out.println(set);
HashSet info = new HashSet();
info.add(new Person("지이츄", 25));
info.add(new Person("지이츄", 25));
System.out.println(info);
}
}
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString () { return name + ":" + age; }
public boolean equals(Object obj) {
if(!(obj instanceof Person)) return false;
Person p = (Person) obj;
return name.equals(p.name) && age == p.age;
}
public int hashCode() {
return Objects.hash(name, age);
}
// HashSet의 add메서드는 새로운 요소를 추가하기전에 기존에 저장된 요소와 같은 것인지 판별하기 위해 추가하려는 요소의 equals()와 hashCode()
// 를 목적에 맞게 오버라이딩 해야함
}
import java.util.*;
public class CF4 {
public static void main(String[] args) {
Set set = new TreeSet();
for(int i = 0; set.size() < 7; i++) {
int num = (int)(Math.random() * 45 + 1);
set.add(num);
}
System.out.println(set);
HashMap client = new HashMap();
client.put("지이츄","0724");
client.put("미미유","1217");
client.put("미미유","1997");
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("id와 비밀번호를 입력하세요");
System.out.println("id : ");
String id = s.nextLine().trim();
System.out.println("password : ");
String password = s.nextLine().trim();
if(!client.containsKey(id)) {
System.out.println("입력하신 id는 존재하지 않습니다. 다시 입력해주세요");
continue;
}
if(!(client.get(id)).equals(password)) {
System.out.println("비밀번호가 일치하지 않습니다, 다시 입력해주세요");
} else {
System.out.println("id와 비밀번호가 일치합니다");
break;
}
}
}
}
public class CF4 {
public static void main(String[] args) {
Set set = new TreeSet();
for(int i = 0; set.size() < 7; i++) {
int num = (int)(Math.random() * 45 + 1);
set.add(num);
}
System.out.println(set);
HashMap client = new HashMap();
client.put("지이츄","0724");
client.put("미미유","1217");
client.put("미미유","1997");
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("id와 비밀번호를 입력하세요");
System.out.println("id : ");
String id = s.nextLine().trim();
System.out.println("password : ");
String password = s.nextLine().trim();
if(!client.containsKey(id)) {
System.out.println("입력하신 id는 존재하지 않습니다. 다시 입력해주세요");
continue;
}
if(!(client.get(id)).equals(password)) {
System.out.println("비밀번호가 일치하지 않습니다, 다시 입력해주세요");
} else {
System.out.println("id와 비밀번호가 일치합니다");
break;
}
}
}
}
'Java > Java 문법 연습' 카테고리의 다른 글
제네릭스 연습 2 (2) | 2023.05.30 |
---|---|
제네릭스 연습 1 (0) | 2023.05.29 |
객체지향 연습 (1) | 2023.05.21 |
배열 연습 (0) | 2023.05.12 |
조건문과 반복문 연습 (0) | 2023.05.12 |