Java 添加。拆分到我的代码中
我正在用Java创建一个带有数组的控制台程序,我需要在一行中以以下格式输入所有代码,最多100次 汽车品牌名称:汽车型号名称:汽车型号税:汽车型号价格 这是两个字符串和两个int变量,我已经编写了代码,但我不知道如何使用.split将正确的信息输入到相应的变量中 这是我的代码:Java 添加。拆分到我的代码中,java,console-application,Java,Console Application,我正在用Java创建一个带有数组的控制台程序,我需要在一行中以以下格式输入所有代码,最多100次 汽车品牌名称:汽车型号名称:汽车型号税:汽车型号价格 这是两个字符串和两个int变量,我已经编写了代码,但我不知道如何使用.split将正确的信息输入到相应的变量中 这是我的代码: public class Main { public static void main (String[] args) { //Arrays declared St
public class Main {
public static void main (String[] args)
{
//Arrays declared
String[] cars = new String[20];
String[] car_model_name = new String[20];
int[] car_model_tax = new int[20];
String[] car_make_name = new String[20];
int[] car_model_price = new int[20];
Scanner scan = new Scanner (System.in);
//Loop??
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("quit", "QUIT", "Quit")) {
}
}
break;
for (int x = 0; x < cars.length; x++){
System.out.println("Enter details, separating each with a ':' ");
cars[x] = System.console().readLine();
}
}
}
公共类主{
公共静态void main(字符串[]args)
{
//声明的数组
字符串[]cars=新字符串[20];
字符串[]汽车型号名称=新字符串[20];
int[]车型税=新int[20];
字符串[]汽车品牌名称=新字符串[20];
int[]车型价格=新int[20];
扫描仪扫描=新扫描仪(System.in);
//环路??
while(scanner.hasNextLine()){
字符串行=scanner.nextLine();
if(行等于(“退出”、“退出”、“退出”)){
}
}
打破
对于(int x=0;x
首先,如果您从控制台读取输入,并且无法控制用户的输入长度,您可能更喜欢而不是使用数组来存储信息
其次,如果需要,您可能希望使用拆分和转换它
System.out.println("Enter details, separating each with a ':' ");
Scanner scan = new Scanner(System.in);
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (line.equals("quit")) {
break;
}
final String[] split = line.split(":");
// handle it
}
第三,一本好书可以极大地提高您的编程水平。如果您没有编程经验,我建议您使用Head First Java。否则,我建议使用Java进行思考 这个问题并不完全清楚,但问题可能不仅仅是“如何使用拆分”,还有(或仅仅是)“当拆分实际返回字符串数组时如何提取不同的数据类型”。您的问题是“一般如何使用拆分”,还是问如何从同一字符串中提取不同的数据类型(字符串和整数)?