0

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

  }
}
4

3 回答 3

0

写这个:

return UserWins;
return ComputerWin;
return tie;

您正在生成死代码,因为函数在第一次返回时结束。如果您想将int变量转换为String,只需执行以下操作:

return UserWins + "";
于 2016-11-15T16:13:32.860 回答
0

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

String result = "" + UserWins;
return result;

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

于 2016-11-15T16:15:35.177 回答
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;
}

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

于 2016-11-15T18:58:37.250 回答