帮助java程序(数组) [英] Help with a java program (arrays)

查看:58
本文介绍了帮助java程序(数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的java程序需要帮助。我是java的新手,所以如果这是一个愚蠢的问题,请放轻松。


好​​的问题是


"你会完成本练习的两种方法。标题(以及描述每个标题的注释)如下:创建一个主要方法,询问用户数组的长度,使用readArray读取数组,并使用printArray打印生成的数组。


[PHP] public static void readArray(int a [],int length)

//此方法读取length值到数组a

public static void printArray(int a [],int length)

//此方法打印数组a的值[/ PHP ]


我正在使用的程序是:


[PHP] // Example9.java

import java.util.Scanner;

public class Example9 {

public static void main(String [] args){

Scanner sc = new扫描仪(System.in);

final int MAX = 5;

int [] a = new int [MAX];

int i = 0;

while(i< MAX){

System.out.print("输入数字:");

int n = sc.nextInt();

a [i] = n;

++ i;

}

System.out.println(" ;数据是:);

i = 0;

while(i< MAX){

System.out.println( a [i]);

++我;

}

}


} [ / PHP]

Hi, I need help with my java program. I am new with java, so go easy on me if this is a dumb question.

Ok the question is

"You will complete two methods for this exercise. The headers (and a comment that describes each one) are below: Create a main method that asks the user for the length of the array, uses readArray to read in the array, and uses printArray to print the resulting array."

[PHP]public static void readArray(int a[], int length)
//This method reads "length" values into array a

public static void printArray(int a[], int length)
//This method prints the values from array a[/PHP]

The program that I am working with is:

[PHP]// Example9.java
import java.util.Scanner;
public class Example9{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
final int MAX = 5;
int[] a = new int[MAX];
int i = 0;
while (i < MAX){
System.out.print("Enter a number: ");
int n = sc.nextInt();
a[i] = n;
++i;
}
System.out.println("The data are:");
i = 0;
while (i < MAX){
System.out.println(a[i]);
++i;
}
}

}[/PHP]

推荐答案

好的,首先:欢迎来到bytes.com!


这对我来说很棒你来这儿吧!


当你发帖时,请始终保持发布指南,当您发布代码时,请将其发布在 [code ] ... [/ code] 标签中(当您看似在这里完成了:-))。


好​​的,关于你的任务:你明白,你的计划是做什么的?例如,
OK, first of all: Welcome to bytes.com!

It''s great to have you here!

When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags (as you seemingly have done here :-) ).

OK, about your task: Do you understand, what the program you have does? For example, what does
展开 | 选择 | Wrap | 行号


[PHP] final int MAX = 5;

int [] a = new int [MAX]; [/ PHP]


感谢您的欢迎


好​​我明白我可以在java中输入一个共5次的数字程序。你发布的另外两个我很清楚这意味着什么。第一个while循环意味着我输入一个数字作为输入。我输入的号码是i。 " i" 是数组的一部分。当它通过循环时,它将递增。


第二个while循环意味着i小于最大值(即5)然后它会继续输出。
[PHP]final int MAX = 5;
int[] a = new int[MAX]; [/PHP]

Thanks for the nice welcome

Ok I understand that I can enter a number a total of 5 times into the java program. The other two you posted I sorta got a good idea of what it means. The first while loop means that I enter a number as the input. That number I enter is "i". "i" is a part of the array. While it goes through the loop, it will be incremented.

The second while loop means while "i" is less than the max (which is 5) then it will keep outputting.



好​​我明白我可以输入一个总数5次进入java程序。
Ok I understand that I can enter a number a total of 5 times into the java program.



正确。现在,您希望能够选择,输入的数量,对吗?所以,你必须在那里改变一些东西。

Correct. Now, you want to be able to choose, how many you enter, right? So, you''ll have to change something there.


第一个while循环意味着我输入一个数字作为输入。我输入的号码是i。 " i" 是数组的一部分。当它通过循环时,它将递增。
The first while loop means that I enter a number as the input. That number I enter is "i". "i" is a part of the array. While it goes through the loop, it will be incremented.



我不太确定 - 你可能意味着正确的事情,但你可能没有,所以我会在这里解释一下:

你有一个长度为5的数组。i是一个计数器(或 i ndex),首先设置为0。正如你所说的那样,它是递增的,但在此之前,它在数组中使用。

现在如何在数组中使用它?数组具有一定数量的值可以保存,并且每个值都有一个索引号。因此,如果 a 是一个数组,则 a [0] a 的第一个元素。所以, i 遍历 a 中元素的可能数字,以便每次访问不同的元素。

I''m not quite sure - you may mean the right thing, but you may not, so I''ll explain it here:
You have an array which has the length 5. "i" is a counter (or index), which is set to 0 at first. It is, as you said correctly, incremented, but before that, it is used in the array.
Now how is it used in the array? An array has a certain number of values it can save, and each of these values has an index number. So, if a is an array, then a[0] is the first element of a. So, i goes through the possible numbers for elements in a, so that you access a different one each time.


第二个while循环意味着i和i。小于最大值(即5)然后它将继续输出。
The second while loop means while "i" is less than the max (which is 5) then it will keep outputting.



正确,和i再次递增。 (顺便说一句,有一种特殊类型的循环叫做 for 循环用于那种任务,但是这可能仍然会在你的课程中得到解决。)


好​​的,你还需要知道什么?正如您在第一个循环中看到的那样,Scanner调用 sc 可以读取用户的输入。现在,有没有想要阅读用户输入的地方,但是到目前为止还没有?


问候,

Nepomuk

Correct, and "i" is incremented again. (By the way, there''s a special type of loop called the for loop for that sort of task, but that''s probably still going to be addressed in your course.)

OK, what else do you have to know? That Scanner called sc can, as you see in the first loop, read input from the user. Now, is there anywhere you would like to read user input, but don''t so far?

Greetings,
Nepomuk


这篇关于帮助java程序(数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆