繁体   English   中英

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

[英]How to read multiple inputs on same line in Java?

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

像这样的东西:

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();

    }
}
/*
**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);
 }   
}

要读取单行输入,您可以简单地通过吃 (\\n) 这是 Java 中的新行来做到这一点。

输入: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. 
}

这将完美地工作。

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

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

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

对于由空格或逗号分隔的同一行上的多个字符串输入,我们可以使用以下代码段

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

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

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();
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM