当前位置: 技术问答>java相关
请教高手,这个错误如何解决??
来源: 互联网 发布时间:2015-08-03
本文导语: 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")){ ...
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);
}
}
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);
}
}
|
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没有初始化。
}
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没有初始化。
}
|
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没有初始化。
}