簡體   English   中英

如何在同一行上獲得多個輸入而沒有空白

[英]How to get multiple inputs on the same line without white space

我正在為我的CS課編寫程序。 設置起來並不難,它可以完成預期的工作。 但是我的問題是我希望它在同一行上接受所有輸入而沒有任何空格。 該程序應采用ISBN的前9位數字並找到第十位並打印出來。 我知道您可以通過將數字作為字符串然后解析(我相信嗎?)來做到這一點,以便獲得單獨的整數。 盡管我不能完全確定我上次嘗試正確設置了while循環,但這樣做會使我不得不完全更改代碼嗎? 問題出在這里:(業務:檢查ISBN-10)ISBN-10(國際標准書號)由10位數字組成:d1d2d3d4d5d6d7d8d9d10。 最后一個數字d10是校驗和,它是使用以下公式從其他九個數字計算得出的:(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9)%11如果校驗和為10,則根據ISBN-10約定,最后一位表示為X。 編寫一個程序,提示用戶輸入前9位數字並顯示10位ISBN(包括前導零)。 您的程序應將輸入讀取為整數。 無論哪種方式,代碼都是沒有循環的空白輸入:

/* Class:        CS1301
* Section:       9:30
* Term:          Fall 2015
* Name:          Matthew Woolridge
* Instructor:    Mr. Robert Thorsen
* Assignment:    4
* Program:       1
* ProgramName:   ISBN_Number
* Purpose:       Prompts the user to enter the first nine numbers of an ISBN and calculates the tenth
* Operation:     The information to be numbers are statically instantiated in the code and
*                the table is output to the screen.
* Input(s):      The user inputs the first nine ISBN numbers
* Output(s):     The output will be the full the tenth ISBN number in the full number
* Methodology:   The program will use if statements and defined variables which use the user input variables to calculate the ISBN
* and return it to the user.
*
*/

import java.util.Scanner;
public class ISBN_Number
{

   public static void main (String[] args)
   {

     /******************************************************************************
      *                          Declarations Section                               *
      ******************************************************************************/
      /****************************CONSTANTS********************************/
      int [] ISBN = new int [8];
      int num1 = 0;
      int nextNum;
      int i; 
      int input=0;
      int num10=0;
      String fullIsbn;

      Scanner scan = new Scanner (System.in); //Scanner utility initialization

     /******************************************************************************
      *                             Inputs Section                                  *
      ******************************************************************************/

      System.out.print("Please input the first nine digits (between 0-9) of your ISBN not including the leading 0: ");
      for (i = 0; i<ISBN.length;i++)
      {
         nextNum = scan.nextInt(); // For loop that asks for an input until it fills the array of 8
         ISBN[i] = nextNum;
         ++input;
      }

     /****************************variables********************************/
      if (input == 8)
      {
       num10 = (ISBN[0] * 1 + ISBN[1] * 2 + ISBN[2] * 3 + ISBN[3] * 4 + ISBN[4] * 5 + ISBN[5] * 6 + ISBN[6] * 7 + ISBN[7] * 8 + ISBN[8] * 9) % 11; // Calculates the value of num10
      }
      else 
      {
       System.out.println("You did not input enough or input too many digits.");
      }

     /******************************************************************************
      *                             Processing Section                            *
      ******************************************************************************/

      if (num10 == 10)
      { 
         fullIsbn = num1 + ISBN[0] + "" + ISBN[1] + "" + ISBN[2] + "" + ISBN[3] + "" + ISBN[4] + "" + ISBN[5] + "" + ISBN[6] + "" + ISBN[7] + "" + ISBN[8] + "" + "X";
      }
      else  // Determines if num10 is equal to x or not and prints
      {
         fullIsbn = num1 + "" + ISBN[0] + "" + ISBN[1] + "" + ISBN[2] + "" + ISBN[3] + "" + ISBN[4] + "" + ISBN[5] + "" + ISBN[6] + "" + ISBN[7] + "" + ISBN[8] + "" + num10;
      }

      /******************************************************************************
       *                              Outputs Section                                *
       ******************************************************************************/

      System.out.print("The full ISBN number is " + fullIsbn); // Prints the full 10 digit isbn
   } // Rnds string
} // Ends program

這里是想法:

  1. 將用戶輸入作為int數組的索引。
  2. Sytem.out.print(yourArrayName [9]); //打印出數組的第十個數字

例如:

import java.util.Scanner;
public class Main
{

    public static void main (String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        int [] ISBN = new int [10];
        int input =0;

        System.out.print("Give me 10 numbers: ");

        for (int i = 0; i<ISBN.length;i++){
            int nextNumber = keyboard.nextInt();
            ISBN[i] = nextNumber;
            ++input;

            if (input==10){
                System.out.print("The tenth number you gave me is: "+ISBN[9]);
            }
        }

    } 
} 

您應該能夠應用上述策略,從而實現您所要求的:

import java.util.*;
public class Main
{

    public static void main (String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        int [] ISBN = new int [10];
        int input =0;
        String fullIsbn;

        System.out.print("Give me 10 numbers: ");

        for (int i = 0; i<ISBN.length;i++){
            int nextNumber = keyboard.nextInt();
            ISBN[i] = nextNumber;
            ++input;

            if (input==10){
                ISBN[0]=0;
                ISBN[9] = (ISBN[0] * 1 + ISBN[1] * 2 + ISBN[2] * 3 + ISBN[3] * 4 + ISBN[4] * 5 + ISBN[5] * 6 + ISBN[6] * 7 + ISBN[7] * 8 + ISBN[8] * 9) % 11;
                if (ISBN[9] == 10)
                {

                    fullIsbn = ISBN[0] + "" + ISBN[1] + "" + ISBN[2] + "" + ISBN[3] + "" + ISBN[4] + "" + ISBN[5] + "" + ISBN[6] + "" + ISBN[7] + "" + ISBN[8] + "" + "X";
                }
                else  // Determines if num10 is equal to x or not and prints
                {
                    fullIsbn = ISBN[0] + "" + ISBN[1] + "" + ISBN[2] + "" + ISBN[3] + "" + ISBN[4] + "" + ISBN[5] + "" + ISBN[6] + "" + ISBN[7] + "" + ISBN[8] + "" + ISBN[9];

                }
                System.out.print("The tenth number you gave me is: "+fullIsbn);
            }
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM