1
在這種情況下,如果用戶輸入數組fruits
中的任何一個可用值,我希望if
語句來true
,但我不明白如何做到這一點。使用字符串數組的值在if語句中使用
import java.util.Scanner;
public class Strings {
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
String[] fruits = {"Apple", "apple", "Banana", "banana", "Orange", "orange"};
System.out.println("Enter a name of a fruit: ");
String input = Scan.nextLine();
if(/*input = any one of the values in Array fruits*/){
System.out.println("Yes, that's a fruit");
}
else{
System.out.println("No, that's not a fruit.");
}
的可能重複(http://stackoverflow.com/questions/1128723/在-java-how-can-i-test-if-an-array-contains-a-certain-value) – Erik
Arrays.asList(fruits).contains(input)will for you :) – Parth
你需要一個循環來遍歷數組搜索用戶輸入,如果匹配 – Arvind