如何根据测试用例的值在java中获取n个输入?

如何根据测试用例的值在java中获取n个输入?,java,Java,在java中,如何根据T的值接受n个输入 如果我取t=2,那么我们取两个n输入 如果t=3,则取三个n输入 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();

在java中,如何根据T的值接受n个输入

如果我取t=2,那么我们取两个n输入

如果t=3,则取三个n输入

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);
         }
    }
import java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
int t=s.nextInt();
int n=s.nextInt();
int n=s.nextInt();
int evensum=0;

对于(int i=1;i不能有多个同名变量,请使用循环:

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);
}
Scanner s=新的扫描仪(System.in);
System.out.println(“有多少个值?”);
int nbInput=s.nextInt();

对于(int i=1;i不能有多个同名变量,请使用循环:

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);
}
Scanner s=新的扫描仪(System.in);
System.out.println(“有多少个值?”);
int nbInput=s.nextInt();

对于(int i=1;i每当您为变量分配新值时,新值将替换其中的旧值。换句话说,一个变量中只能有一个值。对于数组,可以为数组变量分配多个值(由其索引标识)但请记住,数组名称本身只能引用单个值

在循环中输入值时,还应记住与
Scanner::next
Scanner::nextInt
等相关的问题。请检查以了解更多信息

考虑到上述各点,处理此类要求的更好方法如下:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        boolean valid;
        int t = 0, n = 0;
        do {
            valid = true;
            System.out.print("Enter the number of integers you want to process: ");
            try {
                t = Integer.parseInt(s.nextLine());
                if (t < 0) {
                    System.out.println(
                            "Error: This is a wrong input. The number should be a positive integer. Try again.");
                    valid = false;
                }
            } catch (NumberFormatException e) {
                System.out.println("Error: This is a wrong input. The number should be a positive integer. Try again.");
                valid = false;
            }
        } while (!valid);

        System.out.println("Thank you. Now enter " + t + " intgers as prompted.");
        int evensum = 0;
        for (int i = 1; i <= t; i++) {
            do {
                valid = true;
                System.out.print("Enter an integer: ");
                try {
                    n = Integer.parseInt(s.nextLine());
                    if (n % 2 == 0) {
                        evensum = evensum + n;
                    }
                } catch (NumberFormatException e) {
                    System.out.println("Error: This is a wrong input. Try again.");
                    valid = false;
                }
            } while (!valid);
        }

        // Display the result of adding all even inputs
        System.out.print("The sum of even integers input by you is " + evensum);
    }
}
另一个示例运行:

Enter the number of integers you want to process: 3
Thank you. Now enter 3 intgers as prompted.
Enter an integer: 10
Enter an integer: 5
Enter an integer: 8
The sum of even integers input by you is 18
Enter the number of integers you want to process: a
Error: This is a wrong input. The number should be a positive integer. Try again.
Enter the number of integers you want to process: 10.5
Error: This is a wrong input. The number should be a positive integer. Try again.
Enter the number of integers you want to process: -20
Error: This is a wrong input. The number should be a positive integer. Try again.
Enter the number of integers you want to process: 3
Thank you. Now enter 3 intgers as prompted.
Enter an integer: 10
Enter an integer: xyz
Error: This is a wrong input. Try again.
Enter an integer: 1.5
Error: This is a wrong input. Try again.
Enter an integer: 5
Enter an integer: 8
The sum of even integers input by you is 18

如果有任何问题/疑问,请随时发表评论。

每当您为变量分配新值时,新值将替换其中的旧值。换句话说,变量中只能有一个值。对于数组,可以为数组变量分配多个值(由其索引标识)但请记住,数组名称本身只能引用单个值

在循环中输入值时,还应记住与
Scanner::next
Scanner::nextInt
等相关的问题。请检查以了解更多信息

考虑到上述各点,处理此类要求的更好方法如下:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        boolean valid;
        int t = 0, n = 0;
        do {
            valid = true;
            System.out.print("Enter the number of integers you want to process: ");
            try {
                t = Integer.parseInt(s.nextLine());
                if (t < 0) {
                    System.out.println(
                            "Error: This is a wrong input. The number should be a positive integer. Try again.");
                    valid = false;
                }
            } catch (NumberFormatException e) {
                System.out.println("Error: This is a wrong input. The number should be a positive integer. Try again.");
                valid = false;
            }
        } while (!valid);

        System.out.println("Thank you. Now enter " + t + " intgers as prompted.");
        int evensum = 0;
        for (int i = 1; i <= t; i++) {
            do {
                valid = true;
                System.out.print("Enter an integer: ");
                try {
                    n = Integer.parseInt(s.nextLine());
                    if (n % 2 == 0) {
                        evensum = evensum + n;
                    }
                } catch (NumberFormatException e) {
                    System.out.println("Error: This is a wrong input. Try again.");
                    valid = false;
                }
            } while (!valid);
        }

        // Display the result of adding all even inputs
        System.out.print("The sum of even integers input by you is " + evensum);
    }
}
另一个示例运行:

Enter the number of integers you want to process: 3
Thank you. Now enter 3 intgers as prompted.
Enter an integer: 10
Enter an integer: 5
Enter an integer: 8
The sum of even integers input by you is 18
Enter the number of integers you want to process: a
Error: This is a wrong input. The number should be a positive integer. Try again.
Enter the number of integers you want to process: 10.5
Error: This is a wrong input. The number should be a positive integer. Try again.
Enter the number of integers you want to process: -20
Error: This is a wrong input. The number should be a positive integer. Try again.
Enter the number of integers you want to process: 3
Thank you. Now enter 3 intgers as prompted.
Enter an integer: 10
Enter an integer: xyz
Error: This is a wrong input. Try again.
Enter an integer: 1.5
Error: This is a wrong input. Try again.
Enter an integer: 5
Enter an integer: 8
The sum of even integers input by you is 18

如果有任何问题/疑问,请随时发表评论。

您不能有多个同名变量。制作一个数组或列表来存储您的输入。Krishan-如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。已接受的答案有助于未来访问者自信地使用该解决方案。检查以了解h如何做。不能有多个同名变量。制作一个数组或列表来存储您的输入。Krishan-如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。已接受的答案有助于未来访问者自信地使用该解决方案。检查以了解如何做到这一点。