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:循環後無法獲得正確的輸入
我建議讓你的代碼看起來更乾淨。 –