2014-05-19 6 views
0

사용자로부터 입력 문자를 받고 소문자를 대문자로 변환하거나 그 반대로 변환하는 프로그램을 만들려고합니다. 변환이 수행 될 때마다 변경 횟수가 증가하고 '.' 가 입력되면 프로그램은 입력을 요구하지 않습니다. 이것은 내가 지금까지 아래로 무엇을 가지고 :Java 대문자, 소문자 질문

import java.io.*; 
class Example5 
{ 
    public static void main(String args[]) throws IOException 
    { 
     InputStreamReader inStream = new InputStreamReader (System.in); 
     BufferedReader stdin = new BufferedReader (inStream); 
     char input = '\0'; 
     int counter = 0; 

     while(!(input == '.')) 
     { 
     System.out.print("Input a character. Input will continue until you enter a period : "); 
     input = (char)stdin.read(); 

     if((int)input > 96 & (int)input < 123) 
     { 
      char upperInput = Character.toUpperCase(input); 
      System.out.println(upperInput); 
      counter++; 
     } 
     else if((int)input > 64 & (int)input < 91) 
     { 
      char lowerInput = Character.toLowerCase(input); 
      System.out.println(lowerInput); 
      counter++; 
     } 
     } 

     System.out.println("The number of changes are : " + counter); 
    } 
} 

변환 및 카운터는, 모든 입력 한 후, 어떤 이유로 라인 잘 작동하지만 "입력 문자를 마침표를 입력 할 때까지 입력이 계속됩니다."반복 모든 입력 후에 여러 번. 이 문제에 대한 해결책은 무엇입니까? 어떤 실수를 했습니까?

+0

:하며'동안 위'("입력 문자 마침표를 입력 할 때까지 입력이 계속됩니다.") '반복. – yate

+0

user3580294 - 고마워요! 이걸 몰랐어! – user3529827

+0

yate - yup, 나는이 방법으로 그것을 바 꾸었습니다. 그러나 그 라인이 여러 번 반복되는 이유에 대해서도 궁금했습니다. 고마워! – user3529827

답변

3

귀하의 인쇄 진술은 귀하의 while 회 돌이에 있습니다. 이로 인해 루프가 시작될 때마다 프로그램이 인쇄됩니다.

루프는 더 많은 입력을 기다리지 않고 새로운 입력이 있는지 여부에 관계없이 반복됩니다.

수정하려면 프로그램 실행 시작시 한 번만 명령문을 인쇄하거나 루프 조건을 변경하여 새로운 입력이있을 때만 다시 실행되도록하려면 루프에서 print 문을 꺼내거나 주어진.

나는 분명하고 도움이되기를 바랍니다.

+0

대단히 고마워요! – user3529827

0

: 사전에

감사는 루프에서의

같이 while 루프 외부에 넣어, 각 시간은 문자의 인쇄하기, 읽기.

System.out.print("Input a character. Input will continue until you enter a period : "); 

     while(!(input == '.')) 
+0

고마워요! 많이 도와 줬어! – user3529827

+0

당신은 환영합니다 : – mohamedrias

0

무언가 같이 당신은 할 수 있습니다 : 당신은 그냥`System.out.print 이동해야

InputStreamReader inStream = new InputStreamReader (System.in); 
     BufferedReader stdin = new BufferedReader (inStream); 
     char input = '\0'; 
     int counter = 0; 
     System.out.print("Input a character. Input will continue until you enter a period : "); 
     do 
     { 

     input = (char)stdin.read(); 
     System.out.print("Input a character. Input will continue until you enter a period : "); 
     if((int)input > 96 & (int)input < 123) 
     { 
      char upperInput = Character.toUpperCase(input); 
      System.out.println(upperInput); 
      counter++; 
     } 
     else if((int)input > 64 & (int)input < 91) 
     { 
      char lowerInput = Character.toLowerCase(input); 
      System.out.println(lowerInput); 
      counter++; 
     } 
     }while(!(input == '.')); 

     System.out.println("The number of changes are : " + counter); 
    } 
관련 문제