1

How can we take n input in java according to value of T

Like if i take t=2,then we take two n input

If value of t=3,then we take three n input

import java.util.Scanner;

public class Main {
     public static void main(String[] args){
         Scanner s= new Scanner(System.in);
         int t=s.nextInt();

         int n=s.nextInt();
         int n=s.nextInt();

         int evensum=0;

         for (int i=1; i<=n; i++) {
             if(i%2==0) {
                 evensum=evensum+i;
             }
             System.out.print(evensum);
         }
    }

As in this code i take t=2 and I take two n inputs. If I take t=3, then what how could I take that third input?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Krishan
  • 11
  • 1
  • 3

1 Answers1

0

You can't have multiple variable with the same name, for your purpose use a loop :

Scanner s = new Scanner(System.in);
System.out.println("How many values ?");
int nbInput = s.nextInt();
for (int i = 1; i <= nbInput; i++) {
    int input = s.nextInt();
    System.out.print(input);
}
azro
  • 53,056
  • 7
  • 34
  • 70