我必须编写一个Java程序
,用户在其中输入学生总数,所以我编写了以下代码:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int numReaders = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of magazin readers:");
numReaders = scan.nextInt();
现在,添加学生总数后,我们应该将他们的名字添加到一个数组中:
//Creating an array of names, where the length is the total number entered by the user
String[] nameStr = new String[numReaders];
int[] ages = new int[numReaders];
for(int i=0; i<numReaders; i++)
{
Scanner n = new Scanner(System.in);
System.out.println("Enter the name of reader: "+i);
nameStr[i] = n.next();
}
之后,我们要相应地添加每个名字的年龄,所以我做了这部分代码:
for(int i=0; i<numReaders; i++)
{
Scanner a = new Scanner(System.in);
System.out.println("Enter the age of reader: "+i);
ages[i] = a.nextInt();
}
//Display the results
System.out.println("Number of readers is: "+numReaders);
for (int i=0; i<numReaders; i++)
{
System.out.println("The name of reader "+i+" is "+nameStr[i]+ " and his age is "+ages[i]);
}
编写此代码后,我使用 Ideone 和命令提示符对其进行了测试,它工作正常:
现在,我需要根据用户的选择调用方法: 如果他输入“a”,则应调用一个方法来指定最大学生的姓名和年龄。 如果他输入“b”,则调用一个方法来查看有多少学生具有用户指定的年龄;如果他输入“c”,则调用一个函数来计算所有学生的平均年龄。
我是方法新手,所以我不知道如何将数组添加到方法中并创建语句。
完整代码如下:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int numReaders = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of magazin readers:");
numReaders = scan.nextInt();
//Creating an array of names, where the length is the total number entered by the user
String[] nameStr = new String[numReaders];
int[] ages = new int[numReaders];
for(int i=0; i<numReaders; i++)
{
Scanner n = new Scanner(System.in);
System.out.println("Enter the name of reader: "+i);
nameStr[i] = n.next();
}
for(int i=0; i<numReaders; i++)
{
Scanner a = new Scanner(System.in);
System.out.println("Enter the age of reader: "+i);
ages[i] = a.nextInt();
}
//Display the results
System.out.println("Number of readers is: "+numReaders);
for (int i=0; i<numReaders; i++)
{
System.out.println("The name of reader "+i+" is "+nameStr[i]+ " and his age is "+ages[i]);
}
//Choosing a statistic
//if a:
System.out.println("Please choose a, b or C:");
Scanner stat = new Scanner(System.in);
char X;
X = stat.next().charAt(0);
if(X=='a')
System.out.println(X+X);
else if(X=='b')
//System.out.println(X);
//Scanner newAge = new Scanner(System.in);
//int ageToSearchFor = newAge.nextInt();
//maxAge(ageToSearchFor);
else
System.out.println(X);
}
}
最佳答案
是的,首先您的用户输入 input
,例如'a
',所以我们就这样吧:
首先,您需要创建显示年龄最大的学生姓名的方法,因此我们将其命名为“getOldestStudent
” ' - 命名方法时,这是典型的命名约定,从小写开始,然后为每个新单词变为大写 - 尝试使它们尽可能直观。
创建方法签名时,您需要为其提供可见性以及它将返回的内容。在这种情况下,由于您只使用一个类,我们将给它私有(private),因此它仅对该类可见。
现在我们需要返回 2 个东西,所以我们可以将它们放入 string
或者将它们放入 array
,这是我推荐的,所以我们将 return
一个array
。但是,您想要input
一个array
进行搜索,因此将其放在括号中为 parameters
(或arguments
)。因此我们的method signature
如下:
private String[] getOldestStudent(String[] students, int[] ages)
然后在这个方法中,您可以简单地执行查找年龄最大的学生所需的代码,将他们的姓名和年龄添加到数组中,然后 return
这个。
需要更多帮助,只需发表评论即可。
顺便说一句,您最好创建一个 ' Student
' 对象,然后给这个对象一个 'name' 属性和 'age' 属性,然后简单地创建一个 array
的students
和getters
和setters
(或 accessors
和 mutators
)这些属性。
关于java - 传递数组作为参数并进行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35081463/