繁体   English   中英

如何在Java中的一行上声明输入

[英]how to declare inputs on one line in java

我无法让我的程序在一行上读取所有整数,而不必在终端窗口中输入每个数字并在每个数字后按回车。

例如,终端窗口将显示为:

For the our text enter the first 9 digits: 013292373"

而不是

For the our text enter the first 9 digits: 0
1
2
3
... etc"

到目前为止,我的代码如下:

/**
* This program calculates the last number of a 10 digit ISBN number.
*/  
import java.util.Scanner;
public class ISBNnum
{
public static void main(String[] args)
{
   //Variables and Scanner
   Scanner input = new Scanner(System.in);
   int dOne;
   int dTwo;
   int dThree;
   int dFour;
   int dFive;
   int dSix;
   int dSeven;
   int dEight;
   int dNine;
   int checksum;

   //Input
   System.out.print("For the our text enter the first 9 digits: ");
   dOne = input.nextInt();
   dTwo = input.nextInt();
   dThree = input.nextInt();
   dFour = input.nextInt();
   dFive = input.nextInt();
   dSix = input.nextInt();
   dSeven = input.nextInt();
   dEight = input.nextInt();
   dNine = input.nextInt();

   //Calculation
   checksum = ((dOne * 1) + (dTwo * 2) + (dThree * 3) + (dFour * 4) + (dFive * 5) + (dSix * 6) +
   (dSeven * 7) + (dEight * 8) + (dNine * 9)) % 11;

   //Output
   if (checksum == 10)
   {
       System.out.print("The whole ISBN is "+dOne+dTwo+dThree+dFour+dFive+dSix+dSeven+dEight+
       dNine+"X");
    }
    else if (checksum < 10)
    {
    System.out.println("The whole ISBN is " + dOne + dTwo + dThree + dFour + dFive + dSix +
    dSeven + dEight + dNine + " - " + checksum);
}
}
}

感谢您的帮助!

Scanner s = new Scanner(System.in).useDelimiter("\\s*");

改编自
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

尝试使用扫描仪,并按空格将String拆分为String[]

import java.util.Scanner;

public class Foo {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in)
        String input = s.nextLine();
        String[] parts = input.split(" ");
        for (String part : parts) {
            System.out.println(part);
        }
    }
}

我用下面的代码扩展了上面的答案。 当我编译并运行它时,它起作用了,用空格分隔了九个数字。

public static void main(String[] args) {
    //Variables and Scanner
    Scanner input = new Scanner(System.in);
    int[] numbers = new int[9];
    int checksum = 0;

    //Input
    System.out.print("For the our text enter the first 9 digits, separated by spaces: ");
    String numInput = input.nextLine();
    String[] parts = numInput.split(" ");
    for (int i = 0; i < parts.length; i++) {
        numbers[i] = Integer.parseInt(parts[i]);
        checksum += numbers[i] * (i + 1);
    }
    //Calculation
    checksum %= 11;
}

跟踪代码,首先创建一个Scanner input对象,一个带有9个索引的int[] numbers ,以及一个实例化为零的int checksum 接下来,我提示用户输入用空格分隔的九个数字,然后使用input.nextLine()接收整行,这会将stdin返回到换行符。 然后,我将numInput按空格拆分为String[] parts ,使所有内容均在第一个空格处索引为零,在第二个空格处索引为1, numInput 。我遍历列表,将数字的每个String表示形式更改为int对象,然后将其放在索引inumbers数组中。 然后,我用numbers[i] (刚刚转换的数字)更新checksum Integer并乘以i+1 循环后,我将checksum乘以11。

终端上的输入看起来像

For the our text enter the first 9 digits, separated by spaces: 1 4 6 2 8 7 2 4 10

如果需要,更改显示String[] parts = input.split(" "); String[] parts = input.split(","); 您可以这样输入数字:

1,4,6,2,8,7,2,4,10

暂无
暂无

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

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