基本的Java for循环和数据存储

huangapple 未分类评论41阅读模式
英文:

Basic for loop in java and data storage

问题

我对编程非常陌生有一个简单的问题我想在一个for循环内输入日期月份和年份输入完成后我想同时显示所有输入的值怎么做呢请帮忙
我已经附上了代码

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            int day = in.nextInt();
            String month = in.next();
            int year = in.nextInt();
        }
    }
    
    // 需要显示来自for循环的所有内容
    // 假设n的值为3
    // 我会提供3个输入
    // 10 一月 1998
    // 11 十一月 2000
    // 12 十二月 1995
    // 我想同时打印出所有内容


请帮助我解决这个问题
英文:

I am very new to coding and I have a simple question. I want to input day, month and year inside a for loop and after inputting it I want to display all the inputted values on the same time. how to do it.kindly help me.
i have attached the code below.

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i=0;i&lt;n;i++) {
	int day = in.nextInt();
	String month = in.next();
	int year = in.nextInt();
}}

//need to display the entire content from the for loop
//suppose if the n value is 3 
//i will be giving 3 inputs
//10 jan 1998
//11 nov 2000
//12 dec 1995
//i want to print all at the same time

Kindly help me with it.

答案1

得分: 0

如果我正确理解了您的问题,您只想打印出您的输入内容,只需在循环中添加以下内容:

System.out.println(String.format("%d %s %d", day, month, year));

或者,虽然不太美观(至少在我看来是这样的):

System.out.println(day + " " + day + " " + month + " " + year);

编辑

如您所指示,您希打印出所有内容。为此,您可以将它们全部保存在列表或数组中,例如:

在循环之前:

String[] dates = new String[n];

在循环中:

dates[i] = String.format("%d %s %d", day, month, year);

然后继续添加另一个循环以打印数组的内容:

for (String date : dates) {
    System.out.println(date);
}
英文:

If I understood your question correctly and you just want to print your inputs, just add the following to the loop:

System.out.println(String.format(&quot;%d %s %d&quot;, day, month, year));

or otherwise, but not as pretty (at least in my opinion):

System.out.println(day + &quot; &quot; + day + &quot; &quot; + month + &quot; &quot; + year);

EDIT

As indicated, you want to print them all at the same time. To do so, you can just save them all in a list or an array for example like the following:
Before the loop:

String[] dates = new String[n];

In the loop:

dates[i] = String.format(&quot;%d %s %d&quot;, day, month, year);

And then go ahead and insert another loop to print the content of the array:

for(String date: dates){
   System.out.println(dates[i]);
}

答案2

得分: 0

根据我收集的信息,您希望设置一个数字,表示用户需要输入多少个日期,然后获取该数量的日期,并在用户输入后打印出这些日期。

以下是能够实现这一目标的基本代码:

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int numOfInputs = 3;  // 您想要输入的不同日期的数量
    int day[] = new int[numOfInputs];  // 声明整数数组day,并将数组大小设置为numOfInputs的值
    String month[] = new String[numOfInputs];  // 声明字符串数组month,并将数组大小设置为numOfInputs的值
    int year[] = new int[numOfInputs];  // 声明整数数组year,并将数组大小设置为numOfInputs的值
    
    // 获取输入
    for(int i=0;i<numOfInputs;i++) {
        System.out.println("请输入日期的天数");
        day[i] = sc.nextInt();
        System.out.println("请输入日期的月份");
        month[i] = sc.next();
        System.out.println("请输入日期的年份");
        year[i] = sc.nextInt();
    }
    // 打印内容
    for(int i=0;i<numOfInputs;i++) {
        System.out.println(day[i] + " " + month[i] + " " + year[i]);
    }
    // 关闭扫描器
    sc.close();
}

如果这不回答了您的问题,或者您需要任何澄清,请告诉我。

英文:

From what I have gathered, you are looking to set a number on how many dates you'd like the user to enter, take in that number of dates and then print out the dates after the user input.

Here is some basic code that will do that for you

public static void main(String args[]) {
	Scanner sc = new Scanner(System.in);
	int numOfInputs = 3;	//How many separate dates you would like to enter
	int day[] = new int[numOfInputs];	//declaring an integer array day and setting the array size to the value of numOfInputs
	String month[] = new String[numOfInputs];	//declaring a string array month and setting the array size to the value of numOfInputs
	int year[] = new int[numOfInputs];	//declaring an integer array year and setting the array size to the value of numOfInputs
	
	//get inputs
	for(int i=0;i&lt;numOfInputs;i++) {
		System.out.println(&quot;Please enter a day&quot;);
		day[i] = sc.nextInt();
		System.out.println(&quot;Please enter a month&quot;);
		month[i] = sc.next();
		System.out.println(&quot;Please enter a year&quot;);
		year[i] = sc.nextInt();
	}
	//print content
	for(int i=0;i&lt;numOfInputs;i++) {
		System.out.println(day[i] + &quot; &quot; + month[i] + &quot; &quot; + year[i]);
	}
	//close scanner
	sc.close();
}

Let me know if this doesn't answer your question or if you need any clarification.

huangapple
  • 本文由 发表于 2020年4月4日 04:23:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/61019852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定