[英]How do I break out of the loop on entering a null input
以下程序是顯示具有最大元音數量的單詞。但它在我給出 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()
如果您提供空輸入(或按回車鍵),則不會觸發。 為了讓您的退出條件起作用,您應該使用
w=sc.nextLine();
之后,如果你按回車,for 被破壞,應用程序打印結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.