gpt4 book ai didi

Java编程,寻找数组中某个键的连接

转载 作者:行者123 更新时间:2023-12-02 03:19:49 25 4
gpt4 key购买 nike

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package w01s03p;

import static java.sql.Types.NULL;
import java.util.Scanner;

/**
*
* @author WIN8
*/
public class task5 {

private static int hitLevel;
private static int attackLevel;
private static int defLevel;
private static int LEVEL;
private static int numberOfData;
private static String[] arrName;
private static int dataCounter;
private static int dataOfCounter;
private static int inputChoice;
private static double temp;

public static void main(String[] _args) {

int numberOfData = 3;
int i, temp = 0;
int inputChoice;
//declaration
String[] arrName;
double[] arrHP;
double[] arrAP;
double[] arrDP;
double[] hitLevel;
double[] attackLevel;
double[] defLevel;
double[] LEVEL;

//instantiation
arrName = new String[numberOfData];
arrHP = new double[numberOfData];
arrAP = new double[numberOfData];
arrDP = new double[numberOfData];
hitLevel = new double[numberOfData];
attackLevel = new double[numberOfData];
defLevel = new double[numberOfData];
LEVEL = new double[numberOfData];

do {
Scanner scanner = new Scanner(System.in);
System.out.println("--------------------");
System.out.println("Monsville Tournament");
System.out.println("--------------------");
System.out.println("1.List Monster");
System.out.println("2.Best By Hits Point");
System.out.println("3.Best By Attack Point");
System.out.println("4.Best By Defense Attack");
System.out.println("5.Search Profile");
System.out.println("6.Registration Monster");
System.out.println("0.Quit");
inputChoice = Keyin.inInt(" Your Choice: ");
switch (inputChoice) {
case 1:
System.out.println("List Monster : ");
if (arrName == null) {
System.out.println("Input Monster");
} else {
for (i = 0; i < 2; i++) {
System.out.println("Monster Name " + arrName[i]);
}
}
break;
case 2:
System.out.println("Daftar Best HP!");
if(arrHP != null){
for (i = 0; i < 2; i++) {
if (arrHP[i] < arrHP[i + 1]) {
temp = (int) arrHP[i + 1];
arrHP[i + 1] = arrHP[i];
arrHP[i] = temp;
}
System.out.println("HP (" + arrHP[i] + ") :" + arrName[i]);
}
}
break;
case 3:
System.out.println("Daftar Best AP!");
for (i = 0; i < 2; i++) {
if (arrAP[i] < arrAP[i + 1]) {
temp = (int) arrAP[i + 1];
arrAP[i + 1] = arrAP[i];
arrAP[i] = temp;
}
System.out.println("AP (" + arrAP[i] + ") :" + arrName[i]);
}
break;
case 4:
System.out.println("Daftar Best DP!");
for (i = 0; i < 2; i++) {
if (arrDP[i] < arrDP[i + 1]) {
temp = (int) arrDP[i + 1];
arrDP[i + 1] = arrDP[i];
arrDP[i] = temp;
}
System.out.println("DP (" + arrDP[i] + ") :" + arrName[i]);
}
break;
case 5:
System.out.println("Search Profile Monster");
break;
case 6:
for (i = 0; i < 2; i++) {
System.out.print("Monster Name: ");
arrName[i] = scanner.next();
System.out.print("hit point: ");
arrHP[i] = scanner.nextInt();
System.out.print("attack point: ");
arrAP[i] = scanner.nextInt();
System.out.print("defense point: ");
arrDP[i] = scanner.nextInt();
}
break;
default:
System.out.println("Invalid selection");
break;
}
} while (inputChoice != 0);

for (i = 0; i < 2; i++) {
if (arrHP[i] >= 100) {
hitLevel[i] = 3;
} else if ((arrHP[i] >= 50) && (arrHP[i] < 100)) {
hitLevel[i] = 2;
} else if (arrHP[i] < 50) {
hitLevel[i] = 1;
}
}
for (i = 0; i < 2; i++) {
if (arrAP[i] >= 20) {
attackLevel[i] = 3;
} else if ((arrAP[i] >= 10) && (arrAP[i] < 20)) {
attackLevel[i] = 2;
} else if (arrAP[i] < 10) {
attackLevel[i] = 1;
}
}
for (i = 0; i < 2; i++) {
if (arrDP[i] >= 15) {
defLevel[i] = 3;
} else if ((arrDP[i] >= 5) && (arrAP[i] < 15)) {
defLevel[i] = 2;
} else if (arrDP[i] < 5) {
defLevel[i] = 1;
}
}

for (i = 0; i < 2; i++) {
LEVEL[i] = +hitLevel[i] + attackLevel[i] + defLevel[i];
}

System.out.println("---------monster profile ---------\"");

for (i = 0; i < 2; i++) {
System.out.println("name : \"" + arrName[i] + "\"");
System.out.println("hit point :" + arrHP[i]);
System.out.println("attack point :" + arrAP[i]);
System.out.println("defense point:" + arrDP[i]);
System.out.println("level :" + LEVEL[i] + "(" + arrHP[i] + "/" + arrAP[i] + "/" + arrDP[i] + ")");
}

}

}

我希望在搜索查询中通过怪物的名称找到怪物,但我不知道如何通过怪物的名称搜索怪物的信息。怪物的信息分散在许多数组中。如果 inputChoice 为 5,那么它应该提示用户查找怪物的个人资料。

谢谢

最佳答案

One hint: do not create 5 different arrays that carry different properties of a "player" (like one for player names, one for player points, ..). Instead: create a class that represents one Player and give that class all the attributes that a player should have. And then create one array to hold player objects! – GhostCat

GhostCat 的完美回应。

但是如果你不听从他的建议,你就必须这样做:

  1. 从控制台获取输入,即:怪物的名称;
  2. 在怪物数组中搜索名称。
  3. 如果名称与输入相符,则保存该怪物的索引
  4. 使用该索引号获取该怪物的所有信息。

代码看起来类似于:

String searchQuery = scanner.next();
int indexFound = -1;
for(int x = 0; x < arrName.length; x++) {
if(arrName[x] != null && (arrName[x].toLowerCase().equals(searchQuery.toLowerCase()) || arrName[x].toLowerCase().contains(searchQuery.toLowerCase())) {
indexFound = x;
break;
}
}

if(indexFound != -1) {
System.out.println("Monster Found by the name of " + searchQuery)
System.out.println("name : \"" + arrName[indexFound] + "\"");
System.out.println("hit point :" + arrHP[indexFound]);
System.out.println("attack point :" + arrAP[indexFound]);
System.out.println("defense point:" + arrDP[indexFound]);
System.out.println("level :" + LEVEL[indexFound] + "(" + arrHP[indexFound] + "/" + arrAP[indexFound] + "/" + arrDP[indexFound] + ")");
} else {
System.out.println("Monster not found");
}

将该段代码放在 case 5: 下。

关于Java编程,寻找数组中某个键的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39766647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com