字符串数组的读取

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

reading of string array

问题

我正在尝试在Java中读取一组字符串值但我只能读取到第n-1个值例如如果字符串数组大小为4我只能提供3个输入
以下是我的代码

package my_project;

import java.util.Scanner;

public class ArrayString 
{
    public static void main(String[] args)
    {
        int n;
        String key;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the no of courses:");
        n = sc.nextInt();
        if (n <= 0)
        {
            System.out.println("Invalid range");
            System.exit(0);
        }
        System.out.println("The available courses are:");
        String[] courses = new String[n];
        for (int i = 0; i < n; i++)
        {
            courses[i] = sc.nextLine();
        }
        for (int i = 0; i < n; i++)
        {
            if (courses[i].equals("java"))
            {
                System.out.println("course is available ");
                System.exit(0);
            }
        }
    }
}
英文:

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.

package my_project;

import java.util.Scanner;

public class ArrayString 
{
	public static void main(String[] args)
	{
		int n;
		String key;
		Scanner sc=new Scanner (System.in);
		System.out.println(&quot;Enter the no of courses:&quot;);
		n=sc.nextInt();
		if(n&lt;=0)
		{
			System.out.println(&quot;Invalid range&quot;);
			System.exit(0);
		}
		System.out.println(&quot;The available courses are:&quot;);
		String [] courses=new String[n];
		for(int i=0;i&lt;n;i++)
		{
			courses[i]=sc.nextLine();
		}
		for(int i=0;i&lt;n;i++)
		{
			if(courses[i].equals(&quot;java&quot;))
			{
				System.out.println(&quot;course is available &quot;);
				System.exit(0);
			}
		}
	}

}

答案1

得分: 0

public static void main(String... args) {
    try (Scanner sc = new Scanner(System.in)) {
        System.out.print("输入课程数量:");
        int n = sc.nextInt();
        sc.nextLine();  // 在读取整数后加入此行以读取后续的字符串

        if (n <= 0)
            System.out.println("无效的范围");
        else {
            System.out.println("可用课程有:");
            String[] courses = new String[n];

            for (int i = 0; i < n; i++) {
                System.out.format("#%d: ", i + 1);
                courses[i] = sc.nextLine();
            }

            for (int i = 0; i < n; i++) {
                if ("java".equalsIgnoreCase(courses[i])) {
                    System.out.println("课程可用");
                    return;
                }
            }
        }
    }
}
英文:
public static void main(String... args) {
    try (Scanner sc = new Scanner(System.in)) {
        System.out.print(&quot;Enter the no of courses: &quot;);
        int n = sc.nextInt();
        sc.nextLine();  // add this to read String after int

        if (n &lt;= 0)
            System.out.println(&quot;Invalid range&quot;);
        else {
            System.out.println(&quot;The available courses are:&quot;);
            String[] courses = new String[n];

            for (int i = 0; i &lt; n; i++) {
                System.out.format(&quot;#%d: &quot;, i + 1);
                courses[i] = sc.nextLine();
            }

            for (int i = 0; i &lt; n; i++) {
                if (&quot;java&quot;.equalsIgnoreCase(courses[i])) {
                    System.out.println(&quot;course is available&quot;);
                    return;
                }
            }
        }
    }
}

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:

确定