본문 바로가기

Java/과제

연습문제 3장

첫번째 숫자로부터 두번째 숫자의 사이의 모둔 짝수의 합을 구하시오!
// Exercise 3-1
import java.io.*;

class Exercise1
{
  public static void main (String[] args) throws IOException
  {
    BufferedReader stdin = 
                     new BufferedReader(new InputStreamReader(System.in));
    String str;
    int iNum1;
    int iNum2;
    int iCnt;
    int iSum=0;

    System.out.print("Enter first Number : ");
    str = stdin.readLine();
    iNum1 = Integer.parseInt(str);
    
    System.out.print("Enter Second Number : ");
    str = stdin.readLine();
    iNum2 = Integer.parseInt(str);

    for(iCnt=iNum1; iCnt <= iNum2; iCnt++)
    {
      if((iCnt%2== 0)
      {
        iSum+=iCnt;
      }  
    }
    System.out.print("the sum of even between
                      first numver and second number : "
 + iSum);
  }
}
 구구단을 출력하라!
// Exercise 3-2
import java.io.*;

class Exercise2
{
  public static void main (String[] args)
  {
    int iCnt1;
    int iCnt2;
    int iSum;

    for(iCnt1=110 > iCnt1; ++iCnt1)
    {
      for(iCnt2=210 > iCnt2; ++iCnt2)
      {  
        iSum=iCnt1*iCnt2;
        System.out.print(iCnt2 + "*");
        System.out.print(iCnt1 + "=" + iSum + "\t");
      }
      System.out.println();
    }
  }
}


계산기 프로그램으로 해당 결과를 출력하는 프로그램을 만드시오!
// Exercise 3-3
import java.io.*;

class Exercise3
{
  public static void main (String[] args) throws IOException
  {
    BufferedReader stdin = 
                     new BufferedReader(new InputStreamReader(System.in));
    String str;
    int iSelect;
    int iNum1;
    int iNum2;

    do{
      System.out.println("Choise one of the following operations!");
      System.out.println("\t1. Add \n\t2. Sub \n\t3. Mul
                          \n\t4. Div \n\t5. Quit"
);
      System.out.print("Choise : ");

      str = stdin.readLine();
      iSelect = Integer.parseInt(str);

      switch(iSelect)
      {
        case 1:
          System.out.print("Input first number : ");
          str = stdin.readLine();
          iNum1 = Integer.parseInt(str);
          
          System.out.print("Input second number : ");
          str = stdin.readLine();
          iNum2 = Integer.parseInt(str);

          System.out.println("Result : " + (iNum1 + iNum2));
          break;

        case 2:
          System.out.print("Input first number : ");
          str = stdin.readLine();
          iNum1 = Integer.parseInt(str);
          
          System.out.print("Input second number : ");
          str = stdin.readLine();
          iNum2 = Integer.parseInt(str);

          System.out.println("Result : " + (iNum1 - iNum2));
          break;

        case 3:
          System.out.print("Input first number : ");
          str = stdin.readLine();
          iNum1 = Integer.parseInt(str);
          
          System.out.print("Input second number : ");
          str = stdin.readLine();
          iNum2 = Integer.parseInt(str);

          System.out.println("Result : " + (iNum1 * iNum2));
          break;

        case 4:
          System.out.print("Input first number : ");
          str = stdin.readLine();
          iNum1 = Integer.parseInt(str);
          
          System.out.print("Input second number : ");
          str = stdin.readLine();
          iNum2 = Integer.parseInt(str);

          System.out.println("Result : " + (double)(iNum1/iNum2));
          break;
      }      
    } while(iSelect != 5);
  }
}