Java:“错误:无法找到符号” [英] Java: "error: cannot find symbol"

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

问题描述

(Rookie的错误,我很确定。)

(Rookie mistake, I'm sure.)

我是一名计算机科学专业的第一年学生,并试图为一项作业编写一个程序,码;

I'm a first year computer science student, and attempting to write a program for an assignment, with the code;

import java.util.Scanner;
public class Lab10Ex1 {

   public static void main(String[] arg) {

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please type a number: ");
    int n = keyboard.nextInt(); 
    calcNumFactors();
  }
  public static void calcNumFactors(){

   System.out.print(n + 1);

  }

}

但编译时,我收到错误;

But upon compiling, I get the error;


Lab10Ex1.java:10:错误:找不到符号System.out.print(n +
1);
^

Lab10Ex1.java:10: error: cannot find symbol System.out.print(n + 1); ^

symbol:variable n

symbol: variable n

location:class Lab10Ex1

location: class Lab10Ex1

如果有人能向我解释我做错了什么,或者如何解决,我会非常感激。

If someone could explain to me what I've done wrong, or how to fix it, I would greatly appreciate it.

推荐答案

n 变量是在 main 中声明的方法因此只在main方法中可见,在其他任何地方都没有,当然也不在 calcNumFactors 方法中。要解决此问题,请为 calcNumFactors 方法提供一个 int 参数,该参数允许调用方法传递 int ,例如 n 进入方法。

The n variable was declared in the main method and thus is visible only in the main method, nowhere else, and certainly not inside of the calcNumFactors method. To solve this, give your calcNumFactors method an int parameter which would allow calling methods to pass an int, such as n into the method.

public static void calcNumFactors(int number) {
   // work with number in here
}

并像这样调用:

int n = keyboard.nextInt(); 
calcNumFactors(n);

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

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