使用用户输入搜索数组并打印具有该值的所有元素

发布于 2025-01-19 21:37:16 字数 1885 浏览 0 评论 0原文

我正在尝试在数组中搜索某个值并在 java.lang.String 中显示包含该值的所有元素。例如,用户选择数字 5,数组中存储了 3 个具有该值的项目,因此所有 3 个项目都会被打印出来。我的代码编译正常,但是当我运行它时,它会跳转到 else 语句,即使我输入的是数组中的数字。 任何帮助都会很棒。这是我的代码:

                case 3:
                //display all elements with the same state
                //get number user wishes to search for
                
                boolean found = false;
                 Scanner input = new Scanner(System.in);
                 System.out.print("Please enter the number you wish to search for");

                 //read user input
                 num = input.nextInt();

                 //traverse array
                 int k = 0;
                 for(k=0; k < myMonths.length; k++){
                     if(myMonths[index] == num){
                         found = true;
                         break;}
                     if(found){System.out.println(k);}

                     else{System.out.println("not found");}
                 }
                break;

这是数组:

            //Menu loop
            int myMonths[] = new int[5];
            int index = 0;
            int num;
            while(choice !=6){


                switch (choice){
                case 1:
                //int n = number of projects
                int n = 1;
                Scanner sc = new Scanner(System.in);
                System.out.println("How many months was your project?");


                for(int i=0; i<1; i++){
                    int a = sc.nextInt();

                    //if months is lesser than 2/greater than 12
                    if((a < 2) || (a > 12)){
                        System.out.println("Please enter an amount between 2 and 12 months");}

                   //if months is between 2 and 12 add it to the array

                    else{myMonths[index++] = a;} }
                break;

I'm trying to search an array for a certain value and display all the elements which contain this value in java. For example, the user selects the number 5, there are 3 projects stored in the array with this value so all 3 of those will be printed out. My code is compiling ok, but when I run it, it jumps to the else statement, even if I am inputting a number that is in the array..
Any help would be amazing. Here's my code:

                case 3:
                //display all elements with the same state
                //get number user wishes to search for
                
                boolean found = false;
                 Scanner input = new Scanner(System.in);
                 System.out.print("Please enter the number you wish to search for");

                 //read user input
                 num = input.nextInt();

                 //traverse array
                 int k = 0;
                 for(k=0; k < myMonths.length; k++){
                     if(myMonths[index] == num){
                         found = true;
                         break;}
                     if(found){System.out.println(k);}

                     else{System.out.println("not found");}
                 }
                break;

Here's the array:

            //Menu loop
            int myMonths[] = new int[5];
            int index = 0;
            int num;
            while(choice !=6){


                switch (choice){
                case 1:
                //int n = number of projects
                int n = 1;
                Scanner sc = new Scanner(System.in);
                System.out.println("How many months was your project?");


                for(int i=0; i<1; i++){
                    int a = sc.nextInt();

                    //if months is lesser than 2/greater than 12
                    if((a < 2) || (a > 12)){
                        System.out.println("Please enter an amount between 2 and 12 months");}

                   //if months is between 2 and 12 add it to the array

                    else{myMonths[index++] = a;} }
                break;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2025-01-26 21:37:16

首先,您应该使用k而不是index。其次,您不应该在每次迭代中打印“未找到”(或对此进行测试)。您应该尽可能限制可变范围。您还提到想要找到所有匹配索引,以便您不应该过早终止循环;我想你想要类似的东西

//assume the value isn't present.
boolean found = false;
//read user input
int num = input.nextInt();

//traverse array
for(int k = 0; k < myMonths.length; k++) {
    if (myMonths[k] == num){
        found = true;
        System.out.println(k); // don't break or the loop ends early.
    }
}
//this test can only be done after you iterate **all** values.
if (!found) {
    System.out.println("not found");
}

First, you should be using k instead of index. Second, you shouldn't be printing "not found" (or testing for that) on every iteration. You should limit variable scope as much as practical. And you also mention wanting to find all matching indices so you shouldn't terminate the loop prematurely; I think you wanted something like

//assume the value isn't present.
boolean found = false;
//read user input
int num = input.nextInt();

//traverse array
for(int k = 0; k < myMonths.length; k++) {
    if (myMonths[k] == num){
        found = true;
        System.out.println(k); // don't break or the loop ends early.
    }
}
//this test can only be done after you iterate **all** values.
if (!found) {
    System.out.println("not found");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文