简体   繁体   中英

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. 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". Both statements (for and while) are used for iterate until a condition is raised (in this case, i < chart.length); also, I didn't test it but I think your code doesn't work because you're not incrementing 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;
    }
}`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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