困在java中的输出错误

时间:2017-04-12 20:32:45

标签: java

我的java程序中出现输出错误。 我的程序是一个简单的猜谜游戏,它从用户那里获取一个数字,也可以创建一个随机数。然后它找到这些数字之间的差异并将其显示给用户。 我是初学者,所以我很感激简单的指导。谢谢。 这是我的代码:

import javax.swing.JOptionPane;
public class RandomGuessMatch
{
   public static void main(String[] args)
   {
      int random_Number, user_Number, difference;
      String random;

      random_Number = 1 + (int)(Math.random() * 100);

      user_Number = JOptionPane.showInputDialog(null,
         "Please guess a number between 1 to 5." + "Please input your number",
         "Guessing Game", JOptionPane.INFORMATION_MESSAGE);

      difference = JOptionPane.showMessageDialog(null,
         "The difference between my number and your number is " + user_Number - random_Number,
         "Guessing Game", JOptionPane.INFORMATION_MESSAGE);

      boolean valid = random_Number == user_Number;
      JOptionPane.showMessageDialog(null,
         "Does your number and my number match?" + "\n" + valid,
         "Guessing Game", JOptionPane.INFORMATION_MESSAGE);
   }
}

这是我得到的错误(我使用jGrasp):

RandomGuessMatch.java:13: error: incompatible types: String cannot be converted to int
      user_Number = JOptionPane.showInputDialog(null,

RandomGuessMatch.java:18: error: bad operand types for binary operator '-'
         "The difference between my number and your number is " + user_Number - random_Number,

6 个答案:

答案 0 :(得分:1)

错误1:

INFORMATION_DIALOG类中没有像JOptionPane这样的参数,你也试图将商店JOptionPane结果存储到Integer类型中,但它应该是String类型。

错误2:

因为你已经连接了String所以你需要减去这样的整数(user_Number - random_Number)

答案 1 :(得分:1)

<强>第一

JOptionPane.showInputDialog(...);

以正确的方式返回String而不是int

String s = JOptionPane.showInputDialog(null, "Please guess a number between 1 to 5." 
                  + "Please input your number",
                    "Guessing Game", JOptionPane.INFORMATION_MESSAGE);

第二

您必须初始化user_Number并将其与String连接,您必须将结果放在(user_Number - random_Number)

之间

JOptionPane.showMessageDialog返回void,因此您无法将其分配给任何变量:

user_Number = 0;
JOptionPane.showMessageDialog(null, "The difference between my number and your number is " 
                + (user_Number - random_Number),
                "Guessing Game", JOptionPane.INFORMATION_MESSAGE);

答案 2 :(得分:1)

您可以尝试如下所示。 这段代码正在运行。

import javax.swing.JOptionPane;
public class RandomGuessMatch {
public static void main(String[] args) {
    int random_Number, difference;
    String user_Number;
    String random;

    random_Number = 1 + (int) (Math.random() * 100);
    System.out.println(random_Number);

    user_Number = JOptionPane.showInputDialog(null,
            "Please guess a number between 1 to 5." + "Please input your number", "Guessing Game",
            JOptionPane.INFORMATION_MESSAGE);

    JOptionPane.showMessageDialog(null,
            "The difference between my number and your number is "
                    + (Integer.parseInt(user_Number) - random_Number),
            "Guessing Game", JOptionPane.INFORMATION_MESSAGE);

    boolean valid = (random_Number == Integer.parseInt(user_Number));
    JOptionPane.showMessageDialog(null, "Does your number and my number match?" + "\n" + valid, "Guessing Game",
            JOptionPane.INFORMATION_MESSAGE);
}
}

答案 3 :(得分:0)

对于第二个错误,您需要使用如下

&#34;我的号码和号码之间的差异是&#34; +(user_Number - random_Number)

然后(user_Number - random_Number)将是字符串横向。否则它将尝试在字符串上使用-,这是不允许的。

对于第一个问题,请检查doc。那里没有INFORMATION_DIALOG

答案 4 :(得分:0)

第一个错误表明INFORMATION_DIALOG不是JOptionPane的成员。这可能是因为您导入了错误的 JOptionPane,或者可能存在拼写错误(或者那里确实没有INFORMATION_DIALOG

第二个错误是组合字符串连接(使用+符号和算术运算-的结果。由于操作顺序,首先评估+,转换将user_Number的值放入一个与"The difference between my number and your number is "连接的字符串中,然后将其解释为算术操作中的操作数,这是您的代码失败的地方。要解决此问题,只需将减法放在括号中:

"The difference between my number and your number is " + (user_Number - random_Number),

答案 5 :(得分:0)

这里可以轻松解决您的问题。 (这将取代你的差异=代码行)

int user_Number_As_Int = Integer.parseInt(user_Number);

  JOptionPane.showMessageDialog(null,
     "The difference between my number and your number is " + (random_Number - user_Number_As_Int),
     "Guessing Game", JOptionPane.INFORMATION_MESSAGE);

请参阅,您的代码的问题是用户输入的数字将保存为字符串,显然无法从int中添加或减去。此外,showMethodDialouge方法返回void,因此您无法设置与其相等的差异。这段代码的作用是改变用户输入int的数量,然后进行减法。同样,您需要更换您的差异= JOptionPane.showMessageDialouge行。

希望这有帮助。