我正在逐字逐句地比较字符串。
以下是导致问题的代码的一部分:
switch(line[1]) {
case 'u':
switch(line[2]) {
case 't':
switch(line[3]) {
case 't':
switch(line[4]) {
case 'o':
switch(line[5]) {
case 'n':
switch(line[6]) {
case 's':
printf("buttons\n");
case ' ':
printf("not buttons\n");
break;
}
break;
}
break;
}
break;
}
break;
}
}
对于line[6]
,如果存在s
字符,则应打印出“按钮”,如果有空格,则应打印出“不按钮”
如果我有一个包含以下内容的配置文件:
buttons 13
button 3
buttons 3
我明白了:
buttons
not buttons
buttons
not buttons
如果我有:
buttons 3
我明白了:
buttons
not buttons
我为每个按钮输入设置了“按钮”和“非按钮”,并且没有为“按钮3”条目获取任何内容
感谢
答案 0 :(得分:2)
not buttons
因为你在buttons
之后没有中断,所以{@ 1}}总会得到case 's'
。因此line[6]
s
时它不会停止1}}。
并且您使用所有这些嵌套开关来比较字符串。请使用strcmp
检查其buttons
或button
。
答案 1 :(得分:2)
Instead of complicating nested switch, use this
FILE * fi; // input file handle
char line[9], c;
while (feof(fi) == 0) {
// Read only required chars
fgets(line, 8, fi);
line[8] = '\0';
while ((c = getc(fi)) != '\n' && c != EOF);
// Simplified comparison
if (strncmp(line, "button", 6) == 0) {
if (line[6] == 's') printf("buttons\n");
else if (line[6] == ' ') printf("not buttons\n");
}
}
答案 2 :(得分:1)
这是一个更简单,更清晰的代码版本:
package com.test.jersey;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
@Path("/test")
public class Test{
@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> getVersions() throws FileNotFoundException {
Map<String, String> response = new HashMap<String, String>();
response.put("I wonder", "If it works");
return response;
}
}