아 이미지 왜 자꾸 깨짐.

이미지 자꾸 깨지니까 그냥 말로하겠음 ...딱히 할말이없네요 이미지올리고싶은데
ps&cube
접속 : 6148   Lv. 86

Category

Profile

Counter

  • 오늘 : 28 명
  • 전체 : 440365 명
  • Mypi Ver. 0.3.1 β
[학생] 자바 나눗셈 나머지 관련 문제 질문입니다. (9) 2015/09/07 PM 08:22
import java.util.Scanner;
public class baksu
{
public static void main(String[] args){

System.out.println("1~99까지 정수를 입력하세요 : ");
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();

int first=n/10;
int second=n%10;

if(first%3==0){
if(second%3==0)
System.out.println("박수 짝짝");

}else if(first%3==0){
if (second%3!=0)
System.out.println("박수 짝");
}else if(first%3!=0){
if (second%3!=0)
System.out.println("박수 없음");

}else
System.out.println("박수짝2");
return;

}
}

문제는 1~99의 숫자 입력받아서
3,6,9중 하나가 있으면 박수 짝, 두개있으면 박수 짝짝 없으면 박수 없음. 으로 나오는거 출력하는겁니다.
first는 십의 자리수,
second는 첫재자리고 3으로 나눠서 나머지가 0이면 배수인걸로 생각해서 만들었습니다.
첫번째 if는 36, 39 입력해보니 박수 짝짝 나와 잘맞고...
11, 12 입력하면 박수없음 나오니 잘 맞는데....
31입력하면 박수짝이 나오질 않네요 ㅜㅠ 어디가 잘못된걸까요?

second%!=0 의 경우에는 둘째자리(일의자리)수의 나머지가 0이 아니면 참이니 뒤에거 실행하는거라 맞게 했다고 생각하는데....ㅜㅠ

if절

신고

 

밥 로스    친구신청

조건문 5번째 라인

}else if(first%3==0){

이 줄을 지우고 해보세요

ps&cube    친구신청

수정해서..if(first%3==0){
if(second%3==0)
System.out.println("박수 짝짝");

}else if (second%3!=0)
System.out.println("박수 짝");
if(first%3!=0){
if (second%3!=0)
System.out.println("박수 없음");

}else
System.out.println("박수짝2");

이렇게 해봤는데 31을 입력하면 박수짝2가 나오네요. 박수짝이나와야하는데.

공허의 플삼위♥    친구신청

if(second%3==0)
System.out.println("박수 짝짝");
else
System.out.println("박수 짝");

으훗    친구신청

윗분조언대로
}else if(first%3==0){
줄 빼시고
하위 if는 활성화 해야죠.
else
System.out.println("박수 짝");

ps&cube    친구신청

감사합니다 ㅋㅋ 해결했어요
if(first%3==0){
if(second%3==0)
System.out.println("박수 짝짝");

if (second%3!=0)
System.out.println("박수 짝");
}
if(first%3!=0){
if (second%3!=0)
System.out.println("박수 없음");

if (second%3==0)
System.out.println("박수짝2");
return;

Lonnie_Noel    친구신청

이렇게 하면 되지 않을까요?

if(first%3==0){
if(second%3==0)
System.out.println("박수 짝짝");
else if (second%3!=0)
System.out.println("박수 짝");
}else if(first%3!=0){
if (second%3!=0)
System.out.println("박수 없음");
else if(second%3==0)
System.out.println("박수 짝");
}else
System.out.println("박수짝2");

ps&cube    친구신청

되네요! ㅋㅋㅋ 감사합니다

DeathWalker    친구신청

int로 받지말고 String으로 받아서 첫번째 두번째 캐릭터가 3,6,9인지 switch case로 체크하시면 더 편할 것 같네요.
switch(firstChar)
case '3':
case '6':
case '9':
switch(secondChar)....
default:

이런식으로..

ps&cube    친구신청

오오..이런방식이..생각도못했는데...해봐야겠네요 감사합니다!
X