代码的间距显示错误!
public static void switchTest() {
String[] cars = {"TATA", "BMW", "Hero", "Honda", "Bajaj", "Yamaha"};
System.out.println("Enter a car name:");
Scanner sc = new Scanner(System.in);
String userInputCar = sc.nextLine();
switch (userInputCar == cars) {
case "TATA":
System.out.println("You enter TATA as a car name");
break;
case "BMW":
System.out.println("You enter BMW as a car name");
break;
default:
System.out.println("Ops! we are unable to get that name. Thank You:)");
}
}
让我以正确的方式问这个问题,我们将把用户输入与给定的数组进行比较。 所以现在的问题是我们如何做到这一点?
我认为你需要这样做:
public static void switchTest() {
switch ( userInputCar ) {
case "TATA":
System.out.println("You enter TATA as a car name");
break;
case "BMW":
System.out.println("You enter BMW as a car name");
break;
default:
System.out.println("Ops! we are unable to get that name. Thank You:)");
}
}
A
switch
就像有很多“如果..”命令。
所以比较不在开关中:
switch (userInputCar == cars)
但是在
case
switch (userInputCar) { // Value to check
case "TATA": // if "TATA".equals(userInputCar)
break;
case "BMW": // else if "BMW".equals(userInputCar)
break;
default: // else
}
对照清单:
if(Arrays.asList(cars).contains(userInputCar)) {
// Found
} else {
// Not found
}
如果你想尝试用数组检查用户输入你可以检查下面的代码
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
String[] cars = {"TATA", "BMW", "Hero", "Honda", "Bajaj", "Yamaha"};
boolean check=true;
System.out.println("Enter a car name:");
Scanner sc = new Scanner(System.in);
String userInputCar = sc.nextLine();
for(int i=0;i<cars.length;i++)
{
if(cars[i].equals(userInputCar))
{
System.out.println("You enter "+ cars[i] +" as a car name");
check=false;
}
}
if(check)
{
System.out.println("Ops! we are unable to get that name. Thank You");
}
}
/*
Here is the Output
Enter a car name:
Hero
You enter Hero as a car name
*/
}
userInputCar == cars
....它不会那样工作,即使您使用 String#equals() 方法来检查正确的字符串相等性。如果有的话,它应该是这样的:
switch (userInputCar.toUpperCase()) {
case "TATA":
System.out.println("You enter TATA as a car name");
break;
case "BMW":
System.out.println("You enter BMW as a car name");
break;
default:
System.out.println("Oops! we are unable to get that name. Thank You:");
}
注意如何使用
userInputCar.toUpperCase()
。这允许用户以任何字母大小写输入汽车名称,仍然会产生肯定的结果。
在任何情况下...由于汽车名称已经包含在字符串 [] 数组 (
cars[]
) 中,您应该检查名称数组而不是创建 switch/case
机制来执行此操作,例如:
// Declare Scanner object as class global:
private final static Scanner sc = new Scanner(System.in);
public static String getCarName() {
String[] cars = {"TATA", "BMW", "Hero", "Honda", "Bajaj", "Yamaha"};
String carName = "";
while (carName.isEmpty()) {
System.out.print("Enter a car name (q to quit): -> ");
carName = sc.nextLine();
// See if 'q' for quit was supplied by User:
if (carName.equalsIgnoreCase("q")) {
return null;
}
// Flag to indicate that supplied car was found within Array:
boolean found = false;
/* Iterate the the cars[] array and see if the User supplied
car exists within that array (ignoring letter case): */
for (String car: cars) {
if (car.equalsIgnoreCase(carName)) {
/* The supplied car name exists. Make the supplied
car name equal the proper car name in array: */
carName = car;
found = true; // Set the `found` flag to true:
break; // Break out of `for` loop:
}
}
// Was the supplied car name found?
if (!found) {
// No...Inform User and have him/her try again...
System.out.println("Oops! We are unable to get that name ("
+ carName + "). Try again..."
+ System.lineSeparator());
carName = ""; // Empty carName to ensure re-loop:
}
}
/* If we get to this point then validation was successfull,
return the car name: */
return carName;
}
使用上面的方法,你可能会遇到这样的情况:
String car = getCarName();
if (car == null) {
System.out.println("'Quit' Provided!");
return;
}
System.out.println("You entered " + car + " as a car name.");