所以我非常困惑,只是寻求帮助:L。这就是我导师的指示。
说明: 使用哨兵值循环。
询问每位用户:
计算(对于每个客户):
共有三种不同的租赁选项,价格各不相同:经济型@ 31.76,轿车@ 40.32,SUV @ 47.56。 [注意:仅考虑全天单位(无小时费率)]。
销售税 = 总额的 6%。
创建摘要数据:
此外,还包括 IPO、算法和案头检查值(设计文件)。
{我要做什么和我的问题}
package yipe;
public class Umm {
import java.util.*;
int count = 0;
static int CarType, days;
static double DailyFee, Total;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("What vehical would you like to rent?\n");
System.out.println("Enter 1 for an economy car\n");
System.out.println("Enter 2 for a sedan car\n");
System.out.println("Enter 3 for an SUV");
CarType = keyboard.nextInt();
if (CarType == '1')
DailyFee=(int)31.76;
else if(CarType == '2')
DailyFee=(int)40.32;
else if(CarType == '3')
DailyFee=(int)43.50;
System.out.print("Please enter the number of days rented. (Example; 3) : ");
days = keyboard.nextInt();
Total = (DailyFee * days * 6/100);
System.out.printf("The total amount due is $" + Total);
}
}
请注意,
'1'
实际上是字符1
,而不是整数1
。他们实际上非常不同。在 Java(以及 C#)中,
int
和
char
类型可以相互转换。为了说明这一点,下面实际上打印了 49:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.print((int)'1');
}
}
同样,以下打印
true
:
System.out.println('1' == 49);
如您所见,字符被隐式转换为等效的
int
值。要了解为什么
'1'
特别等于 49,请查看字符的表示方式。特别是,查看ASCII 图表(这是字符编码的常见约定) - 结果表明字符
'1'
是 ASCII 49。事实上,我们可以“反向”执行与上面相同的操作将 ASCII 49“转换”为其等效字符,下面的行将打印
1
:
System.out.println((char)49);
要了解这种转换的工作原理,您可能需要阅读
评论中链接的这篇相当优秀的文章。如果您对 C# 中的工作原理感到好奇,您可能还想阅读这个问题。
还有一点:当您执行DailyFee=(int)31.76
时,将其转换为
int
实际上会“丢弃”小数点后的所有内容,因此这与编写
DailyFee = 31
没有什么不同。这是因为 31 是整数,而 31.76不是(它是有理数)。 一个小风格点:您可以考虑在这里使用
switch
语句。
package demo;
import java.util.Scanner;
public class Carrentals {
public static void main(String[] args) {
Double a,b,c,d,e ;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of days for rent");
a=sc.nextDouble();
System.out.println("Select the type of vehicle");
c=1000*a;
d=1500*a;
e=2000*a;
System.out.println("Press 1 for SUV");
System.out.println("Press 2 for SEDEN");
System.out.println("Press 3 for ECONOMICAL");
b=sc.nextDouble();
if(b == 1)
System.out.println("The rent is "+c);
else if(b == 2)
System.out.println("The rent is "+d);
else if(b == 3)
System.out.println("The rent is "+e);
sc.close();
}
}
package demo;
import java.util.Scanner;
public class carrent {
public static void main(String[] args) {
System.out.println("SELECT THE TYPE OF VEHICLE");
System.out.println("PRESS 1 FOR SUV");
System.out.println("PRESS 2 FOR SEDAN");
System.out.println("PRESS 3 FOR ECONOMICAL");
Scanner sc=new Scanner(System.in);
char z=sc.next().charAt(0);
switch(z) {
case'1':
System.out.println("YOU HAVE SELECTED SUV");
Scanner suv=new Scanner(System.in);
System.out.println("ENTER THE NUMBER OF DAYS");
int a=suv.nextInt();
int rent1=1000*a;
int rent2=750*a;
System.out.println("SELECT THE CLASS OF VEHICLE");
System.out.println("PRESS 1 FOR AC");
System.out.println("PRESS 2 FOR NON AC");
int b=suv.nextInt();
if(b==1)
System.out.println("THE RENT FOR " +a+ " DAYS FOR SUV AC IS " +rent1+ "
Rupees");
else if(b==2)
System.out.println("THE RENT FOR " +a+ " DAYS FOR SUV NON AC IS "
+rent2+ " Rupees");
break;
case'2':
System.out.println("YOU HAVE SELECTED SEDAN");
Scanner SEDAN=new Scanner(System.in);
System.out.println("ENTER THE NUMBER OF DAYS");
int a1=SEDAN.nextInt();
int rent1S=1500*a1;
int rent2S=12000*a1;
System.out.println("SELECT THE CLASS OF VEHICLE");
System.out.println("PRESS 1 FOR AC");
System.out.println("PRESS 2 FOR NON AC");
int b1=SEDAN.nextInt();
if(b1==1)
System.out.println("THE RENT FOR " +a1+ " DAYS FOR SEDAN AC IS "
+rent1S+ " Rupees");
else if(b1==2)
System.out.println("THE RENT FOR " +a1+ " DAYS FOR SEDAN NON AC IS "
+rent2S+ " Rupees");
break;
case'3':
System.out.println("YOU HAVE SELECTED ECONOMICAL");
Scanner ECONOMICAL=new Scanner(System.in);
System.out.println("ENTER THE NUMBER OF DAYS");
int a2=ECONOMICAL.nextInt();
int rent1E=2000*a2;
int rent2E=1700*a2;
System.out.println("SELECT THE CLASS OF VEHICLE");
System.out.println("PRESS 1 FOR AC");
System.out.println("PRESS 2 FOR NON AC");
int b2=ECONOMICAL.nextInt();
if(b2==1)
System.out.println("THE RENT FOR " +a2+ " DAYS FOR SUV AC IS " +rent1E+
" Rupees");
else if(b2==2)
System.out.println("THE RENT FOR " +a2+ " DAYS FOR SUV NON AC IS "
+rent2E+ " Rupees");
break;
default:
System.out.println("ERROR!!!");
System.out.println("PLEASE SELECT BASED ON ABOVE GIVEN INSTRUCTIONS");
}
sc.close();
}
}
公共课汽车{
public String carModel;
public String registrationNumber;
public double rentalRatePerDay;
public boolean availability;
public LocalDate rentalStartDate;
public Car(String carModel, String registrationNumber, double rentalRatePerDay, boolean isAvailability) {
this.carModel = carModel;
this.registrationNumber = registrationNumber;
this.rentalRatePerDay = rentalRatePerDay;
this.availability = true;
}
public String getCarModel() {
return carModel;
}
public void setCarModel(String carModel) {
this.carModel = carModel;
}
public String getRegistrationNumber() {
return registrationNumber;
}
public void setRegistrationNumber(String registrationNumber) {
this.registrationNumber = registrationNumber;
}
public double getRentalRatePerDay() {
return rentalRatePerDay;
}
public void setRentalRatePerDay(double rentalRatePerDay) {
this.rentalRatePerDay = rentalRatePerDay;
}
public boolean isAvailability() {
return availability;
}
public void setAvailability(boolean isAvailability) {
this.availability = isAvailability;
}
// Method to rent the car
public boolean rentCar(LocalDate startDate) {
if (availability) {
this.availability = false;
this.rentalStartDate = startDate;
return true; // Car rented successfully
} else {
return false; // Car is not available
}
}
// Method to return the car
public double returnCar(LocalDate returnDate) {
if (!availability && rentalStartDate != null) {
long numberOfDays = ChronoUnit.DAYS.between(rentalStartDate, returnDate);
if (numberOfDays < 0) {
numberOfDays = 0; // Handle case where return date is before rental start date
}
this.availability = true; // Car is now available
this.rentalStartDate = null; // Reset the rental start date
return numberOfDays * rentalRatePerDay; // Calculate total rental charges
} else {
return 0.0; // Car was not rented, no charges
}
}
// Method to display rental charges
public void displayRentalCharges(LocalDate returnDate) {
if (!availability && rentalStartDate != null) {
double charges = returnCar(returnDate); // Use returnCar to calculate charges
System.out.printf("Total rental charges for %s (%s) from %s to %s: $%.2f%n", carModel, registrationNumber,
rentalStartDate, returnDate, charges);
} else {
System.out.println("Car has not been rented or already returned.");
}
}
public static void main(String[] args) {
}
}