Java 如何使用用户输入的字符串结束do-while循环? publicstaticvoidmain(字符串[]args) { 做{ 扫描仪键盘=新扫描仪(System.in); System.out.print(“输入字符串:”); 字符串语句=keyboard.nextLine(); 系统输出打印(“输入字母:”); 字符串fullLetter=keyboard.nextLine(); char-letter=fullLetter.charAt(0); keyboard.nextLine(); 整数金额=0; for(int i=0;i
}Java 如何使用用户输入的字符串结束do-while循环? publicstaticvoidmain(字符串[]args) { 做{ 扫描仪键盘=新扫描仪(System.in); System.out.print(“输入字符串:”); 字符串语句=keyboard.nextLine(); 系统输出打印(“输入字母:”); 字符串fullLetter=keyboard.nextLine(); char-letter=fullLetter.charAt(0); keyboard.nextLine(); 整数金额=0; for(int i=0;i,java,Java,} 我希望用户在循环结束时输入“是”或“否”,然后我希望该输入确定程序是否将再次循环。就目前而言,我的代码的最后一行不起作用。我环顾四周,不知道该怎么做才能解决这个问题。你应该使用键盘。next()来读取字符串,而不是键盘。nextLine() next()只读取一个单词,nextLine()读取包括Enter在内的整行,因此它永远不会等于“yes”您应该使用键盘。next()读取字符串,而不是键盘。nextLine() next()只读取一个单词,nextLine()读取包括Enter在内的整
我希望用户在循环结束时输入“是”或“否”,然后我希望该输入确定程序是否将再次循环。就目前而言,我的代码的最后一行不起作用。我环顾四周,不知道该怎么做才能解决这个问题。你应该使用
键盘。next()
来读取字符串,而不是键盘。nextLine()
next()
只读取一个单词,nextLine()
读取包括Enter在内的整行,因此它永远不会等于“yes”您应该使用键盘。next()
读取字符串,而不是键盘。nextLine()
next()
只读取一个单词,nextLine()
读取包括Enter在内的整行,因此它永远不会等于“yes”您需要在循环外部声明变量decision
并在内部初始化:
public static void main (String[] args)
{
do {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
String sentence = keyboard.nextLine();
System.out.print("Enter a letter: ");
String fullLetter = keyboard.nextLine();
char letter = fullLetter.charAt(0);
keyboard.nextLine();
int amount = 0;
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (ch == letter) {
amount++;
}
}
System.out.println(letter + " appears " + amount + " times in " + sentence);
System.out.print("Continue? ");
String decide = keyboard.nextLine();
} while (decide.equals("yes"));
}
您需要在循环外部声明变量decision
,并在内部初始化:
public static void main (String[] args)
{
do {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
String sentence = keyboard.nextLine();
System.out.print("Enter a letter: ");
String fullLetter = keyboard.nextLine();
char letter = fullLetter.charAt(0);
keyboard.nextLine();
int amount = 0;
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (ch == letter) {
amount++;
}
}
System.out.println(letter + " appears " + amount + " times in " + sentence);
System.out.print("Continue? ");
String decide = keyboard.nextLine();
} while (decide.equals("yes"));
}
必须在do/while循环外部声明字符串descripe,否则它是do/while循环的局部变量,并且不能被do测试部分访问。简单使用
String decide;
do {
//do something ...
decide = keyboard.nextLine();
} while (decide.equals("yes"));
publicstaticvoidmain(字符串[]args){
串决定;
做{
扫描仪键盘=新扫描仪(System.in);
System.out.print(“输入字符串:”);
字符串语句=keyboard.nextLine();
系统输出打印(“输入字母:”);
字符串fullLetter=keyboard.nextLine();
char-letter=fullLetter.charAt(0);
keyboard.nextLine();
整数金额=0;
for(int i=0;i<句子长度();i++){
char ch=句子charAt(i);
if(ch==字母){
金额++;
}
}
System.out.println(字母+“显示”+金额+“次输入”
+判决);
系统输出打印(“是否继续?”);
decise=keyboard.nextLine();
}而(决定等于(“是”);
}
将解决您的问题。您必须在do/while循环外部声明字符串descripe,否则它是do/while循环的局部变量,并且不能被do测试部分访问。简单使用
String decide;
do {
//do something ...
decide = keyboard.nextLine();
} while (decide.equals("yes"));
publicstaticvoidmain(字符串[]args){
串决定;
做{
扫描仪键盘=新扫描仪(System.in);
System.out.print(“输入字符串:”);
字符串语句=keyboard.nextLine();
系统输出打印(“输入字母:”);
字符串fullLetter=keyboard.nextLine();
char-letter=fullLetter.charAt(0);
keyboard.nextLine();
整数金额=0;
for(int i=0;i<句子长度();i++){
char ch=句子charAt(i);
if(ch==字母){
金额++;
}
}
System.out.println(字母+“显示”+金额+“次输入”
+判决);
系统输出打印(“是否继续?”);
decise=keyboard.nextLine();
}而(决定等于(“是”);
}
将解决您的问题。您必须在循环外定义变量Decise:
public static void main(String[] args) {
String decide;
do {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
String sentence = keyboard.nextLine();
System.out.print("Enter a letter: ");
String fullLetter = keyboard.nextLine();
char letter = fullLetter.charAt(0);
keyboard.nextLine();
int amount = 0;
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (ch == letter) {
amount++;
}
}
System.out.println(letter + " appears " + amount + " times in "
+ sentence);
System.out.print("Continue? ");
decide = keyboard.nextLine();
} while (decide.equals("yes"));
}
必须在循环外定义变量Decise:
public static void main(String[] args) {
String decide;
do {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
String sentence = keyboard.nextLine();
System.out.print("Enter a letter: ");
String fullLetter = keyboard.nextLine();
char letter = fullLetter.charAt(0);
keyboard.nextLine();
int amount = 0;
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (ch == letter) {
amount++;
}
}
System.out.println(letter + " appears " + amount + " times in "
+ sentence);
System.out.print("Continue? ");
decide = keyboard.nextLine();
} while (decide.equals("yes"));
}
不要转载我的答案:)@BinkanSalaryman我还没有转载你的答案。我和你同时写了我自己的答案。如果你仔细看看,你会发现其中的区别。不要转载我的答案:)@BinkanSalaryman我还没有转载你的答案。我和你同时写了我自己的答案。如果你仔细观察,你会发现其中的区别,“代码不工作”到底是什么意思?你有错误日志吗?代码不工作到底是什么意思?你有错误日志吗?nextLine
读取直到用户点击Enter-“\n”
不包含在结果字符串中nextLine
读取直到用户点击Enter-“\n”
不包含在结果字符串中这起作用,我没有意识到我必须在外部声明它。谢谢!这起作用了,我没有意识到我必须在外面申报。谢谢!