1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package ex01.input;
 
//import java.util.*; ->util에 있는 모든걸 가져다 쓸 수 있음 
import java.util.Scanner//->util에 있는 Scanner만 가져다 쓰기 위함 
 
//입력받기(input) 추가 
public class Ex02_InputTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); //자동import방법: Ctrl + Shift + O 
        char grade = 'A';
        int kor, eng, com, tot = 0;
        double avg = 0//0.0도 가능 
        
        //2.메시지 출력 및 입력받기
        System.out.print("당신의 이름 입력 해주세요: ");
        
        //String(문자열)을 입력받는 방법2가지-.next() / .nextLine()
        String name = sc.next(); //next()이용: 
        
    
        System.out.println("국어, 영어, 전산 점수를 입력하세요: ");  
        kor = sc.nextInt();  eng = sc.nextInt();  com = sc.nextInt();
        
        //또는 tot = sc.nextInt() + sc.nextInt() + sc.nextInt(); 가능(최종점수만 필요한경우)
        tot = kor + eng + com;
        avg = tot/3.0;
        
        System.out.println("*** " + name + " 님의 성적표 ***");
        System.out.println("Kor: " + kor + "\t Eng: " + eng + "\t Com: " + com);        
        System.out.println("Total: " + tot + "\t Average: " + avg + "\t Grade: " + grade);
 
    }
}
cs
 

1) Scanner sc = new Scanner(System.in);

    int kor = sc.nextInt();

 

   cf)A a = new A(); 에서

     aobject 또는 instance라고 부른다

   -다른 클래스에 마음대로 들어갈 수 없기 때문에, key(객체)가 필요하다

   -입력받을 때 사용하는 scanner객체는 jdk 5.0부터 만들어 졌음


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package ex01.input;
 
//import java.util.*; ->util에 있는 모든걸 가져다 쓸 수 있음 
import java.util.Scanner//->util에 있는 Scanner만 가져다 쓰기 위함 
 
//입력받기(input) 추가 
public class Ex02_InputTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); //자동import방법: Ctrl + Shift + O 
        char grade = 'A';
        int kor, eng, com, tot = 0;
        double avg = 0//0.0도 가능 
        
        //2.메시지 출력 및 입력받기
        System.out.print("당신의 이름 입력 해주세요: ");
        
        //String(문자열)을 입력받는 방법2가지-.next() / .nextLine()
        String name = sc.next(); //next()이용: 
        
    
        System.out.println("국어, 영어, 전산 점수를 입력하세요: ");  
        kor = sc.nextInt();  eng = sc.nextInt();  com = sc.nextInt();
        
        //또는 tot = sc.nextInt() + sc.nextInt() + sc.nextInt(); 가능(최종점수만 필요한경우)
        tot = kor + eng + com;
        avg = tot/3.0;
        
        System.out.println("*** " + name + " 님의 성적표 ***");
        System.out.println("Kor: " + kor + "\t Eng: " + eng + "\t Com: " + com);        
        System.out.println("Total: " + tot + "\t Average: " + avg + "\t Grade: " + grade);
 
    }
}
cs

2) Scanner sc = new Scanner(System.in);

   tot = sc.nextInt() + sc.nextInt() + sc.nextInt();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package ex01.input;
 
//import java.util.*; ->util에 있는 모든걸 가져다 쓸 수 있음 
import java.util.Scanner//->util에 있는 Scanner만 가져다 쓰기 위함 
 
//입력받기(input) 추가 
public class Ex02_InputTest {
    public static void main(String[] args) {
        
        //Scanner sc = new Scanner(System.in); //자동import방법: Ctrl + Shift + O 
        char grade = 'A';
 
        //2.메시지 출력 및 입력받기
        System.out.print("Name =  ");
        
        //String(문자열)을 입력받는 방법2가지-.next() / .nextLine()
        String name = new Scanner(System.in).next(); //Scanner선언하면서 입력받기 
        
    
        System.out.println("국어, 영어, 전산 점수를 입력하세요: ");  
        int kor = new Scanner(System.in).nextInt();
        int eng = new Scanner(System.in).nextInt();
        int com = new Scanner(System.in).nextInt();
        
        int tot = kor + eng + com;
        double avg = tot / 3.//3.0으로 써도 가능 
        
        System.out.println("*** " + name + " 님의 성적표 ***");
        System.out.println("Kor: " + kor + "\t Eng: " + eng + "\t Com: " + com);        
        System.out.println("Total: " + tot + "\t Average: " + avg + "\t Grade: " + grade);
 
    }
}
 
cs

3) int kor = new Scanner(System.in).nextInt();


<nextXXX()>

     ->자료형

nextInt() 

nextFloat()

nextDouble()

next() 와 nextLine()

'JAVA > 05_입력받기' 카테고리의 다른 글

sc.nextInt(); -> sc.nextLint(); -> println => ?  (0) 2021.01.14
next() 와 nextLine()의 차이  (0) 2021.01.14

+ Recent posts