今天小菜鸟我写了一段简单的代码:import java.util.*;public class ChangeByPunctuation
{
public static void main(String[] args){
  
  Scanner keyboard=new Scanner(System.in);
  do{
 System.out.println("Enter a sentence with punctuations:");
     String sentenceInput=keyboard.nextLine();
     int lengthOfSentence=sentenceInput.length();
 char lastPunctuation=sentenceInput.charAt(lengthOfSentence-1);
     if((lastPunctuation=='?')&&(lengthOfSentence%2==0)){
      System.out.println("Yes");
     }else if((lastPunctuation=='?')&&(lengthOfSentence%2==1)){
      System.out.println("No");
     }else if(lastPunctuation=='!'){
      System.out.println("Wow");
     }else{
      System.out.println("You always say \""+sentenceInput+" \"");
     }
 System.out.println("Again?Yes or No?");
     String answer=keyboard.nextLine();
  }while(answer.equalsIgnoreCase("yes")); 
}
}结果它说找不到变量answer?执行流不是先经过“String answer=keyboard.nextLine();”再进行循环的检测吗?怎么会编译不通过,而且说找不到呢?请高手指点啊~~

解决方案 »

  1.   


    import java.util.*; public class ChangeByPunctuation 

    public static void main(String[] args){ 
      
      Scanner keyboard=new Scanner(System.in); 
      //扩大你的answer变量范围
      String answer=null;
      do{ 
    System.out.println("Enter a sentence with punctuations:"); 
        String sentenceInput=keyboard.nextLine(); 
        int lengthOfSentence=sentenceInput.length(); 
    char lastPunctuation=sentenceInput.charAt(lengthOfSentence-1); 
        if((lastPunctuation=='?')&&(lengthOfSentence%2==0)){ 
          System.out.println("Yes"); 
        }else if((lastPunctuation=='?')&&(lengthOfSentence%2==1)){ 
          System.out.println("No"); 
        }else if(lastPunctuation=='!'){ 
          System.out.println("Wow"); 
        }else{ 
          System.out.println("You always say \""+sentenceInput+" \""); 
        } 
    System.out.println("Again?Yes or No?"); 
        answer=keyboard.nextLine(); 
      }while(answer.equalsIgnoreCase("yes")); 


      

  2.   


    import java.util.*; public class ChangeByPunctuation 

    public static void main(String[] args){ 
      
      Scanner keyboard=new Scanner(System.in); 
      String answer ="";
      do{ 
    System.out.println("Enter a sentence with punctuations:"); 
        String sentenceInput=keyboard.nextLine(); 
        int lengthOfSentence=sentenceInput.length(); 
        char lastPunctuation=sentenceInput.charAt(lengthOfSentence-1); 
        if((lastPunctuation=='?')&&(lengthOfSentence%2==0)){ 
          System.out.println("Yes"); 
        }else if((lastPunctuation=='?')&&(lengthOfSentence%2==1)){ 
          System.out.println("No"); 
        }else if(lastPunctuation=='!'){ 
          System.out.println("Wow"); 
        }else{ 
          System.out.println("You always say \""+sentenceInput+" \""); 
        } 
        System.out.println("Again?Yes or No?"); 
        answer=keyboard.nextLine(); 
      }while(answer.equalsIgnoreCase("yes")); 

    } answer要定义在循环的外面
      

  3.   

    }while(answer.equalsIgnoreCase("yes")); 当while是真后,执行啥?
      

  4.   

    你的answer在do{}范围里,
    while()里看不到
      

  5.   

    while为真后,再进行一次do-while循环啊~
      

  6.   

    先执行do{}里面的,然后再执行while(),
    判断条件是否成立,如果成立继续do{}里面的,不成立则结束。
      

  7.   

    小弟冒充一下高手!!
    你的answer在do循环中定义只能在循环中用
    在while中调不到它的
    在do循环上一行定义,运行看看是不是你想要的结果!
      

  8.   

    尽量不要用do...while
    直接用while吧,do...while可读性差