[英]How do I break out of the loop on entering a null input
The following program is to display the word with maximum number of vowels.But it does not work until i have given 10 variables as input even though it is supposed to end after giving a null input.How can I fix this problem ( I already tried using different inputs like "." and " ")以下程序是显示具有最大元音数量的单词。但它在我给出 10 个变量作为输入之前不起作用,即使它应该在给出 null 输入后结束。我该如何解决这个问题(我已经尝试过使用不同的输入,如“.”和“”)
** **
import java.util.*;
public class hw1{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String w,temp="";
int c=0,max=0;
for(int i=0;i<10;i++)
{
w=sc.next();
w=w.toUpperCase();
if(w.equals(""))
break;
for(int j=0;j<w.length();j++)
{
if(w.charAt(j)=='A'||w.charAt(j)=='E'||w.charAt(j)=='I'||w.charAt(j)=='O'||w.charAt(j)=='U')
c++;
}
max=Math.max(max,c);
if(max==c)
temp=w;
c=0;
}
System.out.println(temp);
}
}
** **
sc.next()
does not trigger if you give empty input (or press enter).如果您提供空输入(或按回车键),则不会触发。 For allowing your exit condition to work you should use为了让您的退出条件起作用,您应该使用
w=sc.nextLine();
After that, if you hit enter, the for is broken and the app prints the result之后,如果你按回车,for 被破坏,应用程序打印结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.