對於大量輸入輸出的最後一個輸入沒有顯示出來。 當我用來輸入15個或更多的不同的輸入我得到的輸出是一個比給定的輸出數少。不顯示的輸出是最後一個輸出。我沒有在java中使用cmd得到正確的輸出.prompt
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(sc.hasNext())
{
try
{
long x=sc.nextLong();
System.out.println(x+" can be fitted in:");
if(x>=-128 && x<=127)System.out.println("* byte");
if(x>=-pow(2,15) && x<pow(2,15))System.out.println("* short");
if(x>=-(long)pow(2,31) && x<(long)pow(2,31))System.out.println("* int");
if(x>=-(long)pow(2,63) && x<(long)pow(2,63))System.out.println("* long");
//Complete the code
}
catch(Exception e)
{
System.out.println(sc.next()+" can't be fitted anywhere.");
}
}
請提供完整的代碼。無需修改即可運行,使其更容易幫助。 – Aelexe
你基本上是通過獲取nextInt()來跳過第一個輸入,然後在try塊的第一行獲得下一個long。 – scrappedcola