我正在为For循环重做一个类问题,并且不能从跑分中获得总数。我试图使用字符串比较,但这可能是问题所在。作业是:
“Simon Says”是一个记忆游戏,其中“Simon”输出10个字符(R,G,B,Y)的序列,用户必须重复该序列。创建一个for循环,比较从索引0开始的两个字符串。对于每个匹配,将一个点添加到userScore。在不匹配时,使用break语句退出循环。例如:以下模式产生的userScore为4:
simonPattern:RRGBRYYBGY
userPattern:RRGBBRYBGY
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 begins here */
String ss1 = simonPattern.substring(0, 2);
String us1 = userPattern.substring(0, 2);
String ss2 = simonPattern.substring(2, 3);
String us2 = userPattern.substring(2, 3);
String ss3 = simonPattern.substring(4, 5);
String us3 = userPattern.substring(4, 5);
String ss4 = simonPattern.substring(6, 7);
String us4 = userPattern.substring(6, 7);
String ss5 = simonPattern.substring(8, 9);
String us5 = userPattern.substring(8, 9);
for (i = 0; i < simonPattern.length(); i++) {
if (ss1.equals(us1)) {
userScore = userScore + 1;
}
if (ss2.equals(us2)){
userScore = userScore + 1;
}
if (ss3.equals(us3)){
userScore = userScore + 1;
}
if (ss4.equals(us4)){
userScore = userScore + 1;
}
if (ss5.equals(us5)){
userScore = userScore + 1;
}
else{
break;
}
}
/* ^ Your solution goes here ^ */
System.out.println("userScore: " + userScore);
return;
}
}
答案 0 :(得分:0)
您的for循环更改 public static string RenderView(this Controller controller, string viewName, object model, HttpContext currentContext)
{
return RenderView(controller, viewName, new ViewDataDictionary(model), currentContext);
}
public static string RenderView(this Controller controller, string viewName, ViewDataDictionary viewData, HttpContext currentContext)
{
var routeData = new RouteData();
routeData.Values.Add("Controller", "ASPXTest"); //must match your Controller name
routeData.Values.Add("Action", "Test"); //must match your Action name
HttpContextWrapper wrapper = new HttpContextWrapper(currentContext);
controller.ControllerContext = new ControllerContext(wrapper, routeData, controller);
var viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, null);
StringWriter stringWriter;
viewData = new ViewDataDictionary();
using (stringWriter = new StringWriter())
{
var viewContext = new ViewContext(
controller.ControllerContext,
viewResult.View,
viewData,
controller.ControllerContext.Controller.TempData,
stringWriter);
viewResult.View.Render(viewContext, stringWriter);
viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
}
return stringWriter.ToString();
}
到if (ss1 == us1)
的问题与else结果相同,因为String是引用类型而不是原始数据类型
原始数据类型包括if (ss1.equals(us1))
,int
,double
,long
,float
,byte
,short
,{ {1}}并且所有对象都是引用类型
boolean
答案 1 :(得分:0)
您正确地将循环更改为迭代simonPattern
的长度。现在你只需要抛弃循环中的代码并实际使用迭代器变量i
。
要从字符串中提取单个字符,您可以继续使用substring(i, i+1)
获取String
值,但对于单个字符,最好使用charAt(i)
。
另外,要将变量增加1,最好使用userScore++
而不是userScore = userScore + 1
,尽管这也有效。
这意味着您的代码应为:
String simonPattern = "RRGBRYYBGY";
String userPattern = "RRGBBRYBGY";
int userScore = 0;
for (int i = 0; i < simonPattern.length(); i++) {
char c1 = simonPattern.charAt(i);
char c2 = userPattern.charAt(i);
if (c1 != c2)
break;
userScore++;
}
System.out.println("userScore: " + userScore); // prints 4
答案 2 :(得分:0)
您可能希望通过迭代userPattern来考虑这样做:
int userScore = 0;
String simonPattern = "RRGBRYYBGY";
String userPattern = "RRGBBYYBGY";
for (int i = 0; i < userPattern.length(); i++) {
String p = userPattern.substring(0, 1 + i);
if (simonPattern.startsWith(p)) {
userScore = userScore + 1;
}
}
if (userScore < simonPattern.length()) {
System.out.println("You got " + userScore + " letters deep out of " +
simonPattern.length() + " before you made a mistake.");
}
else {
System.out.println("YOU WIN!");
}