简体   繁体   English

如何显示来自用户输入的数组中的字符串?

[英]How do I display a string from an array from user input?

I have an array of 8 strings 我有8个字符串的数组

(Carter","Cocke","Washington","Greene","Hawkins","Johnson","Sullivan","Unicoi") (卡特”, “科克”, “华盛顿”, “格林”, “霍金斯”, “强生”, “苏利文”, “尤尼科伊”)

all referenced by "county" 全部由“县”引用

I'd prompt the user to input a number of 1-8 (iVal), 1 being Carter, 2 being Cocke, etc... 我提示用户输入1-8(iVal),1是Carter,2是Cocke,等等。

Is there a way I could do this other than using a switch statement? 除了使用switch语句之外,还有其他方法吗?

Also, how would I go about this if the user were to input Washington I'd display "Washington is in the array" and the opposite if the string isn't in the array? 另外,如果用户输入Washington我将显示“华盛顿在数组中”,而如果字符串不在数组中则显示相反的文字,我将如何处理呢?

Thanks in advance. 提前致谢。

Here is my array. 这是我的数组。

   String [] county = {"Carter","Cocke","Washington","Greene","Hawkins","Johnson","Sullivan","Unicoi"};
   for (int i = 0; i< county.length; i++)
      {
         System.out.print (county [i]+ "\n");
      }

To prompt the user to enter a county, and then display it (without a switch) is simple enough. 提示用户输入一个县,然后显示它(不进行切换)非常简单。 You could use something like, 您可以使用类似

String[] county = { "Carter", "Cocke", "Washington", "Greene",
        "Hawkins", "Johnson", "Sullivan", "Unicoi" };
Scanner scan = new Scanner(System.in);
for (int i = 0; i < county.length; i++) {
    System.out.printf("%d %s%n", i + 1, county[i]);
}
System.out.println("Please select a county from above: ");
int choice = scan.nextInt();
if (choice > 0 && choice <= county.length) {
    System.out.println("You selected: " + county[choice - 1]);
} else {
    System.out.println("Not a valid choice: " + choice);
}

As for testing if a String array contains a particular String you could write a utility function using the for-each loop like 至于测试String数组是否包含特定的String您可以使用for-each循环编写实用程序函数for-each例如

public static boolean contains(String[] arr, String val) {
    if (arr != null) {
        for (String str : arr) {
            if (str.equals(val)) {
                return true;
            }
        }
    }
    return false;
}

i was working on the same thing, use ArrayList. 我在做同样的事情,使用ArrayList。 this was my code 这是我的代码

import java.util.*;
 public class Practice{
    public static void main(String[] args){
       ArrayList<String> mylist = new ArrayList<>();
        mylist.add("Maisam Bokhari");
        mylist.add("Fawwad Ahmed");
        mylist.add("Ali Asim");
        mylist.add("Maheen Hanif");
        mylist.add("Rimsha Imtiaz");
        mylist.add("Mugheer Mughal");
        mylist.add("Maaz Hussain");
        mylist.add("Asad Shahzada");
        mylist.add("Junaid Khan");
        System.out.println("Name of the student: "+mylist);
      }
  }

now if u want a specific name from the list put this in system.out.println 现在,如果您要从列表中选择一个特定名称,请将其放入system.out.println
System.out.println("Name of the student: "+mylist. get(1) ); System.out.println(“学生姓名:” + mylist。get (1) );

now the trick is to let the user enter the number in get( ) 现在的诀窍是让用户在get()中输入数字

for this i made this program, here is the code 为此,我做了这个程序,这是代码

first make a scanner 首先做一个扫描仪

    Scanner myScan = new Scanner(System.in);
    int a = myScan.nextInt();


    System.out.println("Name of the student: "+mylist.get(a));

now it will only print that name depending on what number user have entered !! 现在,它将仅根据用户输入的号码打印该名称!

I didn't understand well your questions, but I am going to answer on what I have understand. 我对您的问题不太了解,但是我将就我所了解的问题进行回答。

In case you want to print the value of a given index by the user here is the solution: Try an i with correct (existing) index and another one which does not exist eg i=9 如果您要由用户打印给定索引的值,这里是解决方案:尝试使用具有正确(现有)索引的i和另一个不存在的索引,例如i=9

public class app
{
   public static void main(String[] args)
   {
      String [] county ={"Carter","Cocke","Washington","Greene","Hawkins","Johnson","Sullivan","Unicoi"};
      int i = 10;  //i is the user input, you should do that using a BufferedReader or Scanner.
      try
      {
         System.out.println(county[i-1]);
      }
      catch(IndexOutOfBoundsException e)
      {
         System.out.println("This index doesn't exist");
      }
   }
}

In case you want to check if a given word exist you can do it that way: Again try a string which exist and one which does not exist. 如果要检查给定的单词是否存在,可以按照以下方法进行操作:再次尝试一个存在的字符串和一个不存在的字符串。

public class app
{
   public static void main(String[] args)
   {
      String [] county = {"Carter","Cocke","Washington","Greene","Hawkins","Johnson","Sullivan","Unicoi"};
      String word = "Cocke";  //word is the user input, you should do that using a BufferedReader or Scanner.
      boolean found = false;
      for(int i=0; i<=7; ++i)
      {
         if(word == county[i])
         {
            found = true;
            break;
         }
      }

      if(found == true)
      {
         System.out.println(word + " is in the array.");
      }
      else
      {
         System.out.println(word + " is not in the array.");
      }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JFrame Java中从用户输入中绘制字符串 - How do I draw string from user input in a JFrame Java 如何将用户字符串输入与整个数组中的字符串进行比较? - How do I compare user string input to the string in an entire array? 如何通过询问用户输入来打印数组中的元素? (将输入与数组中的元素匹配) - How do I print an element from an array by asking for user input? (matching input with elements in the array) 如何将用户输入用于while循环中的字符串数组? - How do i use user input for a string array that is in a while loop? 如何显示用户输入数组中最大和最小的数字? - How am I able to display the largest and the smallest number from the array of the user's input? 如何显示数组中的图像? - How do I display images from an array? 如何将数据文件中的数据加载到数组中并要求用户输入,然后根据用户输入查找和打印数据? - How do I load data from a data file into an Array and ask for user input then find and print the data based off of user input? 我应该如何使用用户输入从多维数组存储数组 - How i should store array from a multidimensional array with user input 如何使用来自 Java 中二维数组的 select 数据的用户输入? - How do I use user input to select data from a 2D array in Java? 如何检查用户的INPUT是否与数组中的项匹配(例如登录) - How do I check if INPUT from a user matches an item in an array.(Like a login)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM