Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果找不到项目并显示适当的消息,我应该在何处写入? public static void modifyBall(String[]hookPotentialArray,String[]namebarray,int[]ballweightary,int count){ 扫描仪键盘=新扫描仪(System.in); System.out.println(“请输入要修改的球的名称:”); 字符串名称=keyboard.nextLine(); for(int i=0;i_Java - Fatal编程技术网

Java 如果找不到项目并显示适当的消息,我应该在何处写入? public static void modifyBall(String[]hookPotentialArray,String[]namebarray,int[]ballweightary,int count){ 扫描仪键盘=新扫描仪(System.in); System.out.println(“请输入要修改的球的名称:”); 字符串名称=keyboard.nextLine(); for(int i=0;i

Java 如果找不到项目并显示适当的消息,我应该在何处写入? public static void modifyBall(String[]hookPotentialArray,String[]namebarray,int[]ballweightary,int count){ 扫描仪键盘=新扫描仪(System.in); System.out.println(“请输入要修改的球的名称:”); 字符串名称=keyboard.nextLine(); for(int i=0;i,java,Java,您可以创建一个布尔值,并在if语句中将其设置为true,这表示已找到该值。您可以在循环后检查布尔值是否为true,如果为true,则显示错误消息 例如: public static void modifyBall(String[] hookPotentialArray, String[] nameBallArray, int[] ballWeightArray, int count) { Scanner keyboard = new Scanner(System.in);

您可以创建一个布尔值,并在if语句中将其设置为true,这表示已找到该值。您可以在循环后检查布尔值是否为true,如果为true,则显示错误消息

例如:

public static void modifyBall(String[] hookPotentialArray, String[] nameBallArray, int[] ballWeightArray, int count) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter the name of the ball you would like to modify: ");
        String name = keyboard.nextLine();

        for (int i = 0; i < count; i++) {
            if (name.compareToIgnoreCase(nameBallArray[i]) == 0) {
                System.out.println("Please enter a new name for the ball: ");
                String ballName = keyboard.nextLine();
                System.out.println("Please enter a new weight for the ball: ");
                int ballWeight = keyboard.nextInt();
                System.out.println("Please enter a new hook potential for the ball: ");
                String hookPotential = keyboard.next();

                nameBallArray[i] = ballName;
                ballWeightArray[i] = ballWeight;
                hookPotentialArray[i] = hookPotential;

                System.out.println("The ball list has been updated.");
                System.out.println("");


            }

        }
public static void modifyBall(String[]hookPotentialArray,String[]namebarray,int[]ballweightary,int count){
boolean found=false;//是否找到它的布尔值
扫描仪键盘=新扫描仪(System.in);
System.out.println(“请输入要修改的球的名称:”);
字符串名称=keyboard.nextLine();
for(int i=0;i

我还在if语句的末尾添加了一个break,以在找到项时中断循环(如果已找到项,则继续进行没有意义)。

在循环之前将标志变量设置为0。在if内部将其设置为1。在循环之后检查它是否仍然为零。然后打印相应的消息,表明未找到项

public static void modifyBall(String[] hookPotentialArray, String[] nameBallArray, int[] ballWeightArray, int count) {
boolean found = false; //boolean for if it's found or not
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the name of the ball you would like to modify: ");
String name = keyboard.nextLine();

for (int i = 0; i < count; i++) {
    if (name.compareToIgnoreCase(nameBallArray[i]) == 0) {
        found = true; //set boolean if found
        System.out.println("Please enter a new name for the ball: ");
        String ballName = keyboard.nextLine();
        System.out.println("Please enter a new weight for the ball: ");
        int ballWeight = keyboard.nextInt();
        System.out.println("Please enter a new hook potential for the ball: ");
        String hookPotential = keyboard.next();

        nameBallArray[i] = ballName;
        ballWeightArray[i] = ballWeight;
        hookPotentialArray[i] = hookPotential;

        System.out.println("The ball list has been updated.");
        System.out.println("");
        break; //break out of loop if item found
    }
}
//check if boolean is false then print error message
if (!found)
{
    System.out.println("There was a problem finding your item.");
}
试试这个:

Boolean flag  = false; // before loop

flag = true; //inside if

// After loop
if(!flag)
    System.out.println("Not Found");
public static void modifyBall(String[]hookPotentialArray,String[]namebarray,int[]ballweightary,int count){
扫描仪键盘=新扫描仪(System.in);
System.out.println(“请输入要修改的球的名称:”);
字符串名称=keyboard.nextLine();
for(int i=0;i
您应该重构以分离查找/未找到球(或未找到球)的循环,并对球执行任何需要执行的操作(如果未找到球,则执行错误)这有助于提高可读性,因为更多的行缩进更少,并且有助于传达您的意图,因为循环中的大多数代码无论如何只执行一次

    public static void modifyBall(String[] hookPotentialArray, String[] nameBallArray, int[] ballWeightArray, int count) {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Please enter the name of the ball you would like to modify: ");
            String name = keyboard.nextLine();

            for (int i = 0; i < count; i++) {
                if (name.compareToIgnoreCase(nameBallArray[i]) == 0) {
                    System.out.println("Please enter a new name for the ball: ");
                    String ballName = keyboard.nextLine();
                    System.out.println("Please enter a new weight for the ball: ");
                    int ballWeight = keyboard.nextInt();
                    System.out.println("Please enter a new hook potential for the ball: ");
                    String hookPotential = keyboard.next();

                    nameBallArray[i] = ballName;
                    ballWeightArray[i] = ballWeight;
                    hookPotentialArray[i] = hookPotential;

                    System.out.println("The ball list has been updated.");
                    System.out.println("");
                    return;
                }

            }

           System.out.println("ball not found");

}
int-ballIndex=-1;
for(int i=0;i=0){
//找到-使用ballIndex做任何事情
}否则{
//未找到-显示错误
}
int ballIndex = -1;
for (int i = 0; i < count; i++) {
    if (name.compareToIgnoreCase(nameBallArray[i]) == 0) {
       ballIndex = nameBallArray[i];
       break;
    }
}
if (ballIndex >= 0) {
  // found - do everything using ballIndex
} else {
  // not found - display error
}