“找不到符号错误”在我的三角类 [英] "Cannot find symbol error" in my Triangle class

查看:364
本文介绍了“找不到符号错误”在我的三角类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么我会得到num和三角形类型变量的无法找到符号错误。

I can't figure out why I'm getting the cannot find symbol errors for the num and triangle type variables.

这是类:

public class Triangle
{
    private int s1;
    private int s2;
    private int s3;

    public Triangle (int s1, int s2, int s3)
{
        s1= num1;
        s2= num2;
        s3= num3;
}

    public String toString()
    {
    return (s1 + " " + s2 + " " + s3);
    }

    public boolean is_equilateral(){
if(s1==s2 && s2==s3 && s3==s1)
    {
    return Triangle.is_equilateral;
    }
else
{
    return false;
}
}

    public boolean is_isosceles(){
if((s1==s2)||(s2==s3)||(s3==s1))
{
    return Triangle.is_isosceles;
}
else
{
    return false;
}
}

    public boolean is_scalene(){
if(s1!=s2 && s2!=s3 && s3!=s1)
{
    return Triangle.is_scalene;
}
else
{
    return false;
}
}


    }

这是程序:

import java.util.Scanner;
public class Assignment5 {

   //===========================================================
   // Create and determine properties of various triangles.
   //===========================================================
   public static void main (String[] args) {

      Scanner console = new Scanner(System.in);
      int num1;
      int num2;
      int num3;
      String another;
      do
      {
         System.out.println("Enter the sides of the triangle: ");
         num1 = console.nextInt();
         num2 = console.nextInt();
         num3 = console.nextInt();

         Triangle myTriangle = new Triangle (num1, num2, num3);


        System.out.println(myTriangle.toString() + " triangle:");

        //check the isosceles
        if (myTriangle.is_isosceles())
           System.out.println("\tIt is isosceles");
        else
           System.out.println("\tIt is not isosceles");

        //check the equilateral
        if (myTriangle.is_equilateral())
           System.out.println("\tIt is equilateral");
        else
           System.out.println("\tIt is not a equilateral");

        //check the scalene
        if (myTriangle.is_scalene())
           System.out.println("\tIt is scalene");
        else
           System.out.println("\tIt is not scalene");


        System.out.println();
        System.out.print("Check another Triangle (y/n)? ");
        another = console.next();

    } while (another.equalsIgnoreCase("y"));


   }  // method main

}  // class Assignment5

我对Java很新,所以很抱歉,如果这是一个明显的问题。

I'm fairly new to Java so sorry if this is an obvious problem.

推荐答案

private int s1; // these belong to the class
private int s2;
private int s3;

public Triangle (int s1, int s2, int s3) // these belong to the constructor
{
        s1= num1; // num1 isn't declared anywhere
        s2= num2;
        s3= num3;
}

您的类变量和构造函数的参数都命名为 s1 - s3

Both your class variables and the arguments to your constructor are named s1 - s3.

您需要将参数更改为 num1 - num3 ,或更改赋值语句以使用 this 关键字来引用类变量。这是第一种方法:

You need to either change the arguments to num1 - num3, or change the assignment statements to use the this keyword to refer to the class variables. Here's the first method:

private int s1; // these belong to the class
private int s2;
private int s3;

public Triangle (int num1, int num2, int num3) // these belong to the constructor
{
        s1 = num1;
        s2 = num2;
        s3 = num3;
}

为您的课程提供构造函数

这篇关于“找不到符号错误”在我的三角类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆