Java do while循环跳过代码

Java do while循环跳过代码,java,loops,Java,Loops,我在这里的do-while循环中遇到了问题。它第一次运行平稳,但第二次询问国家后,它不允许我输入。有什么帮助吗 import java.util.*; public class UseList { public static void main(String[] args) { Scanner userInput = new Scanner(System.in); ListInterface<String> list = new ArraySorte

我在这里的do-while循环中遇到了问题。它第一次运行平稳,但第二次询问国家后,它不允许我输入。有什么帮助吗

import java.util.*;

public class UseList
{
public static void main(String[] args)
{
    Scanner userInput = new Scanner(System.in);        
    ListInterface<String> list = new ArraySortedList<String>();
    String country, flag;
    char flagCharred;

    do
    {            
        System.out.print("Enter a country you've visited: ");
        country = userInput.nextLine();
        list.add(country);

        System.out.print("\n" +list);

        System.out.print("\nAdd another country?: Y/N ");
        flag = userInput.next();
        flagCharred = flag.charAt(0);
    } 
while (flagCharred == 'y' || flagCharred == 'Y');
}
}
import java.util.*;
公共类使用列表
{
公共静态void main(字符串[]args)
{
扫描仪用户输入=新扫描仪(System.in);
ListInterface list=新的ArraySortedList();
国家、国旗;
炭化;
做
{            
System.out.print(“输入您访问过的国家:”;
country=userInput.nextLine();
列表。添加(国家);
系统输出打印(“\n”+列表);
系统输出打印(“\N添加其他国家/地区?:是/否”);
flag=userInput.next();
flagCharred=flag.charAt(0);
} 
while(flagCharred='y'| | flagCharred=='y');
}
}
不会消耗行尾,因此它将在下一行中消耗

例如,当您写入
Y
作为第二个输入时,实际上您正在写入
Y
并按enter键,这是
'\n'
字符,下一个
将读取
Y
'\n'
将是
下一行的输入,从而使其跳过您想要的“真实”输入


一种解决方案是将
next
更改为
nextLine

不消耗行尾,因此它将在下一行中消耗

例如,当您写入
Y
作为第二个输入时,实际上您正在写入
Y
并按enter键,这是
'\n'
字符,下一个
将读取
Y
'\n'
将是
下一行的输入,从而使其跳过您想要的“真实”输入


一种解决方案是将
next
更改为
nextLine

不消耗行尾,因此它将在下一行中消耗

例如,当您写入
Y
作为第二个输入时,实际上您正在写入
Y
并按enter键,这是
'\n'
字符,下一个
将读取
Y
'\n'
将是
下一行的输入,从而使其跳过您想要的“真实”输入


一种解决方案是将
next
更改为
nextLine

不消耗行尾,因此它将在下一行中消耗

例如,当您写入
Y
作为第二个输入时,实际上您正在写入
Y
并按enter键,这是
'\n'
字符,下一个
将读取
Y
'\n'
将是
下一行的输入,从而使其跳过您想要的“真实”输入



一种解决方案是将
next
更改为
nextLine

这是您问题的解决方案

导入java.util.ArrayList

导入java.util.List

导入java.util.Scanner

公共类使用列表 {

公共静态void main(字符串[]args) {

Scanner userInput=新扫描仪(System.in);
列表=新的ArrayList();
国家、国旗;
炭化;
做
{            
System.out.print(“输入您访问过的国家:”;
country=userInput.nextLine();
列表。添加(国家);
系统输出打印(“\n”+列表);
系统输出打印(“\N添加其他国家/地区?:是/否”);
flag=userInput.next();
flagCharred=flag.charAt(0);
country=userInput.nextLine();
} 
while('y'==flagCharred | |'y'==flagCharred)

}


}

这是您问题的解决方案

导入java.util.ArrayList

导入java.util.List

导入java.util.Scanner

公共类使用列表 {

公共静态void main(字符串[]args) {

Scanner userInput=新扫描仪(System.in);
列表=新的ArrayList();
国家、国旗;
炭化;
做
{            
System.out.print(“输入您访问过的国家:”;
country=userInput.nextLine();
列表。添加(国家);
系统输出打印(“\n”+列表);
系统输出打印(“\N添加其他国家/地区?:是/否”);
flag=userInput.next();
flagCharred=flag.charAt(0);
country=userInput.nextLine();
} 
while('y'==flagCharred | |'y'==flagCharred)

}


}

这是您问题的解决方案

导入java.util.ArrayList

导入java.util.List

导入java.util.Scanner

公共类使用列表 {

公共静态void main(字符串[]args) {

Scanner userInput=新扫描仪(System.in);
列表=新的ArrayList();
国家、国旗;
炭化;
做
{            
System.out.print(“输入您访问过的国家:”;
country=userInput.nextLine();
列表。添加(国家);
系统输出打印(“\n”+列表);
系统输出打印(“\N添加其他国家/地区?:是/否”);
flag=userInput.next();
flagCharred=flag.charAt(0);
country=userInput.nextLine();
} 
while('y'==flagCharred | |'y'==flagCharred)

}


}

这是您问题的解决方案

导入java.util.ArrayList

导入java.util.List

导入java.util.Scanner

公共类使用列表 {

公共静态void main(字符串[]args) {

Scanner userInput=新扫描仪(System.in);
列表=新的ArrayList();
国家、国旗;
炭化;
做
{            
System.out.print(“输入您访问过的国家:”;
country=userInput.nextLine();
列表。添加(国家);
系统输出打印(“\n”+列表);
系统输出打印(“\N添加其他国家/地区?:是/否”);
flag=userInput.next();
flagCharred=flag.charAt(0);
country=userInput.nextLine();
} 
while('y'==flagCharred | |'y'==flagCharred)

}

}

next()只读一个单词,不读整行。尝试改用nextLine(),因为这似乎就是您的意思。next()
Scanner userInput = new Scanner(System.in); 

List<String> list = new ArrayList<String>();

String country, flag;

char flagCharred;

do
{            
    System.out.print("Enter a country you've visited: ");
    country = userInput.nextLine();
    list.add(country);

    System.out.print("\n" +list);

    System.out.print("\nAdd another country?: Y/N ");
    flag = userInput.next();
    flagCharred = flag.charAt(0);
    country = userInput.nextLine();

}