Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的字符串输入_Java - Fatal编程技术网

java中的字符串输入

java中的字符串输入,java,Java,我尝试使用Scanner在java中获取字符串输入,但在此之前,我获取整数输入。这是我的密码 import java.util.*; class prc { public static void main(String[] args) { Scanner input=new Scanner(System.in); int n=input.nextInt(); for(int i=1;i<=n;i++) {

我尝试使用Scanner在java中获取字符串输入,但在此之前,我获取整数输入。这是我的密码

import java.util.*;

class prc
{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        for(int i=1;i<=n;i++)
        {
            String str=input.nextLine();

            System.out.println(str);
        }

    }
}
import java.util.*;
中华人民共和国
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
int n=input.nextInt();
for(int i=1;i
nextLine()
读取下一个换行符之前的所有内容

但是,
nextInt()
只读取组成整数的字符,如果整数是行中的最后(或唯一)文本,则只剩下换行符

因此,您将在后续的
nextLine()
中得到一个空行。解决方案是在循环之前调用
nextLine()
一次(并放弃其结果)。

nextLine()
读取所有内容,包括下一个换行字符

但是,
nextInt()
只读取组成整数的字符,如果整数是行中的最后(或唯一)文本,则只剩下换行符

因此,您将在随后的
nextLine()
中得到一个空行。解决方案是在循环之前调用
nextLine()
一次(并放弃其结果)。

import java.util.*;
中华人民共和国
{
公共静态void main(字符串[]args)
{
字符串strs[];
扫描仪输入=新扫描仪(System.in);
int n=input.nextInt();
input.nextLine();
strs=新字符串[n];
对于(int i=1;i
导入java.util.*;
中华人民共和国
{
公共静态void main(字符串[]args)
{
字符串strs[];
扫描仪输入=新扫描仪(System.in);
int n=input.nextInt();
input.nextLine();
strs=新字符串[n];
对于(int i=1;i
每行旁边的注释中都提到了有关代码的信息

public static void main(String[] args) {
            int num1 = sc.nextInt();       //take int input
            double num2 = sc.nextDouble();    //take double input
            long num3 = sc.nextLong();      //take long input
            float num4 = sc.nextFloat();     //take float input
            sc.nextLine();      //next line will throw error if you don't use this line of code
            String str = sc.nextLine();      //take String input
    }

关于代码的信息在每行旁边的注释中提到

public static void main(String[] args) {
            int num1 = sc.nextInt();       //take int input
            double num2 = sc.nextDouble();    //take double input
            long num3 = sc.nextLong();      //take long input
            float num4 = sc.nextFloat();     //take float input
            sc.nextLine();      //next line will throw error if you don't use this line of code
            String str = sc.nextLine();      //take String input
    }

@MaxZoom:虽然这是更好的样式,但不会影响此程序的行为。请尝试更改for循环,如
for(int i=0;i@Neferseti顺便说一句,根据java约定,类名必须以大写字母-Prc开头case@MaxZoom:虽然这是更好的样式,但不会影响此程序的行为。请尝试更改for循环,如
for(int i=0;i@Neferseti顺便说一句,根据java约定,类名必须以大写字母-Prc开头,在这种情况下,请添加一些说明说明解决方案。请添加一些说明说明解决方案。