JAVA ฉันไม่เข้าใจ Try-catch

นี่เป็นครั้งแรกของฉันที่นี่ ฉันเริ่มเรียนรู้วิธีการเขียนโค้ด ดังนั้นฉันหวังว่าคำถามนี้ที่ฉันมีจะไม่ใช่สิ่งที่ฉันจะหาได้จากที่นี่! (ฉันสัญญาว่าจะค้นหามาระยะหนึ่งแล้ว แต่เนื่องจากฉันเป็นมือใหม่ในหัวข้อนี้ ฉันจึงไม่พบสิ่งใดที่เข้าใจได้สำหรับฉันเพื่อแก้ไขข้อสงสัยของฉัน)

ฉันกำลังเล่นเกมง่ายๆ ใน JAVA ซึ่งโปรแกรมจะสร้างตัวเลขสุ่มและผู้เล่นจะต้องเดาตัวเลขที่สร้างขึ้น เมื่อผู้เล่นกรอกตัวเลข เกมจะแสดงคำใบ้ว่าสูงหรือต่ำกว่าตัวเลขที่สร้างขึ้นแบบสุ่ม

โปรแกรมทำงานได้ดีหากคุณป้อนเพียงตัวเลข แต่ฉันต้องการเพิ่มคำสั่ง try-catch เพื่อจัดการกับอินพุตของผู้ใช้ที่ไม่ถูกต้อง

ฉันลองใช้คำสั่งตามที่แสดงในโค้ดของฉัน แต่ฉันไม่เข้าใจว่าทำไมมันถึงทำงานไม่ถูกต้อง เพราะเมื่อฉันป้อนตัวเลขที่แตกต่างออกไป ระบบจะตรวจจับข้อยกเว้นและพิมพ์บนคอนโซล System.out.println( ) แต่โปรแกรมจะยุติลงเมื่อสิ่งนี้เกิดขึ้น

ฉันต้องการลองจับเพียงเพื่อให้ได้ข้อยกเว้นของการป้อนไม่ใช่ตัวเลขโดยไม่ต้องยุติโปรแกรมทุกครั้งที่จับข้อยกเว้น ฉันจะแก้ไขปัญหานี้ได้อย่างไร?

ขอบคุณมาก ๆ สำหรับความช่วยเหลือของคุณ!

import java.util.Scanner;

public class HiLo {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);  //Creates Scanner object to read from keyboard
        String playAgain = "";  //if == y, game restarts
        try {
            do {
                // Create a random number for the user to guess
                int theNumber = (int)(Math.random() * 100 + 1);
                //System.out.println(theNumber);        //Uncoment this in case we want to know the number (for testing).
                int guess = 0;  //Number entered by the player
                int count = 0;  //Number of tries of guessing the number
                while(guess != theNumber){
                    System.out.println("Guess a number between 1 and 100:");
                    guess = scan.nextInt(); //Reads the number typed on the keyboard by the player
                    count++;    //Plus 1 every time a number is entered
                    System.out.println("You entered " + guess +".");
                    if(guess < theNumber) { //If number entered is smaller
                        System.out.println("The number is bigger" + ", try again!");
                        System.out.println("Number of tries: " + count);
                    } else if(guess > theNumber) { //If number entered is bigger
                        System.out.println("The number is smaller" + ", try again!");
                        System.out.println("Number of tries: " + count);
                    } else {    //If both previous cases are false
                        System.out.println("Congratulations! You've found the number!");
                    }
                }
                //Once guess == theNumber
                System.out.println("Number of tries: " + count);
                System.out.println("Play again? (y/n)");
                playAgain = scan.next();    //Reads the String entered from keyboard by the player
            }
            while(playAgain.equalsIgnoreCase("y"));     //If player enters y, start again.
            //Otherwise
            System.out.println("Thank you for playing! Goodbye :)");
        } catch (Exception e) {
            System.out.println("Incorrect entering! Please enter a number between 1 and 100.");
        }
        scan.close();   //Close scanner
    }   //Close main
}   //Close class

person Vicentequesada    schedule 01.12.2017    source แหล่งที่มา
comment
โปรแกรมยุติลงเนื่องจากหลังจาก catch ของคุณ คุณกำลังปิดเครื่องสแกนและกลับมาจากหลัก ดังนั้นคุณคาดหวังอะไร   -  person tkausl    schedule 01.12.2017
comment
แล้วฉันจะแก้ไขปัญหานี้ได้อย่างไร? ถ้าฉันแก้ไขโครงสร้างของคำสั่ง do- While มีข้อผิดพลาดปรากฏขึ้นใน eclipse และไม่อนุญาตให้ฉันคอมไพล์ ฉันคิดที่จะเพิ่ม Guess = scan.nextInt() หลัง System.out.println() แต่ไม่ได้ผล   -  person Vicentequesada    schedule 01.12.2017


คำตอบ (2)


วาง try-catch ภายใน while loop และสร้างออบเจ็กต์สแกนเนอร์อีกครั้ง (scan = new Scanner(System.in) ภายใน catch block

while (guess != theNumber) {
                    try {                       
                        System.out.println("Guess a number between 1 and 100:");
                        guess = scan.nextInt(); // Reads the number typed on the
                        // keyboard by the player
                        count++; // Plus 1 every time a number is entered
                        System.out.println("You entered " + guess + ".");
                        if (guess < theNumber) { // If number entered is smaller
                            System.out.println("The number is bigger" + ", try again!");
                            System.out.println("Number of tries: " + count);
                        } else if (guess > theNumber) { // If number entered is
                            // bigger
                            System.out.println("The number is smaller" + ", try again!");
                            System.out.println("Number of tries: " + count);
                        } else { // If both previous cases are false
                            System.out.println("Congratulations! You've found the number!");
                        }
                    } catch (Exception e) {
                        System.out.println("Incorrect entering! Please enter a number between 1 and 100.");
                        scan = new Scanner(System.in);
                    }
                }
person Barr3l3y3    schedule 01.12.2017
comment
ยอดเยี่ยม! ทำงานเหมือนมีเสน่ห์! ขอบคุณมาก! ฉันยังมีคำถามอยู่ข้อหนึ่ง: ทำไมคุณต้องสร้างวัตถุสแกนเนอร์อีกชิ้นหนึ่ง ฉันไม่ค่อยเข้าใจ คุณช่วยอธิบายให้ฉันได้เรียนรู้ได้ไหม ขอบคุณที่สละเวลา!! - person Vicentequesada; 01.12.2017
comment
ความคิดเห็นเพิ่มเติม ฉันต้องการวัตถุสแกนเนอร์อีกหนึ่งชิ้นเพื่อให้ทำงานได้อย่างสมบูรณ์ แต่ฉันไม่แน่ใจว่าทำไม 555 ถ้าอธิบายได้จะดีมาก! :D ขอบคุณ! - person Vicentequesada; 01.12.2017
comment
scan.nextInt() จะถูกดำเนินการก่อน จากนั้นจึงกำหนดค่าอินพุตให้เดาตัวแปรเท่านั้น อินพุตที่ไม่ใช่ int จะทำให้ scan.nextInt() ส่งข้อยกเว้น ในขณะที่การวนซ้ำ scan.nextInt() ยังคงส่งข้อยกเว้นเนื่องจากเก็บอินพุตที่ไม่ใช่ int ซึ่งจะทำให้เกิดการวนซ้ำไม่สิ้นสุด ในการแก้ปัญหานี้ เราจำเป็นต้องตรวจสอบให้แน่ใจว่าวัตถุสแกนเนอร์ไม่ได้เก็บค่าอินพุตที่ไม่ใช่ int วิธีหนึ่งในการทำเช่นนี้คือการสร้างออบเจ็กต์ Scanner ใหม่ใน catch block - person Barr3l3y3; 01.12.2017
comment
ขอบคุณมากสำหรับความช่วยเหลือของคุณ! ความนับถือ! - person Vicentequesada; 01.12.2017

คุณต้องเข้าใจการทำงานของบล็อก try-catch คุณไม่จำเป็นต้องล้อมโค้ดทั้งหมดไว้ภายใน try เพียงใส่ส่วนของโค้ดที่ทำให้เกิดข้อยกเว้น ดังนั้น ในกรณีของคุณ ให้ล้อมรอบ guess = scan.nextInt(); ด้วย try แล้วจึงจับข้อยกเว้น เนื่องจากคำสั่งนี้ทำให้เกิดข้อยกเว้นเมื่ออินพุตไม่ใช่จำนวนเต็ม วิธีนี้ทำให้คุณมั่นใจได้ว่าอินพุตของผู้ใช้นั้นถูกต้องสำหรับการวนซ้ำของ while(guess != theNumber) แต่ละครั้ง

แก้ไข_1: ฉันลบบล็อก try-catch ออกจากโค้ดของคุณ และเพิ่มสิ่งต่อไปนี้ & มันใช้ได้ดีสำหรับฉัน:

try{
  guess = scan.nextInt();} //Reads the number typed on the keyboard by the player
catch (InputMismatchException e){
  System.out.println("Incorrect entering! Please enter a number between 1 and 100.");
  scan.nextLine();
  continue;
}
person Vineet Bhat    schedule 01.12.2017
comment
ใช่ ฉันคิดว่านั่นอาจเป็นวิธีแก้ปัญหา แต่เมื่อฉันพยายาม โปรแกรมก็เสร็จสิ้นเพราะเงื่อนไข while จะเป็นเท็จเสมอหากคุณป้อนคำ (ตัวอย่าง) :/ - person Vicentequesada; 01.12.2017
comment
คุณจะต้องเพิ่ม scan.nextLine() ในบล็อก catch เพื่อให้สามารถใช้งานได้ ฉันได้แก้ไขคำตอบเพื่อแสดงสิ่งที่ฉันหมายถึง - person Vineet Bhat; 01.12.2017
comment
เหตุใดคุณจึงใช้คำสั่ง Continue ที่นั่น - person Vicentequesada; 01.12.2017
comment
เนื่องจากเมื่อ guess ไม่ถูกต้อง ฉันไม่ต้องการดำเนินการวนซ้ำอีกต่อไป กล่าวคือ ฉันไม่ต้องการให้ count++ และคำสั่งหลังจากนั้นดำเนินการ แต่ฉันต้องการให้การวนซ้ำครั้งต่อไปเริ่มต้นขึ้น - person Vineet Bhat; 02.12.2017