๐Java-test
๐ ๋ฌธ์ 1
1~100๊น์ง ํฉ๊ณ
๐(๊ฒฐ๊ณผ)

โ(์
๋ ฅ)
public class Test1 {
public static void main(String[] args) {
int sum = 0;
for(int i=1;i<=100;i++){
sum += i;
}
System.out.println("1~100๊น์ง์ ํฉ : "+sum);
}
}
๐ ๋ฌธ์ 2
Scanner ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํ ์ฌ์น์ฐ์ฐ ํ๋ก๊ทธ๋จ
๐(๊ฒฐ๊ณผ)

โ(์
๋ ฅ)
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a;
int b;
String c;
System.out.println("์ ์ ์ฌ์น์ฐ์ฐ ํ๋ก๊ทธ๋จ์ ํ์ํ ์ฐ์ฐ์๋ฅผ ์
๋ ฅํ์์ค");
System.out.println("( +, -, *, / ) >>");
c = scanner.next();
System.out.println("์ฐ์ฐํ ์ ์๋ฅผ ์
๋ ฅํ์์ค >>");
a = scanner.nextInt();
System.out.println("์ฐ์ฐํ ์ ์๋ฅผ ์
๋ ฅํ์์ค >>");
b = scanner.nextInt();
if(c.equals("+")){
System.out.println(a + "+"+ b +"=" + (a+b));
}else if(c.equals("-")){
System.out.println(a + "-"+ b +"=" + (a-b));
}else if(c.equals("*")){
System.out.println(a + "*"+ b +"=" + (a*b));
}else if(c.equals("/")){
double rst = (double)a/(double)b;
System.out.println(a + "/"+ b +"=" + rst);
}else{
System.out.println("์๋ชป๋ ๊ฐ์ ์
๋ ฅํ์ต๋๋ค.");
}
scanner.close();
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
๐ ๋ฌธ์ 3
Scanner ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํ ์ ๋ณดํ์ธ&๋ถ์ ํ๋ก๊ทธ๋จ
์๋ ์์ ํ๋ฉด๊ณผ ๊ฐ์ด ํ์์ ์ด๋ฆ๊ณผ ์ ์๋ฅผ ์
๋ ฅ ๋ ํ์์์ ๋ง๊ฒ ์
๋ ฅํ๊ณ ์
๋ ฅ๋ ํ์์ ์ด๋ฆ๊ณผ ์ ์๋ฅผ ๋ณด๊ฑฐ๋ ๋ถ์ํ ์ ์๋๋ก ์์ฑ
๐(๊ฒฐ๊ณผ)

โ(์
๋ ฅ)
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
boolean run= true;
Scanner sc = new Scanner(System.in);
String[][] students = null;
int studentNum = 0;
while (run) {
System.out.println("--------------------------------------------------");
System.out.println("1.ํ์์ | 2.ํ์์ ๋ณด์
๋ ฅ| 3.ํ์์ด๋ฆ๊ณผ์ ์๋ณด๊ธฐ | 4.๋ถ์ |5.์ข
๋ฃ");
System.out.println("--------------------------------------------------");
System.out.print("์ ํ > ");
int menuNo = sc.nextInt();
switch(menuNo) {
case 1 :
System.out.print("ํ์์ > ");
studentNum = sc.nextInt();
students = new String [studentNum][2];
break;
case 2 :
for(int i=0 ;i<studentNum; i++) {
sc.nextLine();
System.out.print("์ด๋ฆ > ");
students[i][0] = sc.nextLine();
System.out.print("์ ์ > ");
students[i][1] =sc.nextLine();
}
break;
case 3 :
for(int i=0 ;i<studentNum; i++) {
System.out.println("์ด๋ฆ : "+students[i][0]+" , ์ ์ : "+students[i][1]);
}
System.out.println();
break;
case 4 :
int sum =0;
int maxScore =-1;
int maxIndx =0;
for(int i= 0; i< studentNum ; i++) {
sum+=Integer.parseInt(students[i][1]);
if(Integer.parseInt(students[i][1]) > maxScore) {
maxScore = Integer.parseInt(students[i][1]);
maxIndx = i;
}
}
System.out.println(" ์ต๊ณ ๊ฐ : "+ maxScore+" ์ด๋ฆ :"+students[maxIndx][0]);
System.out.println(" ํ๊ท ๊ฐ : "+ (double)sum/studentNum);
break;
case 5 :
run = false;
}
}
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
๐ ๋ฌธ์ 4
ํ๋ก๊ทธ๋จ์ด ์คํ ๋ ์ ์๋ Book ํด๋์ค ํ์ผ์ ์ค๊ณ
์๋์ ์ฑ
๊ด๋ฆฌ ํ๋ก๊ทธ๋จ์ ์ผ๋ถ๋ถ์ด ์คํ๋ ์ ์๋๋ก ์ ํ์ผ ์์ฑ
๐(๊ฒฐ๊ณผ)

import java.util.Scanner;
public class Test4 {
static Book [] books = new Book[10];
static Scanner sc = new Scanner(System.in);
static int insertCnt =0;
public static void main(String[] args) {
boolean run = true;
while (run) {
System.out.println("---------------------------------------");
System.out.println("1. ๋์ ์ ๋ณด ์
๋ ฅ| 2. ์ฑ
๊ฒ์ | 3. ์ข
๋ฃ");
System.out.println("---------------------------------------");
System.out.print("๋ฉ๋ด ์ ํ >");
int menuNo = sc.nextInt();
switch (menuNo) {
case 1:
insertBook();
break;
case 2 :
searchingBook();
break;
case 3:
run = false;
break;
}
}
sc.close();
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
static void insertBook(){
if (insertCnt < books.length) {
insertCnt++;
sc.nextLine();
System.out.print("๋์๋ช
์
๋ ฅ : ");
String title = sc.nextLine();
System.out.print("์ ์ ์
๋ ฅ : ");
String author = sc.nextLine();
System.out.print("์ถํ์ฌ ์
๋ ฅ : ");
String publish = sc.nextLine();
System.out.print("๊ฐ๊ฒฉ ์
๋ ฅ : ");
int price = sc.nextInt();
books[insertCnt-1] = new Book(title, author, publish, price);
System.out.println("์
๋ ฅ๋จ ");
}else { System.out.println("๋ ์ด์ ์
๋ ฅํ ์ ์์ต๋๋ค. "); }
}
static void searchingBook(){
if(insertCnt > 0){
System.out.print("๊ฒ์ํ ์ฑ
์ ๋ชฉ์ ์
๋ ฅ :");
sc.nextLine();
String searchinTitle = sc.nextLine();
int i;
for(i=0; i<insertCnt;i++){
if(searchinTitle.equalsIgnoreCase(books[i].getTitle())){
System.out.println("์ ์ : "+books[i].getAuthor()+" ์ถํ์ฌ : "+books[i].publish+" ๊ฐ๊ฒฉ: "+books[i].price );
break;
}
}
if(i>= insertCnt) System.out.println("๊ฒ์ ์ฑ
์ด ์์ต๋๋ค");
}
}
}
โ(์
๋ ฅ)
public class Book {
String title;
String author;
String publish;
int price;
public Book(String title, String author, String publish, int price) {
super();
this.title = title;
this.author = author;
this.publish = publish;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
๐ ๋ฌธ์ 5
๊ฐ๋จํ ์ฃผ์ฌ์ ๊ฒ์์ ๋ง๋ค๊ธฐ
์๋์ ๊ฐ์ด ๋์ํ๋๋ก ์์ฑ
๐(๊ฒฐ๊ณผ)

โ(์
๋ ฅ)
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
System.out.println("๊ฒ์ ์ค๋ช
. ๊ฒ์ ์ข
๋ฃ๋ exit ์
๋ ฅ");
boolean run =true;
Scanner sc = new Scanner(System.in);
while(run) {
System.out.println("์ฃผ์ฌ์๊ฐ ๊ตด๋ฌ๊ฐ๋๋ค. Enterํค๋ฅผ ๋๋ฅด๋ฉด ์ฃผ์ฌ์ ๊ฐ์ด ๋์ต๋๋ค.");
sc.nextLine();
int num1 = (int)(Math.random()*6)+1;
System.out.println("์ฒซ๋ฒ์งธ ์ฃผ์ฌ์ ๊ฐ : "+ num1);
System.out.println("์ฃผ์ฌ์๊ฐ ๊ตด๋ฌ๊ฐ๋๋ค. Enterํค๋ฅผ ๋๋ฅด๋ฉด ์ฃผ์ฌ์ ๊ฐ์ด ๋์ต๋๋ค.");
sc.nextLine();
int num2 = (int)(Math.random()*6)+1;
System.out.println("๋๋ฒ์งธ ์ฃผ์ฌ์ ๊ฐ : "+ num2);
System.out.println("์ฃผ์ฌ์๊ฐ ๊ตด๋ฌ๊ฐ๋๋ค. Enterํค๋ฅผ ๋๋ฅด๋ฉด ์ฃผ์ฌ์ ๊ฐ์ด ๋์ต๋๋ค.");
sc.nextLine();
int num3 = (int)(Math.random()*6)+1;
System.out.println("์ธ๋ฒ์งธ ์ฃผ์ฌ์ ๊ฐ : "+ num1);
if(num1== num2 && num2 == num3) {
System.out.println("๋น๊ณ !!!!");
}
System.out.println("๊ฒ์ ์ข
๋ฃ๋ฅผ ์ํ์๋ฉด exit๋ฅผ ์
๋ ฅํ์๊ณ , ๊ณ์ํ์ค ๊ฒฝ์ฐ Enterํค๋ฅผ ๋๋ฌ์ฃผ์ธ์");
String result = sc.nextLine();
if(result.equalsIgnoreCase("exit")) run =false;
}
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
}