簡體   English   中英

使用掃描程序接受字符串輸入並存儲在字符串數組中

[英]Using a scanner to accept String input and storing in a String Array

有誰可以幫助我嗎。 我做了很多搜索,但無法在任何地方找到解決方案。 我是Java的初學者,目前在大學休息期間練習一些代碼。

我正在嘗試制作電話簿程序。 目前我正在嘗試添加一個新的聯系人,下面是我的代碼,但我不知道如何將數據存儲在一個數組中,有人可以給我一些指示。

    import java.util.Scanner;

    public class addContact {
    public static void main(String [] args){

    //declare arrays
        String [] contactName = new String [12];
        String [] contactPhone = new String [12];
        String [] contactAdd1 = new String [12];
        String [] contactAdd2 = new String [12];

    //inputs
    String name = "";
    String phone = "";
    String add1 = "";
    String add2 = "";

    //method of taken input
    Scanner input = new Scanner(System.in);

    //while name field is empty display prompt etc.
    while (name.equals(""))
    {
    System.out.println("Enter contacts name: ");
    name = input.nextLine();
    name += contactName[];
    }


    while (add1.equals(""))
    {
    System.out.println("Enter contacts addressline1:");
    add1 = input.nextLine();
    add1 += contactAdd1[];
    }

    while (add2.equals(""))
    {
    System.out.println("Enter contacts addressline2:");
    add2 = input.nextLine();
    add2 += contactAdd2[];
    }

    while (phone.equals(""))
    {
    System.out.println("Enter contact phone number: ");
    phone = input.nextLine();
    phone += contactPhone[];
    }

    }   
}

一個清潔的方法是創建一個Person ,其中包含對象contactNamecontactPhone等。然后,使用一個ArrayList而不是一個數組中添加新的對象。 創建一個接受每個`Person的所有字段的循環:

while (!done) {
   Person person = new Person();
   String name = input.nextLine();
   person.setContactName(name);
   ...

   myPersonList.add(person);
}

使用該列表將不再需要數組邊界檢查。

這段代碼的一個問題是:

name += contactName[];

該指令不會在數組中插入任何內容。 相反,它將連接變量名稱的當前值與contactName數組的字符串表示形式。

而是使用這個:

contactName[index] = name;

此指令將變量名稱存儲在索引index的contactName數組中。

你遇到的第二個問題是你沒有變量index

你可以做的是一個循環,有12次迭代來填充你的所有數組。 index將是你的迭代變量)

//go through this code I have made several changes in it//

import java.util.Scanner;

public class addContact {
public static void main(String [] args){

//declare arrays
String [] contactName = new String [12];
String [] contactPhone = new String [12];
String [] contactAdd1 = new String [12];
String [] contactAdd2 = new String [12];
int i=0;
String name = "0";
String phone = "0";
String add1 = "0";
String add2 = "0";
//method of taken input
Scanner input = new Scanner(System.in);

//while name field is empty display prompt etc.
while (i<11)
{
    i++;
System.out.println("Enter contacts name: "+ i);
name = input.nextLine();
name += contactName[i];
}


while (i<12)
{
    i++;
System.out.println("Enter contacts addressline1:");
add1 = input.nextLine();
add1 += contactAdd1[i];
}

while (i<12)
{
    i++;
System.out.println("Enter contacts addressline2:");
add2 = input.nextLine();
add2 += contactAdd2[i];
}

while (i<12)
{
    i++;
System.out.println("Enter contact phone number: ");
phone = input.nextLine();
phone += contactPhone[i];
}

}   
}

這會更好嗎?

import java.util.Scanner;

public class Work {

public static void main(String[] args){

    System.out.println("Please enter the following information");

    String name = "0";
    String num = "0";
    String address = "0";

    int i = 0;

    Scanner input = new Scanner(System.in);

    //The Arrays
    String [] contactName = new String [7];
    String [] contactNum = new String [7];
    String [] contactAdd = new String [7];

    //I set these as the Array titles
    contactName[0] = "Name";
    contactNum[0] = "Phone Number";
    contactAdd[0] = "Address";

    //This asks for the information and builds an Array for each
    //i -= i resets i back to 0 so the arrays are not 7,14,21+
    while (i < 6){

        i++;
        System.out.println("Enter contact name." + i);
        name = input.nextLine();
        contactName[i] = name;
    }

    i -= i;
    while (i < 6){
        i++;
        System.out.println("Enter contact number." + i);
        num = input.nextLine();
        contactNum[i] = num;
    }

    i -= i;
    while (i < 6){
        i++;
        System.out.println("Enter contact address." + i);
        num = input.nextLine();
        contactAdd[i] = num;
    }


    //Now lets print out the Arrays
    i -= i;
    while(i < 6){
    i++;
    System.out.print( i + " " + contactName[i] + " / " );
    }

    //These are set to print the array on one line so println will skip a line
    System.out.println();
    i -= i;
    i -= 1;

    while(i < 6){
    i++;

    System.out.print( i + " " + contactNum[i] + " / " );
    }

    System.out.println();
    i -= i;
    i -= 1;

    while(i < 6){
        i++;

    System.out.print( i + " " + contactAdd[i] + " / " );
    }

    System.out.println();

    System.out.println("End of program");

}

}

如果我錯了,請糾正我

public static void main(String[] args) {

    Scanner na = new Scanner(System.in);
    System.out.println("Please enter the number of contacts: ");
    int num = na.nextInt();

    String[] contactName = new String[num];
    String[] contactPhone = new String[num];
    String[] contactAdd1 = new String[num];
    String[] contactAdd2 = new String[num];

    Scanner input = new Scanner(System.in);

    for (int i = 0; i < num; i++) {

        System.out.println("Enter contacts name: " + (i+1));
        contactName[i] = input.nextLine();

        System.out.println("Enter contacts addressline1: " + (i+1));
        contactAdd1[i] = input.nextLine();

        System.out.println("Enter contacts addressline2: " + (i+1));
        contactAdd2[i] = input.nextLine();

        System.out.println("Enter contact phone number: " + (i+1));
        contactPhone[i] = input.nextLine();

    }

    for (int i = 0; i < num; i++) {
        System.out.println("Contact Name No." + (i+1) + " is "+contactName[i]);
        System.out.println("First Contacts Address No." + (i+1) + " is "+contactAdd1[i]);
        System.out.println("Second Contacts Address No." + (i+1) + " is "+contactAdd2[i]);
        System.out.println("Contact Phone Number No." + (i+1) + " is "+contactPhone[i]);
    }
}

`

到目前為止,java中沒有使用指針。 您可以從類中創建一個對象,並使用彼此鏈接的不同類,並使用主類中每個類的函數。

暫無
暫無

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

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