Java - Test 1

Joy_allยท2021๋…„ 1์›” 22์ผ

Java

๋ชฉ๋ก ๋ณด๊ธฐ
1/1
post-thumbnail

๐Ÿ“’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 [] names = null; //new String[100];
		//int [] scores = null;
		
		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();
			    //names = new String[studentNum];
			    //scores = new int[studentNum];
			    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();//Scanner์ž…๋ ฅ์„ ์œ„ํ•ด ๋„ฃ์€ ์˜๋ฏธ ์—†๋Š” ์ฝ”๋“œ
			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();//Scanner์ž…๋ ฅ์„ ์œ„ํ•ด ๋„ฃ์€ ์˜๋ฏธ ์—†๋Š” ์ฝ”๋“œ
			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("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
	}

}
profile
beginner

0๊ฐœ์˜ ๋Œ“๊ธ€