I want to enter a number and it’s multiplied from 1 to 10

Asked

Viewed 197 times

0

package exercicio5;
import java.util.*;

public class Exercicio5 {
    public static void main(String[] args) {
        int i,n1,x;

        Scanner ler;
        ler = new Scanner(System.in);
        for(i=1;i<=10;i++){
            x=n1*i;
            System.out.println("Digite o 1°número" +n1);
            n1 = ler.nextInt();
            System.out.println("Multiplicação de 1 até 10" +x);
        }
    }  
}

Is making a mistake in: x=n1*i;

  • 1

    And what it should be n1 in the first iteration? You only read its value after this line...

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

The error is that not initializing the variable to use it, initiating would solve the problem. But this code can be simplified and the variable is not necessary, even there was a logic error making account before the moment you can do and printing wrong information about which number you are typing. Organizing better and using a pattern closer to what people do:

package exercicio5;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner ler = new Scanner(System.in);
        for (int i = 0; i < 10; i++) {
            System.out.println("Digite o 1°numero" + i);
            int n = ler.nextInt();
            System.out.println("Multiplicação de 1 até 10" + n * i);
        }
    }  
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • package exercicio5; import java.util.; public class Exercicio5 { public Static void main(String[] args) { int i,n = 0,x; Scanner read; read = new Scanner(System.in); System.out.println("Enter number" +n); n = read.nextInt(); for(i=1;i<=10;i++){ x=ni; System.out.println("Multiplication from 1 to 10 " +x); } } } 1,n2,n*3 etc...

  • is still wrong and disorganized

Browser other questions tagged

You are not signed in. Login or sign up in order to post.