今天小菜鸟我写了一段简单的代码: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();”再进行循环的检测吗?怎么会编译不通过,而且说找不到呢?请高手指点啊~~
{
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();”再进行循环的检测吗?怎么会编译不通过,而且说找不到呢?请高手指点啊~~
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"));
}
}
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要定义在循环的外面
while()里看不到
判断条件是否成立,如果成立继续do{}里面的,不成立则结束。
你的answer在do循环中定义只能在循环中用
在while中调不到它的
在do循环上一行定义,运行看看是不是你想要的结果!
直接用while吧,do...while可读性差