728x90
SMALL
break문 말고 true false로 빠져나갈 수 있는 방법
옆의 경우일 때 많이쓰임
삼항연산자쓰는법
if문 두개쓰면 만족해도 확인.
else는
System.out.println("변수 b는 " + ((b%2 ==0) ? "짝수" : "홀수"));
if문 두개쓰면 만족해도 확인.
else는 조건안맞으면 그냥 실행
그래서 if else문을 써야함
형변환
package Project01;
import java.util.Scanner;
/*5. max값을 찾아 출력하세요.
* 1번에서 작성한 소스코드를 적극 활용하세요.
* 입력정보 : 학생 이름과 성적(100점 만점), 중복값은 없는 걸로 가정한다.
* 출력 : 입력된 성적(점수)들 중 최대값을 출력하세요.
* 학습정보 : 단, 단독 if문 2개만 사용하여 출력하시오(논리연산자(and, or)사용할 것)
*/
//흰트 a > b && a > c
public class Pro05 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name1, name2, name3;
int score1, score2, score3;
System.out.print("1번 학생 이름을 입력하세요 : ");
name1 = in.next();
System.out.print(name1 + "의 성적을 입력하세요 : ");
score1 = in.nextInt();
System.out.print("2번 학생 이름을 입력하세요 : ");
name2 = in.next();
System.out.print(name2 + "의 성적을 입력하세요 : ");
score2 = in.nextInt();
System.out.print("3번 학생 이름을 입력하세요 : ");
name3 = in.next();
System.out.print(name3 + "의 성적을 입력하세요 : ");
score3 = in.nextInt();
int max = score3;
if (score1 > score2 && score1 > score3) {
max = score1;
}
if (score2> score1 && score2 > score3) {
max = score2;
}
System.out.println("최댓값 : " + max);
}
}
초기최댓값을 마지막꺼로 하는것
package com.chap02;
import java.util.ArrayList;
public class Ex19 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> list = new ArrayList<String>();
list.add("c++");
list.add("java");
list.add("python");
for(int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}
}
}
추가하는법
list.size하면 그 리스트의 사이즈만큼 포문 돌림
밑에는 list.get(i)
new를 꼭 써줘야함
package Project01;
import java.util.Scanner;
/*9. while문 사용하기(학생 점수의 합계가 100점 이상이면 반복문 빠져나오기)
* 8번에서 작성한 소스코드를 활용하세요.
* 입력정보 : 학생 이름과 성적(100점 만점), 입력된 점수는 50점 이상으로 가정한다.
* 출력 : 입력된 성적(점수)의 총 합계가 100점 이상이면 반복문을 빠져나온다.
* 마지막 "프로그램이 종료되었다"는 안내메시지를 띄운다.
* 학습정보 : break 사용하기
*/
public class Pro09 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name;
int sum = 0, i = 0, score=0;
while(sum< 100) {
System.out.print("학생" + (i+1) + "의 이름을 입력하세요 : ");
name = in.next();
System.out.print("학생" + (i+1) + "의 성적을 입력하세요 : ");
score = in.nextInt();
i++;
sum = sum+score;
}
System.out.println("종료");
}
}
while문을 써서 빠져나오기
package Project01;
import java.util.Scanner;
/*10. static 멤버필드, static 메소드 활용하여 프로그램 작성하기
* 8번에서 작성한 소스코드를 활용하세요.
* 멤버필드 : 학생 이름과 성적(100점 만점)입력 시킬 배열
* 메소드 : input(), output()
* 메소드 input()의 경우 Scanner를 사용하여 사용자로부터 한꺼번에 입력받도록한다.
* 메소드 output()의 경우 input()이 실행된 후 output() 출력이 실행되도록 한다.
* 마지막 "프로그램이 종료되었다"는 안내메시지를 띄운다.
* 학습정보 : static
*/
public class Pro10{
static Scanner in = new Scanner(System.in);
static String[] name = new String[3];
static int[] score = new int[3];
static int i = 0;
public Pro10 () {
input();
}
static void input() {
for (int i = 0; i < score.length; i++) {
System.out.print("학생" + (i+1) + "의 이름을 입력하세요 : ");
name[i] = in.next();
System.out.print(name[i] + "의 성적을 입력하세요 : ");
score[i] = in.nextInt();
}
output();
}
static void output() {
System.out.println("학생 정보");
for (int i = 0; i < score.length; i++) {
System.out.println((i+1) + ". 이름 : " + name[i] + ", 성적 : " + score[i]);
}
}
static void run() {
input();
input();
input();
}
public static void main(String[] args) {
run();
}
}
static void어쩌구....
근데 밑에 input()을 세번 실행해주는 run()이라는 메소드를 만들면 run만 출력하면 됨...
개쩐다...
static = 공용
final 은 바꿀수x
PI같이 대문자는 상수로 바꿀수없는 값
a값없으면 멤버필드로감 메소드로가는거 xx
package com.chap03;
public class Ex29 {
static int recursiveSum(int n) {
if (n == 1) { // n이 1이면, 그냥 1을 반환함.
return 1;
}
return n + recursiveSum(n - 1);
// n이 1이 아니면, n을 1부터 (n-1)까지의 합과 더한 값을 반환함.
}
public static void main(String[] args) {
int res = recursiveSum(10);
System.out.println(res);
}
}
recursivesum
내가 나를 부르는..
- 클래스
package com.chap04;
public class Ex31 {
public static void main(String[] args) {
new Data(); //같은 파일안에 없어도 같은 패키지안에 있으면 데이터 가져다 쓸 수 있음
}
}
- this
같은애들끼리는 this라고 해야함
만약 this없으면 두번째 Info만 실행
this붙히면 바깥것
습관화하면 좋음
728x90
LIST
'JAVA > Java' 카테고리의 다른 글
20201005_ 10 Review (생성자, 초기화) (0) | 2020.10.06 |
---|---|
20201005_10 클래스배열, 생성자 (0) | 2020.10.05 |
20200929_9 클래스배열 (0) | 2020.09.29 |
20200929_9 (0) | 2020.09.29 |
20200928_8 클래스분리 Review (0) | 2020.09.29 |
댓글