I know i need a while true loop i believe but where do i insert it and hwo does
ID: 3914541 • Letter: I
Question
I know i need a while true loop i believe but where do i insert it and hwo does it look. I need the user to just be able to continously input 2 cities and get the distance between them until the user puts a number outside 1 - 10
I know this was just answered by ronaldo. but i may have been unclear on what i needed. I need the program to just cancel and stop when an invalid input is put in not state invalid and keep going. sorry about repost didnt know of any other way to get answered
double [][] array =
{ {0,152.7,188.4,571.3,216.4,42.21,186.5,98.4,201.0,224.6},
{152.7,0,239,634.7,65,167.3,97.7,191.4,328.6,352.2},
{188.4,239,0,74.7,301.6,154.2,198.2,106.1,233.9,254.4},
{571.3,634.7,74.7,0,692.9,607.3,732.5,641.8,570.4,540.5},
{216.4,65,301.6,692.9,0,232,127.2,257.4,377.3,400.9},
{42.21,167.3,154.2,607.3,232.0,0.189, 7,45.9,163.7,187.3},
{186.3,97.7,198.2,732.5,127.2,189.7,0,183,346.5,370.2},
{98.4,191.4,106.1,641.8,257.4,45.9,183,0,177,197.5},
{201,328.6,233.9,570.4,377.3,163.7,346.5,177,0,30.2},
{224.6,352.2,254.4,540.5,400,187.3,370.2,197.5,30.2,0}
};
Scanner input= new Scanner(System.in);
System.out.println( "1. Killeen"+ " 2. Dallas"
+" 3. Houston"+ " 3. Houston"+" 4. El-Paso" + " 5. Sherman"+
" 6. Taylor"+ " 7. Tyler"+ " 8. Dime Box"+ " 9. Big Foot"+
" 10. Frio Town"+" Please enter starting city");
int start = input.nextInt();
System.out.println("1. Killeen"+ " 2. Dallas"
+" 3. Houston"+ " 3. Houston"+" 4. El-Paso" + " 5. Sherman"+
" 6. Taylor"+ " 7. Tyler"+ " 8. Dime Box"+ " 9. Big Foot"+
" 10. Frio Town"+" Please enter destination!" );
int finish = input.nextInt();
System.out.println(array[start-1][finish-2]+" Miles away from each other");
Explanation / Answer
import java.util.Scanner; public class DistanceCities { public static void main(String[] args) { double [][] array = { {0,152.7,188.4,571.3,216.4,42.21,186.5,98.4,201.0,224.6}, {152.7,0,239,634.7,65,167.3,97.7,191.4,328.6,352.2}, {188.4,239,0,74.7,301.6,154.2,198.2,106.1,233.9,254.4}, {571.3,634.7,74.7,0,692.9,607.3,732.5,641.8,570.4,540.5}, {216.4,65,301.6,692.9,0,232,127.2,257.4,377.3,400.9}, {42.21,167.3,154.2,607.3,232.0,0.189, 7,45.9,163.7,187.3}, {186.3,97.7,198.2,732.5,127.2,189.7,0,183,346.5,370.2}, {98.4,191.4,106.1,641.8,257.4,45.9,183,0,177,197.5}, {201,328.6,233.9,570.4,377.3,163.7,346.5,177,0,30.2}, {224.6,352.2,254.4,540.5,400,187.3,370.2,197.5,30.2,0} }; Scanner input= new Scanner(System.in); int start, finish; while (true) { System.out.println("1. Killeen" + " 2. Dallas" + " 3. Houston" + " 3. Houston" + " 4. El-Paso" + " 5. Sherman" + " 6. Taylor" + " 7. Tyler" + " 8. Dime Box" + " 9. Big Foot" + " 10. Frio Town" + " Please enter starting city"); start = input.nextInt(); if(start < 1 || start > 10) { break; } System.out.println("1. Killeen" + " 2. Dallas" + " 3. Houston" + " 3. Houston" + " 4. El-Paso" + " 5. Sherman" + " 6. Taylor" + " 7. Tyler" + " 8. Dime Box" + " 9. Big Foot" + " 10. Frio Town" + " Please enter destination!"); finish = input.nextInt(); if(finish < 1 || finish > 10) { break; } System.out.println(array[start - 1][finish - 2] + " Miles away from each other"); } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.