我想知道如何在for循环的每一行之后获得输入。
System.out.println("How many days?");
int days = keyboard.nextInt();
int A = 0;
int B = 0;
int C = 0;
for(int i = 1; i < days; ++i(
{
System.out.println("Enter an input for day " +i + "of A B C:");
}
打印出来
Enter an input for day 1 of A B C:
Enter an input for day 2 of A B C:
取决于天好的价值。但是我需要在每天之后为A B C输入一个值,而不是一次只打印它们。我该如何继续这样做?
答案 0 :(得分:1)
使用Scanner keyboard
Scanner keyboard = new Scanner(System.in);
System.out.println("How many days?");
int days = keyboard.nextInt();
int A = 0;
int B = 0;
int C = 0;
for(int i = 1; i < days; ++i(
{
System.out.println("Enter an input for day " +i + "of A:");
A = keyboard.nextInt();
System.out.println("Enter an input for day " +i + "of B:");
B = keyboard.nextInt();
System.out.println("Enter an input for day " +i + "of C:");
C = keyboard.nextInt();
}
如果您想在一次通话中输入A,B和C,可以使用String abcInput = keyboard.nextLine();
,然后继续String
:
String abcInput = keyboard.nextLine();
String[] split = abcInput.split(" ");
if(split.length == 3){
//3 Values found so assign it to A,B and C
A = Integer.parseInt(split[0]);
B = Integer.parseInt(split[1]);
C = Integer.parseInt(split[2]);
}else{
System.out.println("Not enough input");
}
答案 1 :(得分:0)
我猜你做到了:
Scanner keyboard = new Scanner(System.in);
上面代码中的某处。
System.out.println("How many days?");
int days = keyboard.nextInt();
int A = 0;
int B = 0;
int C = 0;
for(int i = 1; i < days; ++i)
{
System.out.println("Enter an input for day " +i + "of A:");
int A = keyboard.nextInt();
}
将您的扫描仪代码移动到for循环内以进行输入。不确定A,B,C在上下文中是什么。但如果你需要所有n天的A,B和C,那么只需重复B和C的for循环中的两行。