【问题标题】:Unexpected Type while compiling编译时出现意外类型
【发布时间】:2016-02-05 09:38:18
【问题描述】:

我用 JAVA 编写了以下代码:

import java.util.Scanner;

public class Equations
    {
        public static void main (String [] args)
        {
            Scanner scan = new Scanner (System.in);
            System.out.println ("This program solves a system of 2 linear  equations" +"\n"+
        "Enter the coefficients a11 a12 a21 a22 b1 b2:");

            int a11 = scan.nextInt();
            int a12 = scan.nextInt();
            int a21 = scan.nextInt();
            int a22 = scan.nextInt();
            int b1 = scan.nextInt();
            int b2 = scan.nextInt();

            System.out.println ("Eq: " + a11 + "*x1" + "+" + a12 + "*x2 = " + b1);
            System.out.println ("Eq: " + a21 + "*x1" + "+" + a22 + "*x2 = " + b2);

            if(((a11*a22)-(a12*a21)) != 0){
                double Equ1single = ((b1*a22)-(b2*a12))/((a11*a22)-(a12*a21));
                double Equ2single = ((b2*a11)-(b1*a21))/((a11*a22)-(a12*a21));
                System.out.println ("Single solution: (" + Equ1single + "," + Equ2single + ")");
            } 

            if(((b2*a11)-(b1*a21)) = 0){
                System.out.println ("Many solutions");
            }

       }
}

在 BlueJay 环境中编译此代码时出现错误。 错误如下:

意外的类型。必需的变量;找到的值。

它将“-(b1*a21)”标记为问题。但我确实将 a21 声明为 int。而且,之前的“if”条件是“相同的”并且不会给出任何错误。

编译这个有什么问题?

【问题讨论】:

  • 尝试使用 2 = 例如if(((b2*a11)-(b1*a21)) == 0){
  • = 是赋值,== 用于比较。
  • 你应该使用 == 而不是 =。 == 是相等性测试,= 将右侧 sie 中表达式的结果分配给左侧的变量。

标签: java types compilation int


【解决方案1】:
if(((b2*a11)-(b1*a21)) = 0){

应该是

if(((b2*a11)-(b1*a21)) == 0){

【讨论】:

  • 请在评论中回答这个问题,然后投票以拼写错误结束。
  • @SotiriosDelimanolis,我不确定我明白为什么?这是一个有效的问题,有一个有效的答案。它可能是重复的,但我认为这不是一个无效的问题?
  • 这是一个错字引起的问题。在我看来,他们打算使用== 似乎很明显。我不希望我们鼓励这些问题,也不想回答它们。国际海事组织。 (我现在没有选票,否则我会投票结束这个问题。)
猜你喜欢
  • 2020-03-24
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-24
  • 2017-08-15
相关资源
最近更新 更多