switch문
switch(인자값){
case 조건값 1:
수행문;
break;
case 조건값2:
수행문;
break;
case 조건값3:
수행문;
default:
주위할점
char c='a';
switch(c){
case 'a':
break;
변수의 값이 char 이면 case 값:
string str="aaa"
switch(aaa){
case "aaa":
string은 객체여서 case에 사용할 수 없다.
http://cafe.naver.com/hanbiteni/24
예제1
public class xx {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=1;
switch(n){
case 0:
System.out.print("hi0");
break;
case 1:
System.out.print("hi1");
break;
case 2:
System.out.print("hi2");
break;
case 3:
System.out.print("hi3");
break;
default:
System.out.println();
}
}
}예제2
가위바위보
if문으로 표현
랜덤으로 보여지는 값과 입력한 값의 결과 보여주기
import java.util.Random;
import java.util.Scanner;
public class xx {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random ran=new Random();
int temp=ran.nextInt(3);
Scanner s=new Scanner(System.in);
System.out.println("0:가위, 1:바위, 2:보");
int mytemp =s.nextInt();
if(temp==0){
System.out.println("가위");
}
else if(temp==1){
System.out.println("바위");
}
else{
System.out.println("보");
}
if(mytemp==0){
System.out.print("가위");
}
else if(mytemp==1){
System.out.print("바위");
}
else{
System.out.print("보");
}
if(temp==mytemp){
}
}
}랜덤값과 입력값을 비교해서 이겼는지 졌는지 표현
me.equals는 String으로 입력한 문자열과 값이 같은지 비교한다.
import java.util.Random;
import java.util.Scanner;
public class xx {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random ran=new Random();
int temp=ran.nextInt(3);
Scanner s=new Scanner(System.in);
String me=s.next(); //내가 낼 것을 저장하는 객체
//System.out.print(me);
if(temp==0){
System.out.println("가위");
if(me.equals("가위")){//me==가위, me변의 문자열이 가위와 같은가
System.out.println("비겼다");
}
else if(me.equals("바위")){
System.out.println("이겼다");
}
else if(me.equals("보")){
System.out.println("졌다");
}
else{
System.out.println("졌다");
}
}
else if(temp==1){
System.out.println("바위");
if(me.equals("가위")){
System.out.println("졌다");
}
else if(me.equals("바위")){
System.out.println("비겼다");
}
else if(me.equals("보")){
System.out.println("이겼다");
}
else{
System.out.println("졌다");
}
}
else{
System.out.println("보");
if(me.equals("가위")){
System.out.println("이겼다");
}
else if(me.equals("바위")){
System.out.println("졌다");
}
else if(me.equals("보")){
System.out.println("비겼다");
}
else{
System.out.println("졌다");
}
}
}
}Swich문으로 표현
import java.util.Random;
import java.util.Scanner;
public class xx {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random ran=new Random();
int com=ran.nextInt(3);
Scanner s =new Scanner(System.in);
String me=s.next();
switch(com){
case 0:
System.out.println("가위");
if(me.equals("가위")){
System.out.println("비겼다");
}
else if(me.equals("바위")){
System.out.println("이겼다");
}
else if(me.equals("보")){
System.out.println("졌다");
}
else{
System.out.println("졌다");
}
break;
case 1:
System.out.println("바위");
if(me.equals("가위")){
System.out.println("졌다");
}
else if(me.equals("바위")){
System.out.println("비겼다");
}
else if(me.equals("보")){
System.out.println("이겼다");
}
else{
System.out.println("졌다");
}
break;
case 2:
System.out.println("보");
if(me.equals("가위")){
System.out.println("이겼다");
}
else if(me.equals("바위")){
System.out.println("졌다");
}
else if(me.equals("보")){
System.out.println("비겼다");
}
else{
System.out.println("졌다");
}
break;
default:
}
}
}예제3
자판기 돈 투입
0:콜라, 1:스프라이트 2, 3:식혜 그외: 반환
import java.util.Random;
import java.util.Scanner;
public class xx {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int cash;
int sel; //상품선택변수
System.out.println("0:콜라 700원 | 1:스프라이트 650원 |"+"2:코코팜 600원 |3:식혜 500원 | 그외:반환");
System.out.print("돈입력:");
cash=s.nextInt();
System.out.print("상품선택:");
sel=s.nextInt();
switch(sel){
case 0:
if(cash<700){
System.out.print("돈부족");
}
else{
System.out.print("콜라나왔습니다. 거스름돈 : "+ (cash-700));
}
break;
case 1:
if(cash<700){
System.out.print("돈부족");
}
else{
System.out.print("스트라이프나왔습니다. 거스름돈 : "+ (cash-650));
}
break;
case 2:
if(cash<700){
System.out.print("돈부족");
}
else{
System.out.print("코코팜나왔습니다. 거스름돈 : "+ (cash-600));
}
break;
case 3:
if(cash<700){
System.out.print("돈부족");
}
else{
System.out.print("식혜나왔습니다. 거스름돈 : "+ (cash-500));
}
break;
default:
System.out.print("거스름돈"+(cash));
}
}
}
예제4
입력한 값이 1이면 "영어점수", 입력한 값이 2면 "수학점수" 로 출력해준다.
0:국어, 1:영어, 2:수학, 3:과학
import java.util.Random;
import java.util.Scanner;
public class xx {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int subject=s.nextInt();
switch(subject){
case 0:
System.out.println("국어 점수");
break;
case 1:
System.out.println("영어 점수");
break;
case 2:
System.out.println("수학 점수");
break;
case 3:
System.out.println("과학 점수");
break;
default:
System.out.println("과목을 입력하세요");
}
}
}예제5
국어:100
영어:100
수학:0
과학:100
******** 점수보기 0:국어 1:영어 2:수학 3:과학 4:총점 5:평균 ******
import java.util.Random;
import java.util.Scanner;
public class xx {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("확인하고 싶은 과목을 입력하세요 0:국어 1:영어 2:수학 3:과학 4:총점 5:평균");
int subject=s.nextInt();
int k=100;
int e=100;
int m=0;
int sc=100;
int total=k+e+m+sc;
float average=(k+e+m+sc)/4;
switch(subject){
case 0:
System.out.println("국어점수 : "+ k);
break;
case 1:
System.out.println("영어점수 : "+ e);
break;
case 2:
System.out.println("수학점수 : "+ m);
break;
case 3:
System.out.println("과학점수 : "+ sc);
break;
case 4:
System.out.println("총점 : "+total);
break;
case 5:
System.out.println("평균 : "+average );
break;
default:
}
}
}
예제6
switch case를 사용해서
두변수를 입력받고
0일때는 삼각형의 넓이
1일때는 사각형의 넓이
import java.util.Random;
import java.util.Scanner;
public class xx {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("가로길이: ");
int wid=s.nextInt();
System.out.println("세로길이: ");
int hei=s.nextInt();
System.out.println("0: 삼각형의 넓이 1:사각형의 넓이");
int area=s.nextInt();
switch(area){
case 0:
int tr=wid*hei/2;
System.out.println("삼각형의 넓이:"+tr);
break;
case 1:
int sq=wid*hei;
System.out.println("사각형의 넓이:"+sq);
break;
default:
}
}
}
- [2014/06/19] 140619_[java]조건문 ()
댓글 0
댓글 쓰기 권한이 없습니다.
| 21 | mysql 설치 | 2014.07.23 |
| 20 | jsp강좌 | 2014.07.23 |
| 19 | 140719_01 | 2014.07.17 |
| 18 | 140711_02_자바 | 2014.07.11 |
| 17 | 140711-자바스크립트 | 2014.07.11 |
| 16 | 140710-자바스크립트 | 2014.07.10 |
| 15 | 140709_01 | 2014.07.09 |
| 14 | 140707 | 2014.07.07 |
| 13 | 140707 | 2014.07.07 |
| 12 | 140704_01 | 2014.07.04 |
| 11 | 140701_01 | 2014.07.01 |
| 10 | 140626_01 | 2014.06.26 |
| 9 | 140625_01 | 2014.06.26 |
| 8 | 140624_01 | 2014.06.24 |
| > | 140623_[java] switch문 | 2014.06.23 |
| 6 | 140620_[java]랜덤함수 | 2014.06.20 |
| 5 | 140619_[java]조건문 | 2014.06.19 |
| 4 | 140618_[java]논리연산자 | 2014.06.18 |
| 3 | 140617_[java]연산자 | 2014.06.17 |
| 2 | 140616_[java]상수와 변수 | 2014.06.16 |