简体   繁体   English

我如何在if循环中更改此循环?

[英]How can i change out this while loop for an if?

Can somebody please take a look at my code. 有人可以看看我的代码吗? It is a program that gives the user the chart position of a requested artist. 它是一个程序,可向用户提供所请求艺术家的排行榜位置。 It doesnt quite work. 它不是很有效。 Also, im using a while loop where im told i should be using an if statement. 另外,即时消息使用while循环,即时消息告诉我应该使用if语句。 Could somebody explain this to me and show me how to change it. 有人可以向我解释一下,并告诉我如何进行更改。 I am extremely new to this and dont quite understand here is my code 我对此非常陌生,不太了解这是我的代码

import java.util.*;

public class chartPosition
{
public static void main (String [] args)
{


    System.out.println("Which artist would you like?");
    String [] chart = { "Rihanna", "Cheryl Cole", "Alexis Jordan", "Katy Perry", "Bruno Mars", "Cee Lo Green",
                                 "Mike Posner", "Nelly", "Duck Sauce", "The Saturdays"};

    String entry = "";
    Scanner kb = new Scanner (System.in);

    entry = kb.nextLine();
    find (entry, chart);
 }

public static void find (String entry,String [] chart) {

int location = -1 ;
for (int i=0;i<chart.length;)
{
    while (entry.equalsIgnoreCase( chart[i])) 

    {
        System.out.println( chart + "is at position " + (i+1) + ".");
        location = i;
        break;
    }
    }
if (location == -1);
{
    System.out.println("is not in the chart");
}

}
}

for (int i=0;i<chart.length;)
{
    if (entry.equalsIgnoreCase( chart[i])) 
    {
        System.out.println( chart + "is at position " + (i+1) + ".");
        location = i;
        break;
    }
}

I put the fixes in comments, look at them and change your code =) 我将修复程序放入注释中,查看它们并更改您的代码=)

import java.util.*;

    public class chartPosition
    {
    public static void main (String [] args)
    {


        System.out.println("Which artist would you like?");
        String [] chart = { "Rihanna", "Cheryl Cole", "Alexis Jordan", "Katy Perry", "Bruno Mars", "Cee Lo Green",
                                     "Mike Posner", "Nelly", "Duck Sauce", "The Saturdays"};

        String entry = "";
        Scanner kb = new Scanner (System.in);

        entry = kb.nextLine();
        find (entry, chart);
     }

    public static void find (String entry,String [] chart) {

    int location = -1 ;

// in for loop there should be defined step, in your case you must change for loop on for (int i=0;i<chart.length;i++), becouse your loop stands on same i value

    for (int i=0;i<chart.length;)
    {

//there should be WHILE changed for IF...the if is condition and while is loop...
        while (entry.equalsIgnoreCase( chart[i])) 

        {
            System.out.println( chart + "is at position " + (i+1) + ".");
            location = i;
            break;
        }
        }
    if (location == -1);
    {
        System.out.println("is not in the chart");
    }

    }
    }

You are already inside a for loop, that's why you should change the "while" for an "if". 您已经在for循环中,这就是为什么应将“ while”更改为“ if”的原因。 Both statements (for and while) are used for iterate until a condition is raised (in this case, i < chart.length); 这两个语句(for和while)都用于迭代,直到条件提高(在这种情况下,i <chart.length); also, I didn't test it but I think your code doesn't work because you're not incrementing i: 另外,我没有测试它,但是我认为您的代码不起作用,因为您没有增加i:

for (int i=0; i<chart.length; i++) 
{

    if (entry.equalsIgnoreCase( chart[i])) 
    {
        System.out.println( chart + "is at position " + (i+1) + ".");
        location = i;
        break;
    }
}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM