在这个java程序中找不到符号消息

时间:2015-12-11 13:53:17

标签: java compiler-errors

我不知道为什么我会收到两个错误,我认为我已经接近将程序放在我希望的位置。有人可以告诉我需要做些什么才能让这些错误消失。其他问题并非针对我的问题,也没有帮助。

    C:\Users\Steven\Documents\Java Work\Assignment2Tourist.java:22: error: cannot find symbol
        month1[i] = input.nextInt();
                    ^
  symbol:   variable input
  location: class Assignment2Tourist

    C:\Users\Steven\Documents\Java Work\Assignment2Tourist.java:28: error: cannot find symbol
            month2[i] = input.nextInt();
                        ^
      symbol:   variable input
      location: class Assignment2Tourist
    2 errors

Process completed.

Assignment2Tourist:

import java.util.Scanner;

public class Assignment2Tourist {

    public static void main (String [] arguments)
    {

    int[] month1 = new int[11];
    int[] month2 = new int[11];

    for (int i = 0; i < month1.length; i++)
    {
        System.out.println("Please enter the amount of vistors 2013-");
        month1[i] = input.nextInt();
    }

    for (int i = 0; i < month2.length; i++)
    {
        System.out.println("Please enter the amount of vistors 2014");
        month2[i] = input.nextInt();
    }

        System.out.print("The average visitors for 2013 - ");
        avVisitors(month1);

        System.out.print("The average visitors for 2014 - ");
        avVisitors(month2);

        System.out.print("The most Visitors in 2013 were - ");
        highVisitors(month1);

        System.out.print("The most Visitors in 2014 were - ");
        highVisitors(month2);

        monthAverage(month2, month1);

    }

2 个答案:

答案 0 :(得分:1)

input未声明。 把它放在for loop

之前
Scanner input = new Scanner(System.in);
for (int i = 0; i < month1.length; i++)
{
    System.out.println("Please enter the amount of vistors 2013-");
    month1[i] = input.nextInt();
}
for (int i = 0; i < month2.length; i++)
{
    System.out.println("Please enter the amount of vistors 2014");
    month2[i] = input.nextInt();
}
input.close();

并且不要忘记用Scanner关闭close()

答案 1 :(得分:0)

您缺少扫描仪对象作为输入,您尚未定义输入变量。

public static void main (String [] arguments)
{
String s = "Number of visitor in 2013 was 123450";

int[] month1 = new int[11];

int[] month2 = new int[11];
Scanner input = new Scanner(s);
for (int i = 0; i < month1.length; i++)
{
    System.out.println("Please enter the amount of vistors 2013-");
    month1[i] = input.nextInt();
}

for (int i = 0; i < month2.length; i++)
{
    System.out.println("Please enter the amount of vistors 2014");
    month2[i] = input.nextInt();
}

    System.out.print("The average visitors for 2013 - ");
    avVisitors(month1);

    System.out.print("The average visitors for 2014 - ");
    avVisitors(month2);

    System.out.print("The most Visitors in 2013 were - ");
    highVisitors(month1);

    System.out.print("The most Visitors in 2014 were - ");
    highVisitors(month2);

    monthAverage(month2, month1);

}