다섯째 날 스터디

정혅·2024년 2월 13일

더 조은 아카데미

목록 보기
9/76
post-thumbnail

Q1. class를 생성하여 국어, 수학, 영어 점수를 입력받고 평균을 구하시오

  • 시나리오

    • 스캐너 생성

    • 멤버 변수 생성 > 멤버 변수 선언

    • 멤버 메소드 > 과목 초기화 메소드 / 평균 메소드 / 상태 출력 메소드(국영수 점수, 평균)

import java.util.Scanner;
class Subject{ //멤버 메소드

	double kor, math, eng, avr; //이렇게 간편하게 가능
	//double math;
	//double eng;
	//double avr;
	
	public void initSub(double kor, double math, double eng){ //this를 사용해 멤버변수와 지역변수 구
		
		this.kor = kor;
		this.math = math;
		this.eng = eng;
	}
	
	public void averSub(){
		avr = (kor + math + eng) / 3;
	}
	
	public void printSub(){
		System.out.println("국어 점수 : " + kor + " / 영어 점수 : " + eng + " / 수학 점수 : " + math + " / 평균 점수 : " + avr);
	}
}

class SubjectMain{
	public static void main(String[]args){
	Scanner sc = new Scanner(System.in);
	

	double ko = 0, ma = 0, en = 0;
	System.out.println("국어 점수를 입력하세요 : ");
	ko = sc.nextDouble();
	
	System.out.println("수학 점수를 입력하세요 : ");
	ma = sc.nextDouble();
	
	System.out.println("수학 점수를 입력하세요 : ");
	en = sc.nextDouble();

	Subject r1 = new Subject();
	r1.initSub(ko, ma, en);
	r1.averSub();
	r1.printSub();
	}
}
  • Scanner를 어디에 넣어야할지 몰랐어서 시간이 좀 걸렸음

  • main메소드안에서 또 값을 입력받을 지역변수들을 따로 선언해주지 않아 오류가 계속 났다.


Q2. class 선언을 이용해 등차수열을 나열하시오 - 공차: 3

  • 시나리오

    • 멤버 변수 생성 > 변수 선언

    • 멤버 메소드 > 초기화 / 일정한 수 더해지는 메서드 공식 / 출력

class Isometric{
   int num;
   int cnt;
   
   public void initIso(int n){
   
      num = n;
   }
   public void creaseIso(){
      
      for(; num <= 50; num += 3){
         if(num % 3 != 0){
         printIso();
         }   
      }
   }
   public void printIso(){

      System.out.println("등차수열 : " + num);
   }
}
class IsometricMain{
   public static void main(String[]args){
      Isometric i1 = new Isometric();
      i1.initIso(1);
      i1.creaseIso();
      
   }
}


홍석님이 하신 첫항, 공차, 원하는 항까지를 입력받아 재귀함수로 등차수열 구하기

// 등차수열 재귀함수로

import java.util.Scanner;
class Arithmetic
{
   public static void main(String[] args)
   {
      Scanner sc1 = new Scanner(System.in);
      System.out.println("첫항 입력 : ");
      int a = sc1.nextInt();

      Scanner sc2 = new Scanner(System.in);
      System.out.println("공차 입력 : ");
      int d = sc2.nextInt();

      Scanner sc3 = new Scanner(System.in);
      System.out.println("몇번째 항까지? : ");
      int n = sc3.nextInt();
      arithmetic(a, d, n);

      
   }   
   public static void arithmetic(int a, int d, int n)
   {
         if (n == 1)
         {
            System.out.print(a + "  ");   //System.out.print(a + " ");
         } 


         else
         {
            int resultOfArithmetic = a + (n-1)*d;
            
            arithmetic(a, d, n-1);
            System.out.print(resultOfArithmetic + "  ");

         }
             
   }   
}

Q3. class선언을 사용해 시작 항의 수 , 공차, 출력하고싶은 n번째 항을 입력받아 출력하시오

  • 이건 다음 스터디 때 진행할 예정

0개의 댓글