0

String入力とユーザーからの入力を受け入れ、doubleそれらを 2 つの並列配列に格納してショッピング カートを作成する必要があります。

両方の配列をユーザーが決定したサイズに初期化Amountし、入力に対して実行して 2 つの配列に値を格納する for ループを作成し、追加を設定しinput.nextLine()て、コードがループの最後に作成された新しい行を飲み込むようにしました。これはこれまでの私のコードです。

import java.util.*;
import java.io.*;
public class Item
{
    public static void main(String[] args)
    throws java.io.IOException
    {
        int amount;
        String nextItem,I;

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the number of items you would like to add:");
        amount = input.nextInt();

        String cart[] = new String[amount];
        double price[] = new double[amount];

        for(int i = 0; i<amount; i++)
        {
            System.out.println("Please enter the name of an item:");
            cart[i] = input.nextLine();

            System.out.println("Price?");
            price[i] = input.nextDouble();
            input.nextLine();

        }

     }
}

ただし、プログラムがループを実行するとSystem.out.println、最初の をスキップして両方のコマンドが実行input.nextLine()され、整数入力のみが受け入れられます。

4

1 に答える 1

0

2 つのスキャナ クラスを使用する

     Scanner sc=new Scanner(System.in);   
     Scanner sc1=new Scanner(System.in);
      
    int p[] = new int[3];

    String n[] = new String[3];

    for(int i = 0; i < p.length; i++){

        System.out.print("Name: ");

        n[i] = sc.nextLine();

        System.out.print("Percentage: ");

        p[i]=sc1.nextInt();

// 2 つのスキャナ クラスを使用

于 2021-02-27T12:37:50.543 に答える