E
E
enabl32021-10-02 17:35:44
Java
enabl3, 2021-10-02 17:35:44

Why doesn't java code a^b work?

Hello.
Please tell me what am I doing wrong?
There is a task, write a method that takes 2 integers greater than zero and must return an integer in which the first number is raised to the power of the second.
I do it like this

import java.util.Scanner;

public class testing {
    public static void main(String[] args) {
        System.out.print("Введи число (а): ");
        Scanner get_input = new Scanner(System.in);
        int a = 0;
        while (!get_input.hasNextInt() || ((get_input.nextInt()) <= 0))  {
            System.out.print("Введи число больше 0: ");
            get_input.next();
        }
        a = get_input.nextInt();
        System.out.print("Введи число (b): ");
        int b = 0;
        while (!get_input.hasNextInt()) {
            System.out.print("Введи число больше 0: ");
            get_input.next();
        }
        b = get_input.nextInt();
        int c = getDegree(a, b);
        System.out.println("'A' в степени 'B' = " + c);
    }

    public static int getDegree(int a, int b) {
        int res = (int) Math.pow(a, b);
        return res;
    }
}


But the problem is that when entering a number, if it is greater than 0 and not text, it does not immediately ask for the number B, what did I do wrong?
61586e39b26b7921477258.jpeg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question