字符串数组的读取

huangapple go评论105阅读模式
英文:

reading of string array

问题

  1. 我正在尝试在Java中读取一组字符串值但我只能读取到第n-1个值例如如果字符串数组大小为4我只能提供3个输入
  2. 以下是我的代码
  3. package my_project;
  4. import java.util.Scanner;
  5. public class ArrayString
  6. {
  7. public static void main(String[] args)
  8. {
  9. int n;
  10. String key;
  11. Scanner sc = new Scanner(System.in);
  12. System.out.println("Enter the no of courses:");
  13. n = sc.nextInt();
  14. if (n <= 0)
  15. {
  16. System.out.println("Invalid range");
  17. System.exit(0);
  18. }
  19. System.out.println("The available courses are:");
  20. String[] courses = new String[n];
  21. for (int i = 0; i < n; i++)
  22. {
  23. courses[i] = sc.nextLine();
  24. }
  25. for (int i = 0; i < n; i++)
  26. {
  27. if (courses[i].equals("java"))
  28. {
  29. System.out.println("course is available ");
  30. System.exit(0);
  31. }
  32. }
  33. }
  34. }
英文:

I am trying to read set of string values in java but i am able to read only upto n-1 value i.e for eg if string array size is 4 i am bale to give only 3 inputs.
here is my code.

  1. package my_project;
  2. import java.util.Scanner;
  3. public class ArrayString
  4. {
  5. public static void main(String[] args)
  6. {
  7. int n;
  8. String key;
  9. Scanner sc=new Scanner (System.in);
  10. System.out.println(&quot;Enter the no of courses:&quot;);
  11. n=sc.nextInt();
  12. if(n&lt;=0)
  13. {
  14. System.out.println(&quot;Invalid range&quot;);
  15. System.exit(0);
  16. }
  17. System.out.println(&quot;The available courses are:&quot;);
  18. String [] courses=new String[n];
  19. for(int i=0;i&lt;n;i++)
  20. {
  21. courses[i]=sc.nextLine();
  22. }
  23. for(int i=0;i&lt;n;i++)
  24. {
  25. if(courses[i].equals(&quot;java&quot;))
  26. {
  27. System.out.println(&quot;course is available &quot;);
  28. System.exit(0);
  29. }
  30. }
  31. }
  32. }

答案1

得分: 0

  1. public static void main(String... args) {
  2. try (Scanner sc = new Scanner(System.in)) {
  3. System.out.print("输入课程数量:");
  4. int n = sc.nextInt();
  5. sc.nextLine(); // 在读取整数后加入此行以读取后续的字符串
  6. if (n <= 0)
  7. System.out.println("无效的范围");
  8. else {
  9. System.out.println("可用课程有:");
  10. String[] courses = new String[n];
  11. for (int i = 0; i < n; i++) {
  12. System.out.format("#%d: ", i + 1);
  13. courses[i] = sc.nextLine();
  14. }
  15. for (int i = 0; i < n; i++) {
  16. if ("java".equalsIgnoreCase(courses[i])) {
  17. System.out.println("课程可用");
  18. return;
  19. }
  20. }
  21. }
  22. }
  23. }
英文:
  1. public static void main(String... args) {
  2. try (Scanner sc = new Scanner(System.in)) {
  3. System.out.print(&quot;Enter the no of courses: &quot;);
  4. int n = sc.nextInt();
  5. sc.nextLine(); // add this to read String after int
  6. if (n &lt;= 0)
  7. System.out.println(&quot;Invalid range&quot;);
  8. else {
  9. System.out.println(&quot;The available courses are:&quot;);
  10. String[] courses = new String[n];
  11. for (int i = 0; i &lt; n; i++) {
  12. System.out.format(&quot;#%d: &quot;, i + 1);
  13. courses[i] = sc.nextLine();
  14. }
  15. for (int i = 0; i &lt; n; i++) {
  16. if (&quot;java&quot;.equalsIgnoreCase(courses[i])) {
  17. System.out.println(&quot;course is available&quot;);
  18. return;
  19. }
  20. }
  21. }
  22. }
  23. }

huangapple
  • 本文由 发表于 2020年4月3日 22:21:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61013978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定