Как мне написать пункт о том, как распознать начальный ввод через класс сканера? [дублировать]JAVA

Программисты JAVA общаются здесь
Anonymous
Как мне написать пункт о том, как распознать начальный ввод через класс сканера? [дублировать]

Сообщение Anonymous »

Я довольно новичок в Java, поэтому я могу ошибаться в терминологии. Во время написания программы для определения оценки стоимости транспортного средства у меня есть заявление с просьбой пользователя о состоянии их автомобиля, представленного либо 1 для чистоты, 2 для среднего и 3 для грубых. Я написал еще один пункт, чтобы многократно спросить пользователя о условии, если он не ввел допустимое значение. Тем не менее, даже при первоначальном допустимом входе он все равно будет запрашивать условие автомобиля, хотя действительный вход будет успешным в это время. Я прикрепил то, что у меня есть, что касается ссылки. < /P>
import java.text.DecimalFormat;
import java.util.Scanner;
public class Abramyan_week5b{
public static void main( String[] args){
double vehiclePrice, vehicleMileage, vehicleAge, vehicleCond, deprPerMile, mileDepr, condPct, condDepr, appraisedPrice;
Scanner k = new Scanner(System.in);
DecimalFormat df1 = new DecimalFormat("#,##0.00");
System.out.print("Enter the purchasing price of the vehicle = ");
vehiclePrice = k.nextDouble();
System.out.print("Enter the current mileage of the vehicle = ");
vehicleMileage = k.nextDouble();
System.out.print("Enter the age of the vehicle in years = ");
vehicleAge = k.nextDouble();
System.out.print("Enter the vehicle condition ('1' for clean, '2' for average and '3' for rough) = ");
vehicleCond = k.nextDouble();
while ((vehicleCond != 1) || (vehicleCond != 2) || (vehicleCond != 3)){
System.out.print("Enter the vehicle condition ('1' for clean, '2' for average and '3' for rough) = ");
vehicleCond = k.nextDouble();
if (vehicleCond == 1){
condPct = 0.02;
break;
}
else if (vehicleCond == 2){
condPct = 0.03;
break;
}
else if (vehicleCond == 3){
condPct = 0.04;
break;
}
}
if (vehiclePrice > 60000)
deprPerMile = 0.6;
else if (vehiclePrice > 40000)
deprPerMile = 0.5;
else if (vehiclePrice > 30000)
deprPerMile = 0.4;
else if (vehiclePrice > 20000)
deprPerMile = 0.3;
else
deprPerMile = 0.2;
if (vehicleCond == 1)
condPct = 0.02;
else if (vehicleCond == 2)
condPct = 0.03;
else
condPct = 0.04;
mileDepr = vehicleMileage * deprPerMile;
condDepr = vehiclePrice * vehicleAge * condPct;
appraisedPrice = vehiclePrice - mileDepr - condDepr;
System.out.println(" The depreciated value of the vehicle for mileage is $" + df1.format(mileDepr));
System.out.println(" The depreciated value of the vehicle for the condition is $" + df1.format(condDepr));
System.out.println(" The current appraised value of the vehicle is $" + df1.format(appraisedPrice));
}
}


Подробнее здесь: https://stackoverflow.com/questions/795 ... he-scanner

Вернуться в «JAVA»