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

时间:2019-12-07 09:35:11

标签: java java.util.scanner

因此,我尝试使用扫描仪从一行中读取所有输入,然后获取值并找到第二大输入。我不允许使用数组。您应该输入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();

    }
}

6 个答案:

答案 0 :(得分:1)

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

输入:1 2 3

setUp

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

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

\\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 :(得分:0)

您根本不会阅读下一个输入。

您需要添加一行:

while (sc.hasNextInt()) {
    numbers = sc.nextInt();
    if (numbers > largest) {
        // further code here

但是,一旦执行此操作,您将陷入无限循环,因为用户无法停止输入数字。

为此,您可以选择一个破坏条件。

类似的东西:

// this will be prompted everytime the user enters a number
// you can modify this to be visible after a certain length by keeping a variable
System.out.println("Do you wish to continue inputting numbers? Yes/No");
if (sc.next().equals("No")) {
    break;
}

编辑:如果您已经知道将有10个输入,则可以执行以下操作:

for (int I = 0; I < 10; I++) {
    // your code here
}

希望这会有所帮助。祝你好运。

答案 2 :(得分:0)

下面的代码段可用于在同一行上获取多个Integer Input。

await

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

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

答案 3 :(得分:0)

您可以执行以下操作:

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

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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