Understanding try & catch and error handling

前端 未结 3 1394
我在风中等你
我在风中等你 2020-12-20 07:25
import java.util.Scanner;

public class Lab4_5 {
    public static void main(String[]args) {
        Scanner scan= new Scanner(System.in);

        int rows=0;
              


        
3条回答
  •  眼角桃花
    2020-12-20 07:54

    String choice2=scan.next();
    if(choice2.equals(y)){
      System.out.println("How many rows/columns(5-21)?");
    try
    {
    rows=scan.nextInt();
    }catch(Exception e)
    {
    rows = -1;
    }
     while(rows<5||rows>21){
      System.out.println("That is either out of range or not an integer, try again! ");
    try
    {
    rows=scan.nextInt();
    }catch(Exception e)
    {
    rows = -1;
    }
    }
    

提交回复
热议问题