请教高手,这个错误如何解决??

dqbaiying 2002-08-13 01:58:27
import corejava.*;
public class Guestgame
{
public static void judge(String person,String computer){

if (person == computer){
System.out.println("All Win,You are same.");
}
else if ((person=="stone")&&(computer=="shears")){
System.out.println("You Win!!");
}
else if((person=="cloth")&&(computer=="stone")){
System.out.println("You Win!!");
}
else if((person=="shears")&&(computer=="cloth")){
System.out.println("You Win!!");
}
else System.out.println("Sorry,you lost!!");
}

public static String toString(int i){
String str;
switch(i){
case 1: str="stone";break;
case 2: str="cloth";break;
case 3: str="shears";break;
}
return str;//编译时提示本行出错,好像是说str没有初始化。
}

public static void main(String args[])
{
String Person;
String Computer;
int p = Console.readInt("Please input your choose(1:stone 2:cloth 3:shears):");
int c = new RandomIntGenerator(1,3).draw();
Person = toString(p);
Computer = toString(c);
judge(Person,Computer);
}
}

...全文
55 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
snap_bug 2002-08-13
  • 打赏
  • 举报
回复

public static String toString(int i){
String str;
switch(i){
case 1: str="stone";break;
case 2: str="cloth";break;
case 3: str="shears";break;
default: str=""; //加上这一句没有问题了
}
return str;//编译时提示本行出错,好像是说str没有初始化。
}
ggyy 2002-08-13
  • 打赏
  • 举报
回复
public static String toString(int i){
String str="you not initial";
switch(i){
case 1: str="stone";break;
case 2: str="cloth";break;
case 3: str="shears";break;

}
return str;//编译时提示本行出错,好像是说str没有初始化。
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧