简体   繁体   中英

Is it possible to use the Scanner outside the main method and still call it in?

So what im trying to do is to use my scanner method to go save the inputs that the users put in thats inside the for loop, but how do i call the certain type of input they place in? What i mean by this is, lets say the user places is in an input of 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 as the input for the question method and answer method. In the main method, or any method, how can i call in the input of 10, or 12.

package com.ez.ez;
import java.util.Scanner;
public class ReWrittingBetterCode{
        public static void Questions(){
        for (int i = 1; i <= 10; i++) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a question "+i+"/10");
        String firstQuestion = scanner.nextLine();
        }
    }
    public static void Answer() {
        for (int i = 1; i <= 10; i++) {
        Scanner scanner = new Scanner(System.in); 
        System.out.println("Enter a answer "+i+"/10");
        String answerAnswer = scanner.nextLine();
        }

    }

    public static void main (String[] args) {
        Questions();
        Answer();

    } 
}

You need to allocate an array to hold the questions. Then when you prompt for the answers you can repeat the questions. To be consistent with your loops, you should have single variable called noq or something for number of questions, and use that everywhere when you are using for loops.

static int noq = 10;
static String[] questions = new String[noq];
static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    Questions();
    Answer();

}

public static void Questions() {
    for (int i = 1; i <= noq; i++) {
        System.out.println("Enter a question " + i + "/" + noq);
        String firstQuestion = scanner.nextLine();
        questions[i - 1] = firstQuestion;
    }
}

public static void Answer() {
    for (int i = 1; i <= noq; i++) {
        System.out.println(questions[i - 1]);
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a answer " + i + "/" + noq);
        String answerAnswer = scanner.nextLine();
    }
}

You should also get away from using static everywhere. I had to do it to simplify the example. Check out the Java Tutorials for more information on programming in Java focusing on arrays and the uses of the static keyword.

So what im trying to do is to use my scanner method to go save the inputs that the users put in thats inside the for loop, but how do i call the certain type of input they place in? What i mean by this is, lets say the user places is in an input of 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 as the input for the question method and answer method. In the main method, or any method, how can i call in the input of 10, or 12.

package com.ez.ez;
import java.util.Scanner;
public class ReWrittingBetterCode{
        public static void Questions(){
        for (int i = 1; i <= 10; i++) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a question "+i+"/10");
        String firstQuestion = scanner.nextLine();
        }
    }
    public static void Answer() {
        for (int i = 1; i <= 10; i++) {
        Scanner scanner = new Scanner(System.in); 
        System.out.println("Enter a answer "+i+"/10");
        String answerAnswer = scanner.nextLine();
        }

    }

    public static void main (String[] args) {
        Questions();
        Answer();

    } 
}

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