Java 如何使用循环将键盘输入值放入数组?
在这里,我试图将用户的两个键盘输入输入到单个数组索引位置Java 如何使用循环将键盘输入值放入数组?,java,Java,在这里,我试图将用户的两个键盘输入输入到单个数组索引位置 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tour;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tour;
import java.util.Scanner;
import tour.City;
/**
*
* @author dp
*/
public class Tour {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
City[] city = new City[9];
Scanner in = new Scanner(System.in);
for(int i=0;i<city.length;i++)
{
int no = in.nextInt();
String name = in.nextLine();
city[i]= new City(no,name);
}
}
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包价旅游
导入java.util.Scanner;
进口旅游城市;
/**
*
*@author-dp
*/
公务舱旅游{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
城市[]城市=新城[9];
扫描仪输入=新扫描仪(系统输入);
对于(int i=0;i,因为12
和NY
在不同的行上,当您这样做时
String name = in.nextLine();
返回的字符串
为空。这是因为扫描仪
的“读取点”位于12
之后,但位于其后的行尾标记之前
您可以通过添加另一个nextLine
,并删除其结果来解决此问题:
in.nextLine(); // Skip to end-of-line after the number
String name = in.nextLine();
您正在使用nextInt()
和nextLine()
方法读取用户输入,这些方法读取next
可用令牌,因此现有代码就是这样工作的:
- 它使用
nextInt()
读取一个数字,并将其分配给no
- 然后用户点击
return
,控件读取一个空行(因为下一行是空的)并将其分配给name
City
对象使用12
作为no
和
作为name
创建。For循环开始第二次执行
- 此时,用户已键入
NY
并点击return
- 由于它期望令牌为int(通过调用
nextInt()
),因此它失败并引发异常
如果希望控件分别读取两个输入(并等待用户点击返回),请使用:
只需要读取整型到最后一行
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tour;
import java.util.Scanner;
import tour.City;
/**
*
* @author dp
*/
public class Tour {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
City[] city = new City[9];
Scanner in = new Scanner(System.in);
for(int i=0;i<city.length;i++)
{
int no = in.nextInt();
in.nextLine();//read to the end of line
String name = in.nextLine();
city[i]= new City(no,name);
}
}
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包价旅游
导入java.util.Scanner;
进口旅游城市;
/**
*
*@author-dp
*/
公务舱旅游{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
城市[]城市=新城[9];
扫描仪输入=新扫描仪(系统输入);
对于(int i=0;i
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tour;
import java.util.Scanner;
import tour.City;
/**
*
* @author dp
*/
public class Tour {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
City[] city = new City[9];
Scanner in = new Scanner(System.in);
for(int i=0;i<city.length;i++)
{
int no = in.nextInt();
in.nextLine();//read to the end of line
String name = in.nextLine();
city[i]= new City(no,name);
}
}
}