我已经完成了我的java应用程序,我遇到了问题,并非所有功能都有效 因此,应用程序必须让我添加经度和经度的新城市,如果我想要(这里我有问题,我不能删除它们)从搜索中删除城市。最后,计算距离城市的距离并计算总数。所以这是我的代码...
主
package ex1;
import java.util.*;
import java.math.*;
import java.io.*;
public class Itinerariu {
public static List<Oras> drum = new ArrayList<Oras>();
public static void main(String[] args)
{
Scanner tastatura = new Scanner(System.in);
int a;
Oras drum = new Oras();
do{
System.out.println("Meniu");
System.out.println("----------");
System.out.println("1-Adaugarea oras");
System.out.println("2-Stergerea unui oras");
System.out.println("3-Distanta totala");
System.out.println("4-Distanta dintre doua orase alaturate");
a=tastatura.nextInt();
switch(a)
{
case 1: System.out.println("\nAdaugati orasul\n");
adaugareOras();
break;
case 2: System.out.println("\nStergeti orasul\n");
stergereOras();
break;
case 3:// lungimeOras();
System.out.println("\n");
break;
case 4: System.out.println("\nLungimea dintre doua orase alaturate\n");
/*try{
salvareFisier();
}catch (Exception e){
System.out.println("Eroare");
}*/
break;
default: a=0;
}
}
while(a!=0);
}
/*private static void salvareFisier()
{
for(int i = 0; i<drum.size()-1; i++){
for(int j = 0; i<drum.size(); j++){
if(drum.get(i).getPozitie() > drum.get(j).getPozitie()){
Oras aux = drum.get(i);
drum.add(i, drum.get(j));
drum.add(j, aux);
}
}
}
try {
FileOutputStream fOut = new FileOutputStream("distante.txt");
PrintStream ps = new PrintStream(fOut);
for(int i = 0; i <drum.size()-1; i++){
ps.println("d(" + drum.get(i).getNume()+","+drum.get(i+1).getNume()+")="+distOras(drum.get(i), drum.get(i+1)));
}
ps.close();
fOut.close();
} catch(IOException ex){
System.out.println("Nu s-a putut crea fisierul");
System.exit(1);
}
}
private static double lungimeOras()
{
for(int i = 0; i < drum.size() - 1 ; i++)
for(int j = 0; j< drum.size() ; j++)
if(drum.get(i).getPozitie() > drum.get(j).getPozitie()){
Oras aux = drum.get(i);
drum.add(i, drum.get(j));
drum.add(j, aux);
}
double l = 0;
for(int i = 0; i < drum.size() - 1; i++){
l += distOras(drum.get(i), drum.get(i+1));
}
return l;
}
private static double distOras(Oras oras, Oras oras2)
{
double distanta, long1, long2, lat1, lat2;
long1=oras.getLongitudine()*79;
long2=oras2.getLongitudine()*79;
lat1=oras.getLatitudine()*111;
lat2=oras2.getLatitudine()*111;
distanta = Math.sqrt((long1-long2)*(long1-long2)+(lat1-lat2)*(lat1-lat2));
return distanta;
}
*/
private static void stergereOras()
{
Scanner tastatura = new Scanner(System.in);
String name;
System.out.println("Introduceti numele orasului care doriti sa fie sters");
name = tastatura.next();
for(int i = 0 ; i <= drum.size() ; i++){
if(drum.get(i).getNume() == name){
drum.remove(name);}}
for(Oras o:drum)
System.out.println(o.getNume());
}
private static void adaugareOras()
{
Scanner tastatura = new Scanner(System.in);
Oras x = new Oras();
int l;
int index;
String n;
double lat;
double lon;
System.out.println("Introduceti pozitia orasului in itinerariu");
index = tastatura.nextInt();
x.setPozitie(index);
System.out.println("Introduceti numele orasului:");
n = tastatura.next();
x.setNume(n);
System.out.println("Introduceti latitudinea:");
lat = tastatura.nextInt();
x.setLatitudine(lat);
System.out.println("Introduceti longitudinea:");
lon = tastatura.nextInt();
x.setLongitudine(lon);
drum.add(x);
System.out.println("----------");
System.out.println("1-Introducerea urmatorului oras");
System.out.println("2-Revenire la meniul principal");
l = tastatura.nextInt();
switch(l){
case 1:
adaugareOras();
break;
default:
break;
}
}
}
这是第二个代码。
package ex1;
public class Oras {
public int pozitie;
public String nume;
public double latitudine;
public double longitudine;
public int getPozitie() {
return pozitie;
}
public void setPozitie(int pozitie) {
this.pozitie = pozitie;
}
public String getNume() {
return nume;
}
public void setNume(String nume) {
this.nume = nume;
}
public double getLatitudine() {
return latitudine;
}
public void setLatitudine(double latitudine) {
this.latitudine = latitudine;
}
public double getLongitudine() {
return longitudine;
}
public void setLongitudine(double longitudine) {
this.longitudine = longitudine;
}
}
以下是删除城市的问题:
private static void stergereOras()
{
Scanner tastatura = new Scanner(System.in);
String name;
System.out.println("Introduceti numele orasului care doriti sa fie sters");
name = tastatura.next();
for(int i = 0 ; i <= drum.size() ; i++){
if(drum.get(i).getNume() == name){
drum.remove(name);}}
for(Oras o:drum)
System.out.println(o.getNume());
}
答案 0 :(得分:0)