2014-05-15 39 views
-1
public static void main(String[] args) throws Exception 
{ 
    //intro 
    char rt,yn; 
    double v1,v2,v3, 
     w,  //width 
     d,  //depth 
     pc,  //pool capacity 
     rof= 50, //rate of flow 
     v,  //volume 
     ttf,  //time to fill 
     cap  //unit capacity 
     ; 
    int f; 

    Scanner sc = new Scanner(System.in); 

    System.out.println("Press 'enter' to get started"); 
    sc.nextLine(); 

    System.out.println("Please enter the pool type, press 'enter' after you finished"); 
    do { 
     System.out.println("Please enter a alphabet"); 
     System.out.println("'S'for Small(20x12x4)"); 
     System.out.println("'L'for Large(30x20x10)"); 
     System.out.println("'C'for Custom new size"); 

     //read rt S L C 
     rt= (char)System.in.read(); System.in.read(); 
     while (rt!='S' && rt!='s' && rt!='L' && rt!='l' && rt!='C' && rt!='c') 
     { 
      System.out.println("invalid input"); 
      System.out.println("please enter a alphabet"); 
      sc.next(); 
     } 

     if (rt=='S'||rt=='s') 
     { 
      calc(20,12,4); 
     } 
     else if(rt=='L'||rt=='l') 
     { 
      calc(20,12,4); 
     } 
     else if(rt=='C'||rt=='c') 
     { 
      System.out.println("Please enter the pool sizes, press enter after each side"); 
      System.out.println("Please enter Numbers, or errors may occur"); 
      System.out.println("now please enter your width(in ft)>>"); 

      v1= sc.nextDouble(); sc.nextLine(); 
      System.out.println("Please enter your length(in ft)>>"); 
      v2=sc.nextDouble(); sc.nextLine(); 
      System.out.println("Please enter your depth(in ft)>>"); 
      v3=sc.nextDouble(); sc.nextLine(); 
      calc(v1,v2,v3); 
     } 

     yn= (char)System.in.read(); 
    } 
    while(yn=='Y'); 

    System.out.println("Thank you for using"); 
} 

public static void calc(double l, double w, double d) throws Exception 
{ 
    double 
     pc,  //pool capacity 
     rof= 50, //rate of flow 
     v,  //volume 
     ttf1, //time to fill, whole num 
     cap= 7.5 //unit capacity 
     ; 

    v = l*w*d; 
    pc = v*cap; 
    ttf1 = pc /(rof*60); 

    System.out.println("It will take "+ ttf1 + " hours to fill up a pool with length: " + 
         l+" ft, width: "+w+" ft, and the depth of"+d+" ft"); 
    System.in.read(); 
    System.out.println("want do another one? Y/anykey"); 
} 

如果我运行此操作,在第一次输入Y之后第二次返回无效输入......导致此错误的原因是什么?有没有办法避免这种情况发生?Java:循环后无法获得正确的输入

+1

我建议让你的代码看起来更干净。 –

回答

0

现在,它的工作。

import java.io.IOException; 
import java.util.Scanner; 


public class DataProva { 

    public static void main(String[] args) { 
     //intro 
     char rt = 0; 
     char yn = 'Y'; 
     double v1,v2,v3, 
      w,  //width 
      d,  //depth 
      pc,  //pool capacity 
      rof= 50, //rate of flow 
      v,  //volume 
      ttf,  //time to fill 
      cap  //unit capacity 
      ; 
     int f; 

     Scanner sc = new Scanner(System.in); 

     System.out.println("Press 'enter' to get started"); 
     sc.nextLine(); 
     sc.reset(); 

     System.out.println("Please enter the pool type, press 'enter' after you finished"); 
     while(yn=='Y') { 
      System.out.println("Please enter a alphabet"); 
      System.out.println("'S'for Small(20x12x4)"); 
      System.out.println("'L'for Large(30x20x10)"); 
      System.out.println("'C'for Custom new size"); 

      //read rt S L C 

      try { 
       rt= (char) System.in.read(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      while (rt!='S' && rt!='s' && rt!='L' && rt!='l' && rt!='C' && rt!='c') 
      { 
       System.out.println("invalid input"); 
       System.out.println("please enter a alphabet"); 

       rt=(char) sc.next().charAt(0); 
      } 

      if (rt=='S'||rt=='s') 
      { 
       try { 
        calc(20,12,4); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      else if(rt=='L'||rt=='l') 
      { 
       try { 
        calc(20,12,4); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      else if(rt=='C'||rt=='c') 
      { 
       System.out.println("Please enter the pool sizes, press enter after each side"); 
       System.out.println("Please enter Numbers, or errors may occur"); 
       System.out.println("now please enter your width(in ft)>>"); 

       v1= sc.nextDouble(); sc.nextLine(); 
       System.out.println("Please enter your length(in ft)>>"); 
       v2=sc.nextDouble(); sc.nextLine(); 
       System.out.println("Please enter your depth(in ft)>>"); 
       v3=sc.nextDouble(); sc.nextLine(); 
       try { 
        calc(v1,v2,v3); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      System.out.println("want do another one? Y/anykey"); 
      sc.reset(); 
      try { 
       System.in.read(); 
       sc.next(); 
       yn= (char) System.in.read(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      sc.reset(); 





     } 

     System.out.println("Thank you for using"); 
     sc.close(); 

    } 

    public static void calc(double l, double w, double d) throws Exception 
    { 
     double 
      pc,  //pool capacity 
      rof= 50, //rate of flow 
      v,  //volume 
      ttf1, //time to fill, whole num 
      cap= 7.5 //unit capacity 
      ; 

     v = l*w*d; 
     pc = v*cap; 
     ttf1 = pc /(rof*60); 

     System.out.println("It will take "+ ttf1 + " hours to fill up a pool with length: " + 
          l+" ft, width: "+w+" ft, and the depth of"+d+" ft"); 


    } 


} 

您在System.in中读取a/r时遇到了问题。按Return键导致返回字符。 在你第一次检查时,你没有将sc.next()分配给rt,所以它只是无休止地循环。因为rt总是和以前一样。

作为一般规则,main不应该抛出Exception,因为没有人可以管理该Exception。你应该使用try catch来管理Exception。

您的变量应该每次都被初始化。

你的代码非常程序化,你可以用对象做更好的思考。

如果你使用Wrapper类,它会更好(char的包装类是Charachter)。

+0

这有助于很多,即时通讯仍然是一个新手到Java,谢谢你这么多 – user3642243

+0

@ user3642243那么,作为一个提示, 你可以想像一个像他的属性的OBJECT池池。 ,然后用Pool pool = new Pool(attributes) 构建一个对象,然后使用它进行操作。 无论如何如果你满意接受我的回答:) – user3384514