2016-11-15 434 views
-2

所以在第四种方法我试图确定赢家,但它说int不能转换为java.lang.String 是的,我必须使用不同的方法,如果你发现别的错误的代码让我知道,谢谢你不兼容的类型:int不能转换为java.lang.String

import java.util.Random;// importing the random class to generate random number 
import javax.swing.JOptionPane;//importing the JOptionPane to use the pop up windows and menu 
public class TiriBark 
{ 
    private static int ComputerWin =0; // 0 is for loss and 1 for win 
    private static int UserWins =0; // 0 is for loss and 1 for win 
    private static int tie=0; // if it's a tie the value will be 1 
    private static int Comp; // holds the value of the random number generated by the computer 
    private static int user; // holds users choice of rock papper or scissor 
    public static void main(String[] args) // main method 
    { 
    Computer(); 
    user(); 
    } 
    /**this method generated a random number between 1 and 3 
    *@return Comp 
    */ 
    public static int Computer() 
    { 

    Random rand = new Random(); 
    int Comp = rand.nextInt(3)+1; 
    return Comp;} 
    /**this method asked the user to enter his choice of rock paper or scissor 
    *@return user 
    */ 
    public static int user(){ 
    String User = JOptionPane.showInputDialog("Enter 1 for rock 2 for paper and 3 for scissor "); 
    int user = Integer.parseInt(User); 
    return user; } 
    /** this method calculates and determines the winner and if possible a tie 
    *@return ComputerWin 
    *@return UserWins 
    *@return tie 
    */ 
    public static String resutls() { 
    if (user == Comp) { 
     tie =1; } 
    if (user==1 && Comp == 2){ 
     ComputerWin=1; } 
    if (user ==1 && Comp ==3){ 
     UserWins=1;} 
    if (user ==2 && Comp ==1){ 
     UserWins=1;} 
    if (user ==2 && Comp == 3){ 
     ComputerWin=1;} 

    if (user ==3 && Comp ==1) { 
     ComputerWin=1; } 

    if (user ==3 && Comp ==2){ 
     UserWins=1;} 
    return UserWins; 
    return ComputerWin; 
    return tie; 

    } 
} 
+1

从'resutls()'返回的所有值都是'int'值。我建议使方法的返回类型为'int' –

+0

你想返回3个值吗? – bradimus

+0

是的bradimus,但我猜这是不可能的 –

回答

0

写这篇:

return UserWins; 
return ComputerWin; 
return tie; 

你生成无用代码,因为函数结束于第一返回。 如果你想在INT可变投进去字符串只是做:

return UserWins + ""; 
+0

我应该将这种方法分成3种其他方法吗? –

+0

'返回Integer.toString(UserWins);'更清楚地表达意图 – bradimus

+0

您正在修改三个静态变量 – Dante

0

UserWins确实是一个整数。要将整数转换为字符串,您可以:

String result = "" + UserWins; 
return result; 

这会自动将您的整数转换为字符串。还有很多其他方法可以执行相同的转换。

+1

'return Integer.toString(UserWins);'更清楚地表达意图。 – bradimus

0

您的代码存在的问题之一是在所讨论的方法结尾处的三个返回语句。

public static String resutls() { 
    if (user == Comp) { 
     tie =1; } 
    if (user==1 && Comp == 2){ 
     ComputerWin=1; } 
    if (user ==1 && Comp ==3){ 
     UserWins=1;} 
    if (user ==2 && Comp ==1){ 
     UserWins=1;} 
    if (user ==2 && Comp == 3){ 
     ComputerWin=1;} 

    if (user ==3 && Comp ==1) { 
     ComputerWin=1; } 

    if (user ==3 && Comp ==2){ 
     UserWins=1;} 
    return UserWins; 
    return ComputerWin; 
    return tie; 

有了它,这里所写的方式,程序将返回UserWins不管是什么,只是因为它会检查谁赢了之后的下一个代码。你想要做的就是在你的陈述中放入返回陈述。那么这将做什么,当这个方法被调用时,它将检查if语句和哪一个是正确的,相应的return语句将被执行。尝试这样的:

if(user == Comp) 
{ 
    return tie; 
} 
else if(user == 1 && Comp == 2) 
{ 
    return ComputerWin; 
} 

对于你的字符串问题;我的第一个问题是你需要的方法返回一个字符串?这里的问题是方法声明是:public static String results()。这必须返回一个字符串,这意味着你的return语句必须是一个String类型。将其更改为int类型应该可以解决您的问题。

相关问题