본문 바로가기
IT/프로그래밍

JAVA 프로그래밍 기초부터 활용까지 3장 소스코드

by nutrient 2021. 5. 22.
728x90
728x170

 

//예제03-01 IFTest.java
package ch03;
import javax.swing.JOptionPane;

public class IFTest{
	public static void main(String args[]){
		int  x;
		String a;
		
		a=JOptionPane.showInputDialog("수를 입력하시오");
		x=Integer.parseInt(a);
		
		if(x<=10)
			JOptionPane.showMessageDialog( null,"입력한 값 = "+x);
		
		System.exit(0);
	}
}

 

//예제03-02 IFEvenTest.java
package ch03;
import javax.swing.JOptionPane;

public class IFEvenTest{
	public static void main(String args[]){
		int  x;
		String a;
		String str1;
		
		a=JOptionPane.showInputDialog("수를 입력하시오");
		x=Integer.parseInt(a);
		
		if(x%2==0)
			str1="짝수";
		else
			str1="홀수";
		
		JOptionPane.showMessageDialog( null,str1);
		System.exit(0);
	}
}

 

//예제03-03 IFSignTest.java
package ch03;
import javax.swing.JOptionPane;

public class IFSignTest{
	public static void main(String args[]){
		int  x;
		String a;
		String str1;
		
		a=JOptionPane.showInputDialog("수를 입력하시오");
		x=Integer.parseInt(a);
		
		if(x>0)
		    str1="양수";
		else if(x==0)
			str1="영";
		else
            str1="음수";
		
		JOptionPane.showMessageDialog( null,str1);
		System.exit(0);
	}
}

 

//예제03-04 SwitchTest.java
package ch03;
import javax.swing.JOptionPane;

public class SwitchTest{
	public static void main(String args[]){
		int  x;
		String a;
		String str1;
		
		a=JOptionPane.showInputDialog("1-4사이의 값을 입력하시오.");
		x=Integer.parseInt(a);
		
		switch(x){
			case 1:
				str1="1 입니다.";
				break;
			case 2:
				str1="2 입니다.";
				break;
			case 3:
				str1="3 입니다.";
				break;
			case 4:
				str1="4 입니다.";
				break;
			default:
				str1="해당값이 없습니다.";
				break;
		}
		JOptionPane.showMessageDialog( null,"입력하신 값은 "+str1);
		System.exit(0);
	}
}

 

//예제03-05 ForTest.java
package ch03;
import javax.swing.JOptionPane;

public class ForTest{
	public static void main(String args[]){
		int k=0;
		for(int i=0;i<10;i++){
			k++;
		  }
		JOptionPane.showMessageDialog( null,"k의 값은 "+k);
		System.exit(0);
	}	
}

 

//예제03-06 MultiForTest.java
package ch03;
import javax.swing.JOptionPane;

public class MultiForTest{
	public static void main(String args[]){
		String str1="";
		
		str1="구구단"+"\n";
		
		for(int i=2;i<=9;i++){
		  str1+=i+"단"+" : ";
		  for(int j=1;j<=9;j++){
		  	  str1+=i+"*"+j+"="+(i*j);
		  	if(j<9)
		  		str1+=" | ";
		  }
		  str1+="\n";
		}
		
		JOptionPane.showMessageDialog( null,str1);
		System.exit(0);
	}
}

 

// 예제03-07 WhileTest.java
package ch03;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class WhileTest {
   public static void main( String args[] )
   {
      int count=0;
      int gradeValue;
      int total=0;
      int i=0; 
      double average;        
      String studentCount="";
      
      studentCount=JOptionPane.showInputDialog("학생수를 입력하세요" );
      
      if(studentCount.equals("") || studentCount.equals(null))
    	  count=0;
      else
          count = Integer.parseInt(studentCount);

      while ( i<count ) {
         gradeValue = Integer.parseInt( JOptionPane.showInputDialog("학생별 점수를 입력하세요" ) );
		 i++;
		 total += gradeValue;
      }

      DecimalFormat digits = new DecimalFormat( "0.00" );

      if ( count != 0 ) {
         average = (double) total / count;  

         JOptionPane.showMessageDialog( null,
            "반평균: " + digits.format( average ),
            "반평균구하기",JOptionPane.INFORMATION_MESSAGE );
      }else{
         JOptionPane.showMessageDialog( null,
            "학생수를 입력하지 않았습니다.", "반평균구하기 에러메시지",
            JOptionPane.INFORMATION_MESSAGE );
      }
      System.exit( 0 ); 
   }
}
728x90
그리드형

댓글