-1

在为我的学校科目编码时,我必须用 Doubles 填充一个数组列表,直到我收到“in”消息,在这种情况下,我可以停止并扫描其他两个值,这样我就可以开始计算了。因为显示整个代码无关紧要,我将只显示我们遇到问题的类:

    import java.util.Scanner;

class Test{
  double x1, x2;
  Scanner sc;
  ArrayList<Double> coefficients;

  void help() {    
    sc = new Scanner(System.in);
    coeffiecients = new ArrayList<Double>();

      while(!sc.next().equals("in")){
        coefficients.add(coefficient);
        coefficient = sc.nextDouble();
      }

      x1 = sc.nextDouble();
      x2 = sc.nextDouble();
    }

  public static void main(String[] args){
    new Test().help();
  }
}

因此,使用此代码,我收到错误消息:cannot find symbol

符号:变量 nextDouble

位置:java.util.Scanner 类型的变量 sc

我只是不明白为什么这段代码会出错。有人能帮我吗?

[[添加了我的代码,仍然出现错误,ArrayList<Double> coefficients; 为什么错了?]]

正确的代码在这里! 变化都在答案 1 中描述

import java.util.*;

class Test{
  double x1, x2;
  Scanner sc;
  ArrayList<Double> coefficients;
  double coefficient;

  void help() {    
    sc = new Scanner(System.in);
    coefficients = new ArrayList<Double>();

      while(!sc.next().equals("in")){
        coefficients.add(coefficient);
        coefficient = sc.nextDouble();
      }

      x1 = sc.nextDouble();
      x2 = sc.nextDouble();
    }

  public static void main(String[] args){
    new Test().help();
  }
}
4

1 回答 1

1

nextDouble是一种Scanner方法,而不是一个领域。您需要调用该方法

coefficient = sc.nextDouble();
x1 = sc.nextDouble();
x2 = sc.nextDouble();
于 2013-09-22T14:58:22.583 回答