java Java中的“西蒙说”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28797826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:12:15  来源:igfitidea点击:

"Simon says" in java

javaloops

提问by Gui

"Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence.

“西蒙说”是一种记忆游戏,其中“西蒙”输出 10 个字符(R、G、B、Y)的序列,用户必须重复该序列。

Create a for loop that compares the two strings starting from index 0.

创建一个 for 循环,比较从索引 0 开始的两个字符串。

For each match, add one point to userScore. Upon a mismatch, exit the loop using a break statement.

对于每场比赛,在 userScore 上加一分。在不匹配时,使用 break 语句退出循环。

Ex: The following patterns yield a userScore of 4: simonPattern: R, R, G, B, R, Y, Y, B, G, Y userPattern: R, R, G, B, B, R, Y, B, G, Y

例如:以下模式产生的 userScore 为 4: simonPattern: R, R, G, B, R, Y, Y, B, G, Y userPattern: R, R, G, B, B, R, Y, B, G, Y

import java.util.Scanner;

public class SimonSays {
public static void main (String [] args) {
  String simonPattern = "";
  String userPattern = "";
  int userScore = 0;
  int i = 0;

  userScore = 0;
  simonPattern = "RRGBRYYBGY";
  userPattern  = "RRGBBRYBGY";

  /* Your solution goes here  */
  char s;
  char u;


  for (i=0;i<=10;i++) {
     s = simonPattern.charAt(i);
     u = userPattern.charAt(i);

     if (s == u) {
        userScore = userScore + 1;
        continue;
     }

  }

  System.out.println("userScore: " + userScore);

  return;
 }
}

And the system said:

然后系统说:

Runtime error (commonly due to an invalid array/vector access, divide by 0, etc.).

Why my code didn't work?

为什么我的代码不起作用?

回答by gomezportillo

Instead of hard-programming the length of your String (just because before you should have finger counted it and you may be wrong) you could use

你可以使用而不是硬编程你的字符串的长度(只是因为在你应该用手指计算它之前你可能是错的)

for (int i=0; i < simonPattern.length(); i++)

for (int i=0; i < simonPattern.length(); i++)

Happy coding

快乐编码

Edit: but it could alse generate an Exception, as userPattern could be smaller than simonPattern. You could check it by

编辑:但它也可能生成异常,因为 userPattern 可能小于 simonPattern。你可以通过

if (simonPattern.length() == userPattern.length()) {
     userScore = userScore + 1;
     continue; // this is not really needed but it may if having more else-if
} else{
     break; // This will break the loop as soon as it finds a mismatch

}

}

回答by Gabriele Mariotti

The error is here:

错误在这里:

for (i=0;i<=10;i++) {

Change in

在某一方面的变化

for (i=0;i<10;i++) {

Your string has length = 10.

您的字符串长度为 10。

回答by Marko ?ivanovi?

for (i=0;i<=10;i++) {will execute 11 times (0 to 10) and you only have 10 elements in your strings. Changing the line to for (i=0;i<10;i++) {will execute proper number of times and shouldn't throw any exceptions.

for (i=0;i<=10;i++) {将执行 11 次(0 到 10)并且您的字符串中只有 10 个元素。将行更改为for (i=0;i<10;i++) {将执行适当的次数并且不应引发任何异常。