簡體   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