J
J
Jourloy2018-07-06 19:29:27
Java
Jourloy, 2018-07-06 19:29:27

[Outdated] What is wrong in my java code?

public class Main {
After these lines, I start setting variables that are used in various methods (so as not to constantly create the same variable).
There I created a variable in , which is responsible for entering data from the console. And also: Subsequently, in order to consider the answer (for example, Yes), I will write like this: And now imagine this picture:
static Scanner in = new Scanner(System.in);
static String answer = "";
answer = in.nextLine();

public static int Name1() {
    System.out.println("Yes or No?");
    answer = in.nextLine(); //Я ввожу Yes
    //Там дальше код, который возвращает переменную (не answer)
}
public static int Name2() {
    int whileStop = 0; 
    while (whileStop = 0) {
        System.out.println("Da or Net?");
        answer = in.nextLine();
        if (answer.equals("Da") {
            // Что-то написано
            whileStop = 1;
        } else if (answer.equals("Net") {
            // И тут что-то написано
            whileStop = 1;
        } else {
            System.out.println ("ERROR");
        }
    }
    //Там дальше код, который возвращает переменную (не answer)
}
public static void main(String[] args) {
    Name1();
    Name2();
}

In console I get this:
Yes or No?
Yes
Da or Net?
Da or Net?

According to the idea, after I enter "Yes", I should be asked "Yes or Net", and then wait for my answer, but for some reason the program from the first method saves the answer variable and transfers it to the second method, skipping line:
answer = in.nextLine();
You will say "Why did you get this?", and I will answer you that I decided to check and slightly changed my code:
public static int Name1() {
    System.out.println("Yes or No?");
    answer = in.nextLine(); //Я ввожу Yes
    //Там дальше код, который возвращает переменную (не answer)
}
public static int Name2() {
    int whileStop = 0; 
    while (whileStop = 0) {
        System.out.println("-- \n" + answer + "-- \nDa or Net?");
        answer = in.nextLine();
        if (answer.equals("Da") {
            // Что-то написано
            whileStop = 1;
        } else if (answer.equals("Net") {
            // И тут что-то написано
            whileStop = 1;
        } else {
            System.out.println ("ERROR");
        }
    }
    //Там дальше код, который возвращает переменную (не answer)
}
public static void main(String[] args) {
    Name1();
    Name2();
}

And so in the console it turns out like this (after the changes):
Yes or No?
Yes
--
Yes
--
Da or Net?
Da or Net?

What could be the reason for this, because a variable cannot be so easily moved from one method to another without return and parameters.
UPD: We figured out the static variable, the next question is: why is the line skipped?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2018-07-06
@Jourloy

after all, a variable cannot so easily get over from one method to another without return and parameters.

Your variable is static , it is one for the entire class. You yourself created it "so as not to constantly create the same variable"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question