我正在尝试制作一个要求多个输入的脚本,然后将它们打印出来。
例如:
您提供了以下输入:
1)5
2)3
3)4
下面你可以看到我有多远。很好地询问所有输入,但我无法弄清楚如何在输入下面打印它们。
import java.util.Scanner;
public class Inputs {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
System.out.println("How many inputs?");
int inp=read.nextInt();
for (int i=1;i<=inp;i++){
System.out.print("What was " + i + ". input? ");
int print=read.nextInt();
System.out.println(print);
}
}
}
答案 0 :(得分:1)
public class Inputs {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
System.out.println("How many inputs?");
int inp=read.nextInt();
int[] answers = new int[inp];
for (int i=1;i<=inp;i++){
System.out.print("What was " + i + ". input? ");
int print=read.nextInt();
System.out.println(print);
answers[i] = print;
}
int index = 0;
for(int a : answers){
System.out.println( index + ")" + " " + a);
index ++;
}
}
}
你可以自己编码清理它作为一种做法=)
答案 1 :(得分:1)
您可以使用集合(我使用列表)来保护您的输入;
public class Inputs {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
System.out.println("How many inputs?");
int inp=read.nextInt();
List<Integer> numbers = new ArrayList<Integer>();
for (int i=1;i<=inp;i++){
System.out.print("What was " + i + ". input? ");
int print=read.nextInt();
numbers.add(print);
System.out.println(print);
}
System.out.println("Inputs are :");
for (Integer integer : numbers) {
System.out.println(integer);
}
}
}
输出是;
How many inputs?
3
What was 1. input? 5
5
What was 2. input? 3
3
What was 3. input? 4
4
Inputs are :
5
3
4
答案 2 :(得分:0)
您需要创建一个整数数组供以后使用。
int array = new int[inp];
答案 3 :(得分:0)
您需要存储输入并稍后打印
import java.util.Scanner;
public class Inputs {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
System.out.println("How many inputs?");
int inp=read.nextInt();
int[] keepInputsHere=new int[inp];
for (int i=0;i<inp;i++){
System.out.print("What was " + i + ". input? ");
int readInt=read.nextInt();
keepInputsHere[i]=readInt;
}
System.out.print("Print results: ");
for (int i=0;i<inp;i++){
System.out.print( keepInputsHere[i]);
}
}
}
答案 4 :(得分:0)
别忘了关闭扫描仪并将登录信息分成不同的功能:
public class Inputs {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
System.out.println("How many inputs?");
int inp = read.nextInt();
Print(ReadInput(inp,read));
}
private static int[] ReadInput(int questionsNo, Scanner sc) {
int[] inputs = new int[questionsNo];
for (int i = 0; i < questionsNo; i++) {
System.out.print("What was " + i + ". input? ");
int readInt = sc.nextInt();
inputs[i] = readInt;
}
sc.close();
return inputs;
}
private static void Print(int[] inputs) {
System.out.println("Print results: ");
for (int i = 0; i < inputs.length; i++) {
System.out.println( i + ")" + " " + inputs[i]);
}
}
}