搜索一个数组的值,但只打印一审[更新] [英] Searching an array for a value but only printing the first instance[update]

查看:89
本文介绍了搜索一个数组的值,但只打印一审[更新]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个赛马程序将打印1日2次和第3名的马匹。我要成立的10匹马中的数组,然后从1-3到自己的位置添加随机数。获胜者是先马打15或更高。

我的问题是我的程序不能正常打印的第二和第三位的马匹。

[更新]我去掉了很多条件,并添加字符串变量来存储信息。它运行顺畅,但现在我不知道为什么程序不断重复印刷等和

下面是code,我更新了循环被打印获奖者:

 进口的java.util。*;
公共类赛马{
  公共静态无效的主要(字串[] ARG){
   扫描仪读卡器=新的扫描仪(System.in);
   INT范围= 3;
   INT赢= 15;
   最终诠释SIZE = 5;
   随机RAN =新的随机();
   布尔赢家= FALSE;
   布尔秒= FALSE;
   INT [] = arrRan新INT [SIZE]    System.out.print(去比赛preSS输入,使马跑!);    串readString = reader.nextLine();
    而(赢家!= TRUE){//永远循环下去,直到赢家
    //程序开始
      System.out.print(readString);
      如果(readString.equals()){//进入时是pressed         的for(int i = 0; I< arrRan.length;我++){//循环进入时,增加的位置是pressed
            arrRan [I] = arrRan [I] +(ran.nextInt(3)+ 1); //添加到随机数阵列
            的System.out.println(马+第(i + 1)+:+ arrRan [I]); //打印数组的内容
         }
      }//万一
      如果(reader.hasNextLine()){
         readString = reader.nextLine();
         的System.out.println(请preSS输入。);
      }
  的for(int i = 0; I< arrRan.length;我++){
     如果(arrRan [I]> = 15){//如果一个胜利者被发现
        第一=第一+马+(I + 1)+;
        赢家= TRUE;     }     //如果获奖者被发现,然后寻找一个亚军
     //如果没有第14位再搜索13,如果没有13那么12最低第二名将在第12位。
     如果(arrRan [Ⅰ] == 14){
        第二= +二马+(I + 1)+;
        secondPl = TRUE;     }
     如果(arrRan [I] = 14&安培;!&安培; arrRan [I] == 13){
        第二= +二马+(I + 1)+;
        secondPl = TRUE;
     }
     如果(arrRan [I] = 13安培;!&安培; arrRan [I] == 12){
        第二= +二马+(I + 1)+;
        secondPl = TRUE;
     }
     如果(arrRan [Ⅰ] == 13){
        第三=第三+马+(I + 1)+;
     }
  }
  如果(冠军==真){
  的System.out.println(第一);
  的System.out.println(第二);
  的System.out.println(第三);
  }    } //结束时
   } //关闭主
} //关闭类


解决方案

您可以从循环使用破退出语句。

I am making a horse race program that will print the 1st 2nd and 3rd place horses. I have to set up the 10 horses in an array and then add random number from 1-3 to their position. The winner is the first horse to hit 15 or beyond.

My problem is that my program does not properly print the 2nd and 3rd place horses.

[UPDATE] I removed the many conditions and added string variables to store the information. It runs smoother but now I dont know why the program keeps printing duplicates and such.

Here is the code, I updated the for loop that was printing the winners:

import java.util.*;
public class HorseRace{
  public static void main(String[ ] arg){
   Scanner reader = new Scanner(System.in);
   int range = 3;
   int win = 15;
   final int SIZE = 5;
   Random ran = new Random( );
   boolean winner = false;
   boolean second = false;
   int[ ] arrRan = new int[SIZE];

    System.out.print("Off to the races! Press enter to make the horses run.");

    String readString = reader.nextLine();
    while(winner!=true){//loop forever until winner
    //begin program
      System.out.print(readString);
      if(readString.equals("")){//when enter is pressed

         for(int i = 0; i<arrRan.length; i++){//loop that adds position when enter is pressed
            arrRan[i] = arrRan[i] + (ran.nextInt(3) + 1); //add to the array in random numbers
            System.out.println("Horse " + (i+1) + ": " + arrRan[i]);//print the contents of the array
         }       
      }//end if
      if(reader.hasNextLine()){
         readString = reader.nextLine();
         System.out.println("Please press enter.");
      }
  for(int i = 0; i<arrRan.length; i++){
     if(arrRan[i]>=15){//if a winner is found
        first = first + "Horse " + (i+1)+" ";
        winner = true;

     }

     //if winner is found then look for a 2nd place
     //if there is no position 14 then search for 13, if no 13 then 12. The lowest 2nd place will be in position 12. 
     if(arrRan[i]==14){
        second = second + "Horse " + (i+1)+" ";
        secondPl = true;

     }
     if(arrRan[i]!=14&&arrRan[i]==13){
        second = second + "Horse " + (i+1)+" ";
        secondPl = true;  
     }
     if(arrRan[i]!=13&&arrRan[i]==12){
        second = second + "Horse " + (i+1)+" ";
        secondPl = true;  
     }
     if(arrRan[i]==13){
        third = third + "Horse " + (i+1)+" "; 
     }         
  }
  if(winner==true){
  System.out.println(first);
  System.out.println(second);
  System.out.println(third);
  }      

    }//end while
   }//close main
}//close class

解决方案

You can exit from a for loop using the break statement.

这篇关于搜索一个数组的值,但只打印一审[更新]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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