오버로딩
=>같은 이름의 메소드가 여러개 있어도 인자가 다르면 사용가능
ex)
public static void quiz1(int i){
}
public static void quiz1(int z){
}
public static void quiz1(int n){
}
for(int i=1;i<=10;i++)의 int i는 for문의 지역변수라 for문 밖에서는 사용x
class안에 int, String, boolean만 써도
자동으로 int = 0, String =null, boolean = false 인식
for(int i=1;i<=5;i++)
{
System.out.print(i+"번점수: ");
score=sc.nextInt();
if(score<1 || score>100)
{
System.out.println("\t잘 못 입력했어요");
i--; //범위가 정해져 있어서 i--;를 안쓰면 5번만사용가능
//예외사항은 횟수(범위: i)에 넣으면 안되니
//i--;를 해줘서 5번 온전히 사용가능하게 해줌
continue;
}
sum+=score;
}
System.out.println("합계는 "+sum);
public static void quiz3() {
/*
* 총 5개의 점수를 입력받아 합계를 구하시오
* 만약 1~100이 아닐경우 다시 입력받아라
* 1번점수: 88
* 2번점수: 99
* 3번점수: 200
* 잘못입력했어요
* 4번점수: 33
*
* ========
* 합계
* */
Scanner sc=new Scanner(System.in);
int score;
int sum=0;
for(int i=1;i<=5;i++)
{
System.out.print(i+"번점수: ");
score=sc.nextInt();
if(score<1 || score>100)
{
System.out.println("\t잘 못 입력했어요");
i--; //범위가 정해져 있어서 i--;를 안쓰면 5번만사용가능
//예외사항은 횟수(범위: i)에 넣으면 안되니
//i--;를 해줘서 5번 온전히 사용가능하게 해줌
continue;
}
sum+=score;
}
System.out.println("합계는 "+sum);
}
if(변수이름.startsWith("문자"))
=> "문자"로 시작하는 것을 찾아줌
package sist0622;
import java.util.Scanner;
public class StartsWith_03 {
public static void main(String[] args) {
//이름을 반복 입력해서 이씨가 몇 명인지 알아보자
//종료==끝
Scanner sc=new Scanner(System.in);
String name;
int cnt=0;
while(true)
{
System.out.println("이름입력(끝이면 종료)");
name=sc.nextLine();
//break문
if(name.equals("끝"))
break;
//조건
if(name.startsWith("이")) //지금은 name이지만 이로 시작하는 거라면 상관없는지
cnt++;
}
System.out.println("이씨 성을 가진 사람은 총 "+cnt+"명 입니다");
}
}
public static void main(String[] args) {
//2~9까지 구구단 (구구단 기본로직)
for(int dan=2;dan<=9;dan++)
{
for(int j=1;j<=9;j++)
{
System.out.println(dan+"x"+j+"="+dan*j);
}
System.out.println();
}
label은 본인이 설정하는 이름
이름: for(int dan=2;dan<=9;dan++)
{
for(int j=1;j<=9;j++)
{
if(j==5)
break 이름;
}
}
break '이름';을 쓰면 '이름:'을 붙인 반복문 탈출
//구구단으로 break연습
/*반복문 앞에 이름(label)을 붙이고 해당 이름을 break처리하면
2개 이상의 반복문을 빠져나가는게 가능하다*/
loop: for(int dan=2;dan<=9;dan++)
{
for(int j=1;j<=9;j++)
{
//각 단의 4까지만 나오게 break
/*if(j==5)
break;*/
//반복문 앞에 이름 붙이고 break 이름;하면
//해당 반복문 break 가능
if(j==5)
break loop;
System.out.println(dan+"x"+j+"="+dan*j);
}
System.out.println();
}
기존 break; 와 continue; 처럼 사용
if(조건)
{
문장
break;
}
if(조건)
{
문장
continue;
}
public static void main(String[] args) {
//단을 입력해서 해당단 출력하기
//0을 입력시 종료
Scanner sc=new Scanner(System.in);
int dan;
while(true)
{
System.out.println("단을 입력하세요(종료:0)");
dan=sc.nextInt();
//종료
if(dan==0)
{
System.out.println("종료합니다");
break;
}
//2~9단까지만 입력가능
if(dan>9 || dan<2)
{
System.out.println("잘 못 적었어요");
continue;
}
//구구단 출력
System.out.println("["+dan+"]");
for(int n=1;n<=9;n++)
{
//System.out.println(dan+"x"+n+"="+dan*n);
System.out.printf("%d x %d = %2d\n",dan,n,dan*n);
}
System.out.println();
}
}
public static void main(String[] args) {
//가로방향으로 구구단을 출력하세요
//[2단] [3단] ...... [9단]
System.out.println("\t**가로방향 구구단**");
//제목[]
for(int dan=2;dan<=9;dan++)
{
System.out.print("["+dan+"단]\t");
}
System.out.println();
//단출력
for(int i=1;i<=9;i++)
{
for(int dan=2;dan<=9;dan++) //단
{
System.out.print(dan+"x"+i+"="+dan*i+"\t"); // 1. 2x1 3x1 .... 9x1 먼저 출력하고
// 3. i가 1개증가해서
// 2x2 3x2 .... 9x2 순서대로 출력
}
System.out.println(); //2. 줄내려서
}
}
public static void main(String[] args) {
System.out.println("중첩(다중)for문으로 별모양 찍기");
//기본 5*5
for(int i=1;i<=5;i++)//행 갯수(row)
{
for(int j=1;j<=5;j++)//열 갯수(column)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();
//위부터 12345 순서대로 찍기
/*
모양 ex)
*
**
***
****
*****
*/
for(int i=1;i<=5;i++) //행갯수
{
for(int j=1;j<i+1;j++) //조건식 j<=i랑 같다 ..열갯수(i만큼만 반복)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();
//위부터 54321 순서대로 찍기
/*
모양 ex)
*****
****
***
**
*
*/
for(int i=5;i>=1;i--) // for(int i=1;i<=5;i++) 행갯수
{
for(int j=1;j<i+1;j++) // for(int j=5;j>=i;j--) 열갯수(i가 1일때 5번, 2일때 4번... 5일때 1번)
{
System.out.print("*");
}
System.out.println();
}
}
public static void quiz1() {
/*
* 나이를 반복해서 입력받는다
* 0이되면 빠져나와서 출력한다
* 나이가 50이상일 경우 a변수 증가
* 나이가 30이상일 경우 b변수 증가
* 그 나머지는 c변수 증가
*
* ===========================
* 50세이상: 3명
* 30세이상 50세미만: 2명
* 그이외: 1명*/
Scanner sc=new Scanner(System.in);
int age,a=0,b=0,c=0;
while(true)
{
System.out.println("나이를 입력해주세요");
age=sc.nextInt();
if(age==0)
{
System.out.println("종료합니다");
break;
}
if(age>=50)
a++;
else if(age>=30)
b++;
else
c++;
}
System.out.println("50세이상: "+a+"명");
System.out.println("30세이상 50세미만: "+b+"명");
System.out.println("그이외: "+c+"명");
}