i'm developing a java program that shows area and perimeter of two rectangle with fixed width&length, current date and reads input from user to solve a linear equation. I was able to ask a user if they want to re-run the program or not. the problem is that if they input y or Y, the program runs, but if the user enters anything els the program quits. I'd like to check this input and see if:
1- it's a y or Y, re-run
2- it's n or N, quit
3- neither 1 nor 2, ask the user again if he/she wants to re-run the program of not.
Here's my code:
public static void main(String[] args) {
char ch = 'y';
GregorianCalendar calendar = new GregorianCalendar();
LinearEquation le = new LinearEquation(1.0,1.0,1.0,1.0,1.0,1.0);
Rectangle rec1 = new Rectangle(4.0,40.0);
Rectangle rec2 = new Rectangle(3.5,35.9);
Scanner input = new Scanner(System.in);
Double a, b, c, d, e,f;
do{
System.out.println("First rectangle info is: ");
System.out.print(" width is: "+ rec1.getWidth() +
"\n height is: " + rec1.getHeight()+
"\n Area is: "+ rec1.getArea() +
"\n Perimeter is: " + rec1.getPerimeter());
System.out.println();
System.out.println();
System.out.println("Second rectangle info is: ");
System.out.print(" width is: "+ rec2.getWidth() +
"\n height is: " + rec2.getHeight()+
"\n Area is: "+ rec2.getArea() +
"\n Perimeter is: " + rec2.getPerimeter());
System.out.println();
System.out.println("Current date is: " + calendar.get(GregorianCalendar.DAY_OF_MONTH) +
"-" + (calendar.get(GregorianCalendar.MONTH) + 1)+
"-" + calendar.get(GregorianCalendar.YEAR));
System.out.println("Date after applying 1234567898765L to setTimeInMillis(long) is: ");
calendar.setTimeInMillis(1234567898765L);
System.out.println(calendar.get(GregorianCalendar.DAY_OF_MONTH) +
"-" + (calendar.get(GregorianCalendar.MONTH) + 1)+
"-" + calendar.get(GregorianCalendar.YEAR));
System.out.println();
System.out.println("Please, enter a, b, c, d, e and f to solve the equation:");
try{
System.out.println("a: ");
a = input.nextDouble();
System.out.println("b: ");
b = input.nextDouble();
System.out.println("c: ");
c = input.nextDouble();
System.out.println("d: ");
d = input.nextDouble();
System.out.println("e: ");
e = input.nextDouble();
System.out.println("f: ");
f = input.nextDouble();
le.setA(a);
le.setB(b);
le.setC(c);
le.setD(d);
le.setE(e);
le.setF(f);
if(le.isSolvable()){
System.out.println("x is: "+ le.getX() + "\ny is: "+ le.getY());
}else {
System.out.println("The equation has no solution.");
}
//System.out.println("Would you like to re-run the program( y or n)");
//ch = input.next().charAt(0);
}
catch (Exception ee) {
System.out.println("Invalid input");
//System.out.println("Would you like to re-run the program( y or n)");
ch = input.next().charAt(0);
}
System.out.println("Would you like to re-run the program( y or any other input to quit)");
ch = input.next().charAt(0);
}while(ch == 'y' || ch == 'Y');
}
do-whileloop is considered as bad practice because to see the continuation condition, you have to scroll down to the very end of it. Rather usewhile.