我怎样才能让它更短更好?

时间:2016-06-29 03:29:24

标签: java arrays

如何让它更短更好?

import java.util.Scanner;

  public class letter {
     public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        String 1 , 2 ,3 ,4 ,5 ,6 ,7 and so forth;
        System.out.print("Enter the number: \t");
        1 = input.nextInt();
        2 = input.nextInt();
        3 = input.nextInt();
        ...and so forth.

        if (1 == A){System.out.println("Your name is anything");}

2 个答案:

答案 0 :(得分:0)

您可以使用for循环和数组使其更简单。首先声明数组int[] name = new int['how many items you want to enter'];然后使用一个说

的for循环
for(int i = 0; i<name.length; i++){
name[i]=input.nextInt();
}

要访问数组中的项目,请输入数组名称,然后输入所需项目的索引。在java数组中,从索引0开始,因此第一个元素的索引为0,即name[2]将为2提供name的索引pymysql.connect }。

答案 1 :(得分:0)

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

  public static void main(String[] args) throws Exception {
  Step 1:  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //More suitable to read data than Scanner Class
    String input = reader.readLine(); //Use appropriate "reading" method for eg. readLine() for reading String

 Step 2 : Iterate according to your need and read data


  Step 3 :   reader.close(); //Close the BufferReader
  }
}