我想把Char'S'设置为80美元 当它要求用户输入汽车尺寸时,他们应输入“S”,“M”或“L”并在字符串“每日租金:80美元”上显示输出,如果他们输入“M”,则应显示“每日租用”费率:100美元“
举个例子,说明如何将char'S'设置为$ 80
package carRental;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Stage2 {
public static void main(String[] args) {
String s1 = getInput("Enter Customer Name: ");
String s2 = getInput("Enter Customer Address: ");
String s3 = getInput("Enter Customer phone No: ");
String s4 = getInput("Enter Customer License No: ");
String s5 = getInput("Enter Credit Card No: ");
String s6 = getInput("Enter Expiry Date: ");
String s7 = getInput("Enter Hire Length (in days): ");
String s8 = getInput("Enter Make/Model of Assigned Vehicle: ");
String s9 = getInput("Enter Registration No of Assigned Vehicle: ");
String s11 = getInput("Enter Car Size - (S)mall, (M)edium or (L)arge: ");
String s12 = getInput("Enter Days Hired: ");
double hirerate = Double.parseDouble(s11);
int noofdays = Integer.parseInt(s7);
double result = hirerate * noofdays;
System.out.println("Customer Details: ");
System.out.println("Name: " + s1);
System.out.println("Address: " + s2);
System.out.println("Phone Number: " + s3);
System.out.println("License No: " + s4);
System.out.println("Credit Card No: " + s5);
System.out.println("Expiry Date: " + s6);
System.out.println("Car Hire Details: ");
System.out.println("Make/Model: " + s8);
System.out.println("Registration No: " + s9);
System.out.println("Hire Length (days): " + s7);
System.out.println("Daily Hire Rate: " + s11);
System.out.println("Basic Hire Charge: " + result);
System.out.println("Days Hired: " + s12);
System.out.println("Late Return Surcharge: ");
System.out.println("Adjusted Hire Charge: ");
}
private static String getInput(String prompt) {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return stdin.readLine();
} catch (Exception e) {
return "Error: " + e.getMessage();
}
}
}
答案 0 :(得分:0)
您可以使用Switch Operator
。
char c = /* whatever */;
switch(c) {
case 's':
case 'S':
//Your conditions here for S
break;
case 'm':
case 'M':
//Your conditions here for M
break;
// (...)
case 'l':
case 'L':
//Your conditions here for L
break;
}
或者您可以在切换前将其标准化为小写或大写。
char c = Character.toUpperCase(/* whatever */);
答案 1 :(得分:0)
String rate(final char type) {
switch (Character.toUpperCase(type)) {
case 'S':
return "$60";
case 'M':
return "$80";
case 'L':
return "$100";
default:
System.err.println("No valid entry for " + type);
return "";//or throw
}
}