This is what I have so far. My program terminates after repeating the while loop
ID: 3575216 • Letter: T
Question
This is what I have so far. My program terminates after repeating the while loop once instead of keep getting input. I attached the output I am getting and what it is supposed to look like. Please let me know if you can help. Thank you.
public class DistanceBetweenPoints{
public void calculateDistance(){
double x1, y1, x2, y2;
String check;
Scanner input = new Scanner(System.in);
System.out.print("Enter x1: ");
x1 = input.nextDouble();
System.out.print("Enter y1: ");
y1 = input.nextDouble();
System.out.print("Enter x2: ");
x2 = input.nextDouble();
System.out.print("Enter y2: ");
y2 = input.nextDouble();
double distance = caldis(x1, y1, x2, y2);
System.out.println("The distance between points " + "(" + x1 + "," + y1 + ") and (" + x2 + "," + y2 + ") is " + distance);
System.out.println("Enter 'y' if you want to enter another pair of points 'n' if you do not:");
check = input.next();
while (check.equalsIgnoreCase("Y"))
{
System.out.print("Enter x1: ");
x1 = input.nextDouble();
System.out.print("Enter y1: ");
y1 = input.nextDouble();
System.out.print("Enter x2: ");
x2 = input.nextDouble();
System.out.print("Enter y2: ");
y2 = input.nextDouble();
distance = caldis(x1, y1, x2, y2);
System.out.println("The distance between points " + "(" + x1 + "," + y1 + ") and (" + x2 + "," + y2 + ") is " + distance);
System.out.println("Enter 'y' if you want to enter another pair of points 'n' if you do not:");
if (check.equalsIgnoreCase("N"))
{
break;
}
}
}
public double caldis(double x1, double y1, double x2, double y2)
{
return Math.sqrt(Math.pow((x1-x2), 2) + Math.pow((y1-y2),2));
}
public static void main ( String args[] )
{
DistanceBetweenPoints distance = new DistanceBetweenPoints ();
distance.calculateDistance ();
}
}
Explanation / Answer
The code for calculateDistance method can be reduced like this by keeping check value to Y by default.. and coming to your query ...you missed to keep " check = input.next() " after System.out statement in the while loop hence you are unable to input yes or no. refer this code for that method:
public void calculateDistance(){
double x1, y1, x2, y2;
String check;
check="Y";
Scanner input = new Scanner(System.in);
double distance;
while (check.equalsIgnoreCase("Y"))
{
System.out.print("Enter x1: ");
x1 = input.nextDouble();
System.out.print("Enter y1: ");
y1 = input.nextDouble();
System.out.print("Enter x2: ");
x2 = input.nextDouble();
System.out.print("Enter y2: ");
y2 = input.nextDouble();
distance = caldis(x1, y1, x2, y2);
System.out.println("The distance between points " + "(" + x1 + "," + y1 + ") and (" + x2 + "," + y2 + ") is " + distance);
System.out.println("Enter 'y' if you want to enter another pair of points 'n' if you do not:");
check = input.next();
if (check.equalsIgnoreCase("N"))
{
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.