【问题标题】:The method triangleLengths is undefined for type Triangles对于三角形类型,方法 triangleLengths 未定义
【发布时间】:2017-01-23 14:47:50
【问题描述】:

我是 java 新手,我的任务是编写一个程序,该程序需要 3 个输入,然后将它们作为三角形的边进行评估,以确定它是什么类型的三角形,但是我在标题中遇到了错误。

import java.util.Scanner;
import java.util.ArrayList;

public class Triangles {

public static void main(String[] args) {

    Scanner user_input = new Scanner(System.in);

    ArrayList<Integer> triangleLengths = new ArrayList<Integer>();

    for (int i=0; i < 3; i++) {

        System.out.print("Triangle length #" + i + ": ");
        triangleLengths.add(i,user_input.next());   
    }

    if (triangleLengths(0) == triangleLengths(1) == triangleLengths(2)) {

        System.out.println("This triangle is an equilateral triangle");

    }

我遇到的问题是在标题中出现错误的“if”语句中。非常感谢您的帮助。





【问题讨论】:

    标签: java eclipse object methods types


    【解决方案1】:

    替换行if (triangleLengths(0) == triangleLengths(1) == triangleLengths(2)) {

    if (triangleLengths.get(0) == triangleLengths.get(1) == triangleLengths.get(2)) {
    

    注意您的 if 条件还有另一个问题。请记住 If 接受布尔值 所以修改为

     if (triangleLengths.get(0) == triangleLengths.get(1) && triangleLengths.get(1) == triangleLengths.get(2)) {
    

    【讨论】:

    • 非常感谢!现在可以了,感谢您的帮助:)
    • @JakeB 接受答案是 stackoverflow 表达谢谢的方式
    猜你喜欢
    • 2015-08-21
    • 2013-11-11
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多