Java 如何将此while循环更改为if?
谁能看看我的密码吗。 它是一个程序,为用户提供所需艺术家的图表位置。 它不太管用。 另外,我使用了一个while循环,我告诉我应该使用if语句。 有人能给我解释一下,告诉我怎么改吗。 我对这一点非常陌生,不太了解 这是我的密码Java 如何将此while循环更改为if?,java,Java,谁能看看我的密码吗。 它是一个程序,为用户提供所需艺术家的图表位置。 它不太管用。 另外,我使用了一个while循环,我告诉我应该使用if语句。 有人能给我解释一下,告诉我怎么改吗。 我对这一点非常陌生,不太了解 这是我的密码 import java.util.*; public class chartPosition { public static void main (String [] args) { System.out.println("Which artist wou
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");
}
}
}
导入java.util.*;
公共类图表位置
{
公共静态void main(字符串[]args)
{
System.out.println(“您想要哪位艺术家?”);
String[]chart={“蕾哈娜”、“谢丽尔·科尔”、“亚历克西斯·乔丹”、“凯蒂·佩里”、“布鲁诺·马尔斯”、“Cee Lo Green”,
“迈克·波斯纳”、“耐莉”、“鸭子酱”、“星期六”};
字符串条目=”;
扫描仪kb=新扫描仪(System.in);
entry=kb.nextLine();
查找(条目、图表);
}
公共静态void find(字符串条目,字符串[]图表){
int位置=-1;
for(inti=0;i
for(inti=0;i我将修复放在注释中,查看它们并更改代码=)
import java.util.*;
公共类图表位置
{
公共静态void main(字符串[]args)
{
System.out.println(“您想要哪位艺术家?”);
String[]chart={“蕾哈娜”、“谢丽尔·科尔”、“亚历克西斯·乔丹”、“凯蒂·佩里”、“布鲁诺·马尔斯”、“Cee Lo Green”,
“迈克·波斯纳”、“耐莉”、“鸭子酱”、“星期六”};
字符串条目=”;
扫描仪kb=新扫描仪(System.in);
entry=kb.nextLine();
查找(条目、图表);
}
公共静态void find(字符串条目,字符串[]图表){
int位置=-1;
//在for循环中应该定义步骤,在您的情况下,您必须更改for循环on for(int i=0;i您已经在for循环中,这就是为什么您应该将“while”更改为“if”。这两个语句(for和while)都用于迭代,直到出现条件(在本例中,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;
}
}`
for(int i=0;我必须将单词while
更改为单词if
。最好将i++
放在for循环的末尾,只需将while(entry.equalsIgnoreCase(chart[i])
更改为if(entry.equalsIgnoreCase(chart[i++]))
`(学会)使用调试器,一步一步地检查代码中的变量。这将提供信息,并且您无论如何都需要它来进行真正的调试。
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");
}
}
}
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;
}
}`