public class A6{
public static void main(String[] args){
//String personInfo[][]=new String[][] {
String personInfo[][]={
{"Anderson", "Varejao", "1125"},
{"Giorgi", "Tevzadze", "1125"},
{"Will", "Cherry", "1225"},
{"Will", "Iams", "12245"},
{"Lebron", "James", "6025"},
{"Kevin", "Love", "2525"},
{"Kyrie", "Livings", "454"},
{"Kyrie", "Botti", "4544"},
{"Chris", "Mauer", "425"},
{"Mot", "Daniel", "125"},
{"Viktor", "Muller", "145"},
{"Kiamran", "Chris", "1405"},
{"Zenia", "Vaitehovic", "1025"},
{"Marija", "Grabauskaite","1471"},
{"Milda", "Grabauskaite", "1000"},
{"Dion", "Waiters", "625" },
{"Dion", "Malborg", "6250" }
};
System.out.println("Peoples on the list:");
for(int i = 0; i < personInfo.length; i++) {
for(int j = 0; j < personInfo[i].length; j++) {
System.out.print(" ");
System.out.print(personInfo[i][j]);
}
System.out.println();
}
System.out.println("----------------------");
//---------v XXX - how many people have the first name XXX----------------
String b = "v";
if(args[0].equals(b))
{
System.out.println("Persons with entered name:");
for(int i = 0; i < personInfo.length; i++) {
for(int j = 0; j < personInfo[i].length; j++) {
if(personInfo[i][j].equals(args[1]))
{
System.out.println(args[1]);
}
}
}
System.out.println();
}
//------vp XXX YYY - what is the salary of the person XXX YYY------
}
}
我开始做新的自我控制练习。这里我有二维String数组,它包含这些信息{&#34; name&#34;,&#34; surname&#34;,&#34; salary&#34;}。我已经完成了我的练习的一部分,它计算了我输入命令行的名字的人数。现在我需要编写一个代码例如:我进入命令行Anderson Varejao并希望得到结果1125.换句话说,我输入一个姓名,并希望从personInfo String数组获得某个人的工资。我怎样才能达到并领取薪水?
答案 0 :(得分:0)
for(int i = 0; i < personInfo.length; i++) {
if(personInfo[i][0].equals(args[1]) &&
personInfo[i][1].equals(args[2]) {
System.out.println(personInfo[i][2]);
}
}
System.out.println();
答案 1 :(得分:0)
换句话说,我输入一个姓名,并希望得到一份工资 personInfo字符串数组中的某些人。我怎样才能到达并接受 一个人的薪水?
名称存储在第一个索引中,工资存储在第二个维度的第三个索引中
因此,您可以定义依赖于此的findSalary()
方法:
public String findSalary(String personInfo[][], String name)
for(int i = 0; i < personInfo.length; i++) {
if (name.equals(personInfo[i][0])){
return personInfo[i][2];
}
}
return null;
}
并调用:
String personInfo[][]={ ...};
String salary = findSalary(personInfo, "john");
请注意,使用String
来表示数字事物可能不是最好的事情。
答案 2 :(得分:0)
按空格分割搜索条件并将结果存储在数组中,例如:result [2]
检查[i] [j] =结果[0] firstpart&amp;&amp; checka [i] [j + 1] =结果[1]后半部分然后打印预期值
答案 3 :(得分:0)
如果您仍想使用这种方式,那么这就是您想要的
for (int i = 0; i < personInfo.length; i++) {
if(personInfo[i][0] == args[0] && personInfo[i][1] == args[1]) {
System.out.println(personInfo[i][2]);
}
}
注意输入以空格分隔的名称和姓氏。
但是如果你想要一个好的解决方案,你应该使用面向对象的强大功能
创建一个可以保存人员信息的课程
喜欢:
class Person {
String name = "";
String surName = "";
String salary = "";
//setters and getters here
}
答案 4 :(得分:0)
关于如何在数组中定位元素,因为工资总是在personInfo [2]中,我只是一次迭代一行数据并检查firstName是否对应于peopleinfo中的元素[ 0]&amp;&amp; lName对应于peopleinfo [1]。 看起来像这样:
static Scanner sc = new Scanner(System.in);
static String personInfo[][] = {
{"Anderson", "Varejao", "1125"},
{"Giorgi", "Tevzadze", "1125"},
{"Will", "Cherry", "1225"},
{"Will", "Iams", "12245"},
{"Lebron", "James", "6025"},
{"Kevin", "Love", "2525"},
{"Kyrie", "Livings", "454"},
{"Kyrie", "Botti", "4544"},
{"Chris", "Mauer", "425"},
{"Mot", "Daniel", "125"},
{"Viktor", "Muller", "145"},
{"Kiamran", "Chris", "1405"},
{"Zenia", "Vaitehovic", "1025"},
{"Marija", "Grabauskaite", "1471"},
{"Milda", "Grabauskaite", "1000"},
{"Dion", "Waiters", "625"},
{"Dion", "Malborg", "6250"}
};
public static void main(String[] args) {
System.out.print("Enter First Name: ");
String fName = sc.nextLine();
System.out.print("Enter Last Name: ");
String lName = sc.nextLine();
System.out.println(fName + " " + lName + "' Salary: " + salaryLookUp(personInfo, fName, lName));
}
static String salaryLookUp(String[][] array, String fName, String lName) {
String salary = "";
for (String[] row : personInfo) {
if (row[0].equalsIgnoreCase(fName) && row[1].equalsIgnoreCase(lName)) {
salary = row[2];
break;
} else {
salary = "Not Found";
}
}
return salary;
}
答案 5 :(得分:-1)
这是用C#编写的,希望它有用...
string[,] personInfo = new string[,] {
{"Anderson", "Varejao", "1125"},
{"Giorgi", "Tevzadze", "1125"},
{"Will", "Cherry", "1225"},
{"Will", "Iams", "12245"},
{"Lebron", "James", "6025"},
{"Kevin", "Love", "2525"},
{"Kyrie", "Livings", "454"},
{"Kyrie", "Botti", "4544"},
{"Chris", "Mauer", "425"},
{"Mot", "Daniel", "125"},
{"Viktor", "Muller", "145"},
{"Kiamran", "Chris", "1405"},
{"Zenia", "Vaitehovic", "1025"},
{"Marija", "Grabauskaite","1471"},
{"Milda", "Grabauskaite", "1000"},
{"Dion", "Waiters", "625" },
{"Dion", "Malborg", "6250" }
};
string FirstName = "Milda";
string LastName = "Grabauskaite";
for (int i = 0; i < personInfo.GetLength(0); i++)
{
if(personInfo[i,0] == FirstName && personInfo[i,1] == LastName)
{
Console.WriteLine(FirstName + " " + LastName + " Salary Is: " + personInfo[i,2]);
}
}