第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

當(dāng)用戶輸入字符串而不是整數(shù)輸入時(shí)捕獲錯(cuò)誤

當(dāng)用戶輸入字符串而不是整數(shù)輸入時(shí)捕獲錯(cuò)誤

不負(fù)相思意 2021-12-22 19:23:04
我希望代碼在用戶輸入字符串而不是整數(shù)時(shí)捕獲錯(cuò)誤。您可以看到我嘗試了一個(gè)仍然無法正常工作的 try catch 塊。除此之外,其他一切都很完美。我該如何解決這個(gè)問題?這是輸出應(yīng)該如何:Welcome to the Squares and Cubes tableEnter an integer: fiveError! Invalid integer. Try again.Enter an integer: -5Error! Number must be greater than 0Enter an integer: 101Error! Number must be less than or equal to 100Enter an integer: 9Number  Squared Cubed======  ======= =====1       1       12       4       83       9       274       16      645       25      1256       36      2167       49      3438       64      5129       81      729Continue? (y/n): yEnter an integer: 3Number  Squared Cubed======  ======= =====1       1       12       4       83       9       27
查看完整描述

3 回答

?
米脂

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊

我稍微更改了您的代碼并將其作為一個(gè)整體發(fā)布,以避免混淆:


public static void main(String[] args) {

    // Welcome the user

    System.out.println("Welcome to the Squares and Cubes table");

    System.out.println();

    Scanner sc = new Scanner(System.in);

    String choice = "y";


    do {

        int integer = Integer.MAX_VALUE;

        while (integer == Integer.MAX_VALUE) {

            // Get input from the user

            System.out.print("Enter an integer: ");

            String input = sc.nextLine();

            try {

                integer = Integer.parseInt(input);

            }

            catch (NumberFormatException e) {

                System.out.println("Error! Invalid integer. Try again.");

            }

        }

        if(integer<0){


            System.out.println("Error! Number must be greater than 0");

            System.out.print("Enter an integer: ");

            integer = sc.nextInt();


        }


        if(integer>100){


            System.out.println("Error! Number must be less than or equal to 100");


            System.out.print("Enter an integer: ");

            integer = sc.nextInt();

        }


        // Create a header

        String header = "Number  " + "Squared " + "Cubed   " + "\n"

                +   "======  " + "======= " + "=====   ";

        System.out.println(header);


        int square = 0;

        int cube = 0;


        String row = "";

        for (int i = 1; i <= integer; i++)

        {


            square = i * i;

            cube = i * i * i;


            row = i + "       " + square + "       " + cube;

            System.out.println(row);

        }


        // See if the user wants to continue

        System.out.print("Continue? (y/n): ");

        choice = sc.next();

        System.out.println();


    } while (!choice.equalsIgnoreCase("n"));

}

這個(gè)想法是while在你的循環(huán)中創(chuàng)建另一個(gè)并運(yùn)行它直到用戶傳遞一個(gè)整數(shù)。


查看完整回答
反對 回復(fù) 2021-12-22
?
三國紛爭

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

Integer.parseInt方法是將 the 轉(zhuǎn)換String為 int 并NumberFormatException在字符串無法轉(zhuǎn)換為int類型時(shí)拋出 a 。


應(yīng)該是這樣的:


    System.out.print("Enter an integer: ");

    Scanner sc =new Scanner(System.in);


    try {

        int integer = Integer.parseInt(sc.nextLine());

    } catch (NumberFormatException e) {

        System.out.println("Error! Invalid integer. Try again.");

    }


查看完整回答
反對 回復(fù) 2021-12-22
?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以使用此方法來測試輸入的值是否為有效整數(shù)。以此結(jié)果為基礎(chǔ),您可以從其他驗(yàn)證開始


public boolean isInt(string input) {

    try {

      Integer.parseInt(text);

      return true;

    } catch (NumberFormatException e) {

     return false;

     } 

    }


查看完整回答
反對 回復(fù) 2021-12-22
  • 3 回答
  • 0 關(guān)注
  • 221 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號