簡體   English   中英

使用掃描器時Java.util.NoSuchElementExecption編譯錯誤?

[英]Java.util.NoSuchElementExecption compilation error whilst using a scanner?

我真的不知道這是怎么回事...

也是您輸入2個數字的程序,它會創建一個x高度和y寬度的星形框

import java.util.*;

public class NestedLoops{
    public static void main(String[] args){

        Scanner s = new Scanner(System.in);

        int height;
        int width;

        System.out.println("Please input the Height");
            height = s.nextInt();
        System.out.println("Please input the Width");
            width = s.nextInt();


            for ( int i = 0; i < height; i++ ){
                for ( int j = 0; j < width; j++ ){
                    System.out.print( "*" ) ;
                }
                    System.out.println( "" ) ;
            }


    }
}

您應該檢查是否已經使用hasNextInt api來確定號碼,如下所示

if (s.hasNextInt()) {
    height = s.nextInt();
}
if (s.hasNextInt()) {
    width = s.nextInt();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM