使用命令行输入CTRL + D和try catch语句

时间:2014-05-12 00:59:38

标签: java try-catch

我试图使用catch(EOFException)和其他一些东西,但我不确定如何让它工作。

代码:

public class Circle {      // Student Starter code
   private double radius;    // circle radius

   /**
    * Constructor - Create a new circle
    * @param    inRadius  radius of the circle
    */


   public Circle(double inRadius ) throws Exception{
       if(inRadius > 0.0)
       {
       radius = inRadius;
       }
       else
           throw new ShapeException("ShapeException occurred...");

   }

   /**
    * Return the radius of the circle
    * @return    radius of the circle
    */
   public double getRadius() {
      return radius;
   }

   /**
    * set the radius
    * @param newRadius      new radius of the circle
    */
   public void setRadius(double newRadius) throws Exception{
       if(newRadius > 0.0)
       {
       radius = newRadius;
       }
       else{
           Exception e = new Exception("ERROR: Radius < 0");
        throw e;
         }
   }

   /**
    * Compute and return the area of the circle
    * @return    the area of the circle
    */
   public double area() {
      return Math.PI * radius * radius;
   }

   /**
    * Stretches circle size by multiplying
    * the radius by the factor provided.
    * @param    factor    stretch factor
    */
   public void stretchBy(double factor) throws Exception{
       if(factor > 0.0)
       {
       radius = radius * factor;
       }
       else
           throw new Exception("ERROR: factor < 0");
   }

   /**
    * Return a string representation of a circle.
    * @return    a string representing this circle
    */
   public String toString() {
       return String.format("Circle: %s", getRadius());
   }
}

测试员类:

 import java.util.Scanner;
 import java.util.*;
 import java.io.*;

 public class TestCircleC{
 public static void main(String [] args) {
 // read a radius of the circle from command line 
 boolean run = true;
 Scanner scan = new Scanner(System.in);

 while(run){
   try{
       System.out.printf("Enter a circle radius: ");

       String sradius = scan.next();

       if(sradius.length() <= 0){
       System.out.println("CTRL+D entered - program terminated");
       run = false;
       System.exit(0);
       }

       double radius = Double.parseDouble(sradius);
       // Instantiate a Circle object
       Circle aCircle = new Circle(radius);

       // Print current status of the circle
       System.out.println(aCircle);

       if(radius > 0){
          System.exit(0);
          run = false;
       }

    } 
    catch(EOFException eofe){
       System.out.print("CTRL+D entered - program terminated");
    }
    catch(InputMismatchException ime){
       System.out.println("InputMismatchException occurred...");
    }
    catch(NoSuchElementException nse){
       System.out.println("CTRL+D entered- program terminated");
       run = false;
       System.exit(0);
    }

    catch(ArrayIndexOutOfBoundsException aie) {
          System.out.println("ArrayIndexOutOfBoundsException occurred...");
    }
    catch(NumberFormatException nfe){
       System.out.println("NumberFormatException occurred...");
    }
    catch(ShapeException se){
       System.out.println("Shape Exception Occurred... ");
       //e.printStackTrace();
    }
    catch(Exception e){
       System.out.println("Error: " + e.getMessage());
    }

 }   

}
}

所以代码到目前为止正确地产生了我的输出,只是试图让它产生带有CTRL + D输入的输出并不想因某些原因而工作。

我不允许将hasNext与扫描仪一起使用是我工作的唯一例外。

我通过mac终端窗口收集输入,我正在寻找类似于此的输出...

Enter a circle radius: CTRL+D entered - program terminated

要做....  1.检查是否有效输入,然后终止循环(x)  2.如果代码在Ctrl + d输出上面停止输出信息()

我不确定当命令行的输入终止程序时,为了让程序打印,我应该怎么做

提前致谢

------ ----分配

修改程序以打印输入值的promt,并使用scanner类读入双输入值。不要使用hasNextInt进行测试。

修改程序以抛出异常

当出现输入Control-D的异常时,打印消息&#34; CTRL + D输入 - 程序终止&#34;对于所有其他例外,打印出一条消息说明发生了什么样的异常就足够了。

0 个答案:

没有答案