简体   繁体   中英

Scanner input storing and using

I've been trying out things on my own now that my second java course is done. It's been difficult going back to the beginning and remembering how to work with everything I've learned over the past months, so I'm trying to make a program that asks the user what shape they want to draw (based on basic shapes made with for loops, which was basically the first thing I've learned in programming), and the size of the shape as an int.

I have the scanner set up, but I can't remember how/where the size variable must be in order to be able to use it within my "draw" methods. Basically, I've tried different things, but "size" is always out of reach. Here is my code so far (I've excluded the code that actually draws the shapes, but they all involve for loops with int size as a variable):

public class Practice {


 public static void main(String[] args) {

     Scanner input = new Scanner(System.in);

     System.out.println("Choose a shape!");

     String shape = input.nextLine();

     System.out.println("Choose a size!");

     int size = input.nextInt();

 }


     public static void drawCirlce() {


        //Code to draw a circle of size given input into scanner.  
     }

     public static void drawSquare() {


         //Code to draw a square of size given input into scanner.
     }

     public static void drawTriangle() {


        //Code to draw a triangle of size given input into scanner.
     }

     public static void drawRocket() {


        //Code to draw a rocket of size given input into scanner.
     }

}

Thanks a lot everyone! I'll keep looking around but any hints are very welcome.

You can pass the size variable to the drawing methods like this:

public class Practice {
 public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     System.out.println("Choose a shape!");
     String shape = input.nextLine();
     System.out.println("Choose a size!");
     int size = input.nextInt();


     // Choose what shape you want to draw
     drawCircle(size);
     // or
     drawSquare(size);
     // or
     drawTriangle(size);
     // etc...
 }


     public static void drawCirlce(int size) {
        //Code to draw a circle of size given input into scanner.  
     }
     public static void drawSquare(int size) {
         //Code to draw a square of size given input into scanner.
     }
     public static void drawTriangle(int size) {
        //Code to draw a triangle of size given input into scanner.
     }
     public static void drawRocket(int size) {
        //Code to draw a rocket of size given input into scanner.
     }

}

You need to declare your variables at the class level. Also, because you're using all static methods, these variables need to declared as static as well

public class Practice {

    private static String shape;
    private static int size;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Choose a shape!");
        shape = input.nextLine();
        System.out.println("Choose a size!");
        size = input.nextInt();
    }

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