我有一个关于天气的问题,在for循环中使用switch语句是合法的。它在Eclipse下面编写的方式给了我一个错误,并且不允许我在for循环中的第二个语句中使用switch(n)。我想做的是写...
使用for循环和switch语句是否有更好的方法解决下面的问题?我想用不同的数字和不同的情况写出不同的段落。所以1个拇指,2个鞋子,3个膝盖......直到10个。
import acm.program.*;
public class SingSong extends ConsoleProgram {
public void run() {
for (int n = 1; n <= 10; n++) {
println("This old man, he played " + n);
println("He played knick-knack on my" + switch(n));
println("With a knick-knack, paddy-whack,");
println("Give your dog a bone");
println("This old man came rolling home");
switch (n) {
case 1 : println("thumb"); break;
case 2 : println("shoe"); break;
case 3 : println("knee"); break;
case 4 : println("door"); break;
case 5 : println("hive"); break;
case 6 : println("sticks"); break;
case 7 : println("heaven"); break;
case 8 : println ("pate"); break;
case 9 : println("spine"); break;
case 10 : println("shin"); break;
}
}
}
}
答案 0 :(得分:5)
您想要的是一种方法:
private String which(int n) {
switch (n) {
case 1 : return "thumb";
case 2 : return "show";
case 3 : return "knee";
case 4 : return "door";
case 5 : return "hive";
case 6 : return "sticks";
case 7 : return "heaven";
case 8 : return "pate";
case 9 : return "spine";
case 10 : return "shin";
default: return "";
}
}
然后替换
println("He played knick-knack on my" + switch(n));
与
println("He played knick-knack on my" + which(n));
另一种更简洁的方法是创建一个字符串数组:
static final String[] which = {
"thumb","show","knee","door","hive", "sticks","heaven","pate","spine","shin"};
然后只需使用
println("He played knick-knack on my" + which[n]);
答案 1 :(得分:4)
创建一个包含整数到字符串映射的Map<Integer, String>
并使用Map.get()
来获取字符串:
Map<Integer, String> m = new HashMap<Integer, String>();
m.put(1, "thumb");
// etc
m.put(10, "shin");
println("He played knick-knack on my" + m.get(n));
答案 2 :(得分:3)
println("He played knick-knack on my" + switch(n));
这不会打电话给你的Switch。开关不是一种方法。 为了获得所需的输出,
println("This old man, he played " + n);
println("He played knick-knack on my" );
switch (n){
case 1 : print("thumb"); break;
case 2 : println("shoe"); break;
case 3 : println("knee"); break;
case 4 : println("door"); break;
case 5 : println("hive"); break;
case 6 : println("sticks"); break;
case 7 : println("heaven"); break;
case 8 : println ("pate"); break;
case 9 : println("spine"); break;
case 10 : println("shin"); break;
}
或者更好地遵循@Marko Topolnik方法,我认为这是最好的方法。
答案 3 :(得分:1)
SWitch(n)不是函数,不能这样使用。对于编译器来说,这就像启动一个switch语句,但完全错误,因此它返回一个错误。我认为要正确地执行此操作,您应该将case开关放在不同的方法中,或者直接放在第二行之后。像这样:
import acm.program.*;
public class SingSong extends ConsoleProgram {
public void run(){
for ( int n = 1; n <= 10; n++){
println("This old man, he played " + n);
print("He played knick-knack on my ");
switch (n){
case 1 : print("thumb"); break;
case 2 : print("show"); break;
case 3 : print("knee"); break;
case 4 : print("door"); break;
case 5 : print("hive"); break;
case 6 : print("sticks"); break;
case 7 : print("heaven"); break;
case 8 : print("pate"); break;
case 9 : print("spine"); break;
case 10 : print("shin"); break;
}
println("With a knick-knack, paddy-whack,");
println("Give your dog a bone");
println("This old man came roiling home");
}
}
}
这应该有效。或者用不同的方法:
import acm.program.*;
public class SingSong extends ConsoleProgram {
public void run(){
for ( int n = 1; n <= 10; n++){
println("This old man, he played " + n);
print("He played knick-knack on my");
switcher(n);
print("\n");
println("With a knick-knack, paddy-whack,");
println("Give your dog a bone");
println("This old man came roiling home");
}
}
private void switcher(int n){
switch(n){
case 1 : print("thumb"); break;
case 2 : print("show"); break;
case 3 : print("knee"); break;
case 4 : print("door"); break;
case 5 : print("hive"); break;
case 6 : print("sticks"); break;
case 7 : print("heaven"); break;
case 8 : print ("pate"); break;
case 9 : print("spine"); break;
case 10 : print("shin"); break;
}
}
}
答案 4 :(得分:0)
使用if-else
代替switch语句。
if(n==1){
...
}else if(n==2){
...
}
答案 5 :(得分:0)
你不能这样做:
println("He played knick-knack on my" + switch(n));
如果您希望代码大致相同,则应委托执行切换的方法。
替代解决方案将包括if
/ else if
(有点冗长)的序列,或者可能是充当查找表的数组。
e.g。
// note arrays are indexed from 0
String[] arrayOfObjects = new String[]{"thumb", ....};
println("He played knick-knack on my" + arrayOfObjects(n-1));
我认为,除非你有非连续的索引(不太可能),否则Map
在这种情况下可能有点过分
答案 6 :(得分:0)
您正在使用switch
方法。做以下更改,它将起作用:
import acm.program.*;
public class SingSong extends ConsoleProgram
{
public void run()
{
for ( int n = 1; n <= 10; n++)
{
println("This old man, he played " + n);
println("He played knick-knack on my" + getValue(n));
println("With a knick-knack, paddy-whack,");
println("Give your dog a bone");
println("This old man came roiling home");
}
}
private String getValue(int n)
{
switch (n)
{
case 1 : return "thumb"; break;
case 2 : return "shoe"; break;
case 3 : return "knee"; break;
case 4 : return "door"; break;
case 5 : return "hive"; break;
case 6 : return "sticks"; break;
case 7 : return "heaven"; break;
case 8 : return "pate"; break;
case 9 : return "spine"; break;
case 10 : return "shin"; break;
default : return "VALUE NOT FOUND!!";
}
}
}
答案 7 :(得分:-1)
for循环中的switch / case语句很好,问题是switch不是函数调用。尝试这样的事情:
string obj;
switch (n){
case 1 : obj="thumb"; break;
case 2 : obj="show"; break;
//Etc
}
}
println("This old man, he played " + n);
println("He played knick-knack on my" + obj);
println("With a knick-knack, paddy-whack,");
println("Give your dog a bone");
println("This old man came roiling home");