0

すべての配列要素を表示する代わりに、ユーザーに数値の入力を求め、その要素だけを表示するようにプログラムを変更します。これを機能させるには、Scanner を使用して int 値を返す必要があります。

上記は私が何をする必要があるかを述べており、コードは私の現在のステータスを示しています。どうすればいいのかわからない、乾杯。

import java.util.Arrays;
import java.util.Scanner;


/**
 *
 * Purpose: Introduction to data structures
 * @author p'o'p'
 *
 */
public class StudentNames{

    public static void main(String[] args){         
        String[] names = new String[8];                                        
        Scanner s = new Scanner( System.in );
        for( int i = 0; i < 8; i++ ){
            System.out.println( "Enter student name:" );                            
            names[ i ] = s.next();                          
        }                        
        for (int i = 0; i < 8; i++ ){               
            System.out.println(" Input your chosen number" );
            System.out.println("Student name:"+ names[ i ]);         
            int index = i;
            System.out.println( index );                              
        }

    }
}
4

2 に答える 2

0

効率的なコードではなく、以下のコードを試してください。コードを変更しました。

import java.util.Arrays;
import java.util.Scanner;


/**
*
* Purpose: Introduction to data structures
* @author p'o'p'
*
*/

public class NewClass1{

public static void main(String[] args){         
    String[] names = new String[2];                                        
    Scanner s = new Scanner( System.in );
    for( int i = 0; i < 2; i++ ){
        System.out.println( "Enter student name:" );                            
        names[ i ] = s.next();                          
    }
    System.out.println(" Input your chosen number" );
    Scanner sc=new Scanner(System.in);
    int choice=sc.nextInt();
    for(int i=0;i<names.length;i++)
    {
        if(i==choice && choice < names.length)  
        {
         System.out.println("Student name:"+ names[ i ]);
         System.out.println(i);
        }
     }
}

}

于 2013-11-12T11:12:26.947 に答える