我正在做" Rock,Paper,Scissors"游戏,我想检查输入是否与任何数组匹配,现在我有这个代码,它的工作完美:
String[] rockArray = {"Rock","rock","1"},
paperArray = {"Paper","paper","2"},
scissorsArray = {"Scissors","scissors","3"};
String[][] answersArray = {rockArray, paperArray, scissorsArray};
//Scanner
Scanner scan = new Scanner(System.in);
// Input
System.out.print("\nRock, Paper or Scissors? -> ");
String answer = scan.next();
// Checking if the input contains the right values
while(!Arrays.asList(answerRock).contains(answer) &&
!Arrays.asList(answerPaper).contains(answer) &&
!Arrays.asList(answerScissors).contains(answer)) {
System.out.print("Try again: ");
answer = scan.next();
}
但我想说的很简单,这是我的想法,但由于某种原因,它不起作用:
// Here is my arrays
String[] rockArray = {"Rock","rock","1"},
paperArray = {"Paper","paper","2"},
scissorsArray = {"Scissors","scissors","3"};
String[][] globalArray = {rockArray, paperArray, scissorsArray};
// Input
System.out.print("\nRock, Paper or Scissors? -> ");
String answer = scan.next();
//Validation
while(!Arrays.asList(globalArray).contains(answer) {
System.out.println("Try again: ");
answer = scan.next();
}
非常感谢!
答案 0 :(得分:3)
String[][] globalArray = {rockArray, paperArray, scissorsArray};
...
Arrays.asList(globalArray)
对Arrays.asList()
的调用将创建包含3个元素的列表,3个数组,而不是包含数组内容的列表。然后,当您使用contains
搜索元素时,您将始终获得false
,因为answer
将永远不会出现在数组上(如何,String
不是List<String> allAnswers = Arrays.asList(rockArray);
allAnswers.addAll(Arrays.asList(paperArray));
allAnswers.addAll(Arrays.asList(scissorsArray));
...
while(!allAnswers.contains(answer)) {
...
一个数组)。
我建议你这样做:
Arrays.asList()
这也可以避免一遍又一遍地调用<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:textAlignment="center"
tools:openDrawer="start">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:contextClickable="false"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
,从而提高性能。
答案 1 :(得分:1)
Arrays.asList(...)
不起作用,因为List<String[]>
方法会输出List<String>
而不是asList(...)
。
如果您真的想使用String[] allPossibleInputString
,则必须输入SELECT
votes.movieID,
likes.like_count,
dislikes.dislike_count
FROM votes
LEFT join (
SELECT movieID, count(Vote) as like_count
FROM votes
WHERE Vote = 'li'
GROUP BY movieID
) likes ON likes.movieID = votes.movieID
LEFT join (
SELECT movieID, count(Vote) as dislike_count
FROM votes
WHERE Vote = 'dli'
GROUP BY movieID
) likes ON likes.movieID = votes.movieID
答案 2 :(得分:0)
您的第二个代码段不起作用,因为您无法执行此类!Arrays.asList(globalArray).contains(answer)
之类的操作。
一种解决方案是将三个数组的内容复制到全局数组中,如下所示:
String[] rockArray = {"Rock","rock","1"},
paperArray = {"Paper","paper","2"},
scissorsArray = {"Scissors","scissors","3"};
String[] globalArray = new String[9];
/* Copy Contents To Global Array */
System.arraycopy(rockArray, 0, globalArray, 0, 3);
System.arraycopy(paperArray, 0, globalArray, 3, 3);
System.arraycopy(scissorsArray, 0, globalArray, 6, 3);
现在你可以这样做:
while(!Arrays.asList(globalArray).contains(answer)) {
System.out.println("Try again: ");
answer = scan.nextLine();
}