如何在 Java 的同一行读取多个输入?

How to read multiple inputs on same line in Java?

所以我尝试使用扫描仪从一行中读取所有输入,然后获取值并找到第二大的值。我会使用数组 BUT 我不被允许。您应该输入 10 个整数,按回车键并计算它们。

像这样:

10 20 30 40 50 60 70 80 90 100 ENTER

Second highest number is: 90

我根本无法解决。这应该很容易,但我不知道。

public class SecondLargest {

public static void main(String[] args) {
    {
        int largest = 0;
        int secondLargest = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter integers: ");
        int numbers = sc.nextInt();
        largest = numbers;

        while (sc.hasNextInt()) {
            if (numbers > largest) {
                secondLargest = largest;
                largest = numbers;
            } else if (numbers > secondLargest) {
                secondLargest = numbers;
            }
        }
        System.out.println("Second largest number is: " + secondLargest);
        sc.close();

    }
}

下面的代码片段可用于在同一行进行多个整数输入。

Scanner sc= new Scanner(System.in);    // Declare and Initialize Scanner
while(sc.hasNext())                    // While the input has data execute
    System.out.println(sc.nextInt());  // nextInt() method is used to take Integer data

对于在同一行上以白色 space 或逗号分隔的多个字符串输入, 我们可以使用以下代码片段

Scanner sc = new Scanner(System.in).useDelimiter("[,\s+]");

您可以按照以下方式进行:

import java.util.Scanner;

public class SecondLargest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter 10 integers separated by single spaces: ");
        String str = sc.nextLine();
        int last = str.lastIndexOf(" ");
        int num = Integer.parseInt(str.substring(0, str.indexOf(" ")));
        int largest = num;
        int secondLargest = num;
        int index = str.indexOf(" ");
        for (int i = 1; i < 10; i++) {
            str = str.substring(index + 1);
            index = str.indexOf(" ");
            if (index != -1) {
                num = Integer.parseInt(str.substring(0, index));
                if (num > largest) {
                    secondLargest = largest;
                    largest = num;
                }
            }
        }
        System.out.println("The second largest number is: " + secondLargest);
    }
}

样本运行:

Enter 10 integers separated by single spaces: 10 23 4 12 80 45 78 90 105 7
The second largest number is: 90

注意:这只是一个假设输入格式正确的示例程序。我把它留给你去研究如何处理错误的输入。这对你来说是一个很好的锻炼。如果您需要任何进一步的帮助,请随时发表评论。祝你好运!

/*
**In this code I've used BufferedReader class which is faster than Scanner 
Class as well as have large buffer of 8KB while Scanner has only 1KB.**
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Second_Highest {
 public static void main(String[] args) throws IOException
 {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String[] s=br.readLine().split(" ");
    int a[]=new int[10];
    int max=Integer.parseInt(s[0]);
    for(int i=0;i<10;i++)
    {
        a[i]=Integer.parseInt(s[i]);
        if(a[i]>max)
        {
            max=a[i];
        }
    }
    int secondmax=Integer.parseInt(s[0]);
    for(int i=0;i<10;i++)
    {
        if(a[i]>secondmax && a[i]<max)
        {
            secondmax=a[i];
        }
    }
    System.out.print(secondmax);
 }   
}
import java.util.*;
public class m1{
public static void main(String args[]){
Scannerscan = new Scanner(System.in);
Stringnum = scan.nextLine();
String[] strs = num.split("\s+");
int[] arrs = new int[strs.length];
for(inti=0;i<strs.length;i++)
{
String stringnum = strs[i];
arrs[i] = Integer.parseInt(stringnum);
if(arrs[i]%2==0){
System.out.print(" ");
}
else{
System.out.print(arrs[i]);
}
}
scan.close();
}
}

要读取单行输入,只需吃掉 Java.

中的换行符 (\n) 即可

输入:1 2 3

\Create the object of Scanner Class
Scanner sc = new Scanner(System.in);

int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();

// this will eat the new line and move forward for other inputs.
sc.nextLine()


循环读取或存储在二维数组中。

输入: 1 2 3 3 4 5 6 7 8

//... do the above declaration of array

for ( int i = 0; i < n; i++){
   for( int j = 0; j < m; j++){
      arr[i][j] = sc.nextInt()
    } 
   sc.nextLine(); // you need to eat the \n here. 
}

这将非常有效。

现在您应该能够在一行中轻松获得输入。