不兼容的类型:int无法转换为java.lang.String

时间:2016-11-15 16:09:14

标签: java string methods integer java.lang

所以在第四种方法中,我试图确定胜利者,但它说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;

  }
}

3 个答案:

答案 0 :(得分:0)

写这个:

return UserWins;
return ComputerWin;
return tie;

您正在生成死代码,因为该函数在第一次返回时结束。 如果您想将 int 变量强制转换为字符串,请执行以下操作:

return UserWins + "";

答案 1 :(得分:0)

UserWins确实是一个整数。要将整数转换为String,您可以执行以下操作:

String result = "" + UserWins;
return result;

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

答案 2 :(得分:0)

您的代码的一个问题是相关方法末尾的三个return语句。

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,无论是什么,因为在检查谁获胜后它将成为下一个代码。你要做的是将return语句放在你的if语句中。那么这将做的是当调用该方法时,它将检查if语句以及哪一个是正确的,将执行相应的return语句。尝试这样的事情:

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

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

相关问题