简体   繁体   中英

How do I take an input from my scanner from one class and put it in another class

I'm a highschooler who's pretty new to Java. I'm struggling a bit with object oriented programming. I'm not sure how to take an input from a scanner and implement the input into my code. I'm sorry if my question is bad, I'm just not sure how to approach this problem because the textbook I'm using is too complicated.

This is my main file:

import java.io.*;
import java.util.*;

//for best results play in code screen
public class Main {
  public static void main(String[] args) {
    // calls on the gameStructure file automaticlly so the player wont have to do it
    // manually
    new GameStructure();

    Scanner reader = new Scanner(System.in);
    System.out.println("How long do you want your snake to be? For best results make it less then 7 :)?");
    public int rad = reader.nextInt();
  }
}

This is my second file called GamePanel.java

int bodyParts = parts.rad;

This is the line in which I try to call on the input of the scanner This is the error I'm getting:

    int bodyParts = parts.rad;
                       ^
  symbol:   variable rad
  location: variable parts of type Main
1 error
exit status 1
 ^C
 

您需要将其作为参数传递给另一个类中的方法,或者将其作为静态类属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM