import javax.swing.JOptionPane;
public class NewClass {
int i = 1;
public static void main(String[] args)
{
String output = "";
System.out.println("kilograms | pounds");
System.out.println("--------+-----------");
for ( i = 1; i <= 199; i ++)
{
// Every table line in between
System.out.printLn(“| “+i+” | “+i*2.2+” |”);
}
System.out.println(output);
} //end method
} // end class
这里有什么问题,为什么我不能运行它?试图显示KG从1到199的列表
答案 0 :(得分:1)
你做了一些错别字。这应该编译:
//Removed import because you aren't using it
public class NewClass {
public static void main(String[] args)
{
System.out.println("kilograms | pounds");
System.out.println("--------+-----------");
for (int i = 1; i <= 199; i ++)
// define i at this point because it's only used in this scope
{
System.out.println("| " + i + " | " + (2.2 * i) + " |");
// Use normal "
// you misspelled println
// put parenthesis around calculations
}
System.out.println("");
} //end method
} // end class
您应该考虑使用像Eclipse这样的IDE。它突出显示上述错误。
答案 1 :(得分:0)
你正在尝试在连接字符串的过程中进行数学运算。在括号中包裹i * 2.2,你应该没事。现在,您将i的值附加到String,然后尝试将该String乘以2.2。
答案 2 :(得分:0)
更改你的for循环,这样你就不会使用类成员i
(你应该删除它):
for ( int i = 1; i <= 199; i ++) //could be i < 200 though, if I was being picky...
更改此行,以便 println 而不是 printLn ,并使用普通双引号:
System.out.println("| " + i + " | " + i * 2.2 + " |");