错误:squareFeet无法解析为变量

时间:2018-10-21 07:25:13

标签: java

我是Java编程的新手,这是我一直坚持的问题。

  public static double Totalwallspace( int NumberofRooms )

  {

  double squarefeet;    
  double totalSquareFeet = 0;

  Scanner scanner = new Scanner(System.in);

  for( int currentRoom = 1; currentRoom <= NumberofRooms; currentRoom++){

  System.out.println("Please enter the square feet of the wall space in room: " + currentRoom);

  double squareFeet = scanner.nextDouble();

  scanner.close();

  totalSquareFeet = totalSquareFeet + sqaureFeet;

   }
   return totalSquareFeet;
}

每次我尝试编译它时,总是不断地告诉我以下消息:squareFeet无法解析为变量。

1 个答案:

答案 0 :(得分:0)

是的,Scanner应该关闭,try with resource是最好的方法:

public static double Totalwallspace(int NumberofRooms) {
    try (Scanner scanner = new Scanner(System.in)) {
        double totalSquareFeet = 0;

        for (int currentRoom = 1; currentRoom <= NumberofRooms; currentRoom++) {
            System.out.println("Please enter the square feet of the wall space in room: " + currentRoom);
            totalSquareFeet += scanner.nextDouble();
        }

        return totalSquareFeet;
    }
}