gpt4 book ai didi

java - 跳过输入字符串

转载 作者:行者123 更新时间:2023-12-02 00:52:36 26 4
gpt4 key购买 nike

int id;
float grade;
String name;
Scanner z= new Scanner(System.in);
System.out.println("Give the id:\n");
id=z.nextInt();
System.out.println("your id is :"+id+"\n");

System.out.println("Give the name:");
name=z.nextLine();
System.out.println("your name is :"+name);

System.out.println("Give the grade:\n");
grade=z.nextFloat();

问题是这样的。它输入整数,但当涉及到字符串时,它会打印“给出名称”,但它不会等到我输入某些内容,它会跳到下一条指令。

这是为什么?

最佳答案

您已使用name=z.nextLine(),因此出现此类行为,请将其替换为name=z.next()。下面是编辑后的代码:

int id;
float grade;
String name;

Scanner z= new Scanner(System.in);
System.out.println("Give the id:\n");
id=z.nextInt();
System.out.println("your id is :"+id+"\n");

System.out.println("Give the name:");
name=z.next();
System.out.println("your name is :"+name);

System.out.println("Give the grade:\n");
grade=z.nextFloat();

关于java - 跳过输入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19487311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com