danDevlog

Java - day5 본문

Java

Java - day5

단데기이 2022. 2. 21. 13:09
728x90

while문

public class Main {
   public static void main(String[] args) {
   
      int tot = 0;// 1~10 합을 누적할 변수.
      int i = 1; // 1씩 순차적으로 증가할 증가값.

      while (i <= 10) {
         System.out.print(i);
         System.out.print((i < 10) ? "+" : "=");
         tot += i;
         i++;
      }
      System.out.println(tot);
   }

}
import java.util.Scanner;

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

      while (true) {
         int a = sc.nextInt();
         int b = sc.nextInt();
         System.out.println(a + b);
         if (a == 0 && b == 0) {
            sc.close();// Unreachable code
            break;// 순환문을 탈출할때 사용.
         }
      }
      // sc.close();// Unreachable code
   }
}
import java.util.Scanner;

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

	// cin.hasNext() 는 다음 입력 값의 유무를 체크하는 메소드
      while (in.hasNextInt()) {
         int a = in.nextInt();
         int b = in.nextInt();
         System.out.println(a + b);
      }
      
      in.close();
   }
}

Scanner 클래스의 EOF(end of File)는 hasNext()메소드를 이용한다.

입력된 토큰이 있으면 true를 반환하고, 그렇지 않을 경우 false를 반환합니다.

import java.util.Scanner;

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

      int N = sc.nextInt();
      int c = 0;
      int re = N;

      // or 1개 일때, 둘중 아무거나 참이면 참이다.
      // 좌항에서 참이 나오면 더이상 우항을 체크하지 않음.
      // or 2개 일때,
      // 좌항이 참이라도 우항을 체크
      while ((N != re) | (c == 0)) {
         re = (re % 10) * 10 + ((re / 10) + (re % 10)) % 10;
         c++;
      }
      System.out.println(c);

      sc.close();
   }
}
import java.io.IOException;
import java.util.Scanner;

// 중첩 do-while문 구구단
public class Main {
	public static void main(String[] args) throws IOException {
		Scanner sc = new  Scanner(System.in);
		
		int i = 2;
		int j = 1;
		
		do {
			do {
				System.out.println(i + " x " + j + " = " + i*j);
			}while(++j<=9);
			j = 1;
			System.out.println();
		}while(++i<=9);

	}
}
import java.io.IOException;

public class Main {
	public static void main(String[] args) throws IOException {
		Student s1 = new Student(); // 객체 생성
		Student s2 = new Student();
		Student s3 = new Student();
		
		s1.name = "gildong"; // 필드값 초기화
		s1.grade = 3;
		s1.major = "computer science"; // 객체를 통한 메소드 호출
		s1.major();
		
		s2.name = "gosu";
		s2.grade = 2;
		s2.major = "society";
		s2.major();
		
		s3.name = "dingdong";
		s3.grade = 1;
		s3.major = "math science";
		s3.major();

	}
}
// 클래스 생성
class Student{
	String name, major;
	int grade;
	
	public void study() {
		System.out.println(name + "은 공부한다.");
	}
	public void major() {
		System.out.println(name + "의 전공은 " + major + " 입니다.");
	}
}

'Java' 카테고리의 다른 글

Java - day7  (0) 2022.02.23
Java - day6  (0) 2022.02.22
Java - day4  (0) 2022.02.18
Java - day3  (0) 2022.02.17
Java - day2  (0) 2022.02.16
Comments