作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试一段for循环代码,用户输入他要写的单词数,然后输入单词本身,最后应该检查特定要求(以K开头的单词会增加计数)值以及以 K 开头的最后一个单词也将被注册)。然而,由于某种我不知道的原因,它只是不会循环,无论输入如何,它都会打印出代码上的所有内容。
package Others;
import java.util.*;
public class StartsWithString
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int wordNumber = 0;
String words = "";
int wordCount = 0;
String lastWord = "";
System.out.println("How many words are you going to write?: ");
wordNumber = sc.nextInt();
System.out.println("Write the desired words: ");
for(int i = 0; i < wordNumber; i++);
{
words = sc.nextLine();
if(words.startsWith("K"))
{
wordCount++;
lastWord = words;
}
}
System.out.println("From a total of " + numriFjaleve + " words typed,);
System.out.println(wordCount + " started with the letter K.");
System.out.println("The last word typed which began with the letter K was: " + lastWord);
}
}
最佳答案
for(int i = 0; i < wordNumber; i++);
循环定义末尾有一个分号。所以你的循环实际上不是一个循环。它是一个运行一次的独立代码块
。删除分号。
关于java - For 循环仅运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32186903/
我是一名优秀的程序员,十分优秀!