这是代码(数字操作类是第二个列出的):
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// declare and instantiate ArrayList with generic type <NumberOperations>
ArrayList<NumberOperations> numOpsList
= new ArrayList<NumberOperations>();
// prompt user for set of numbers
System.out.println("Enter a list of positive integers separated "
+ "with a space followed by 0:");
// get first user input using in.nextInt()
int number = in.nextInt();
// add a while loop as described below:
// while the input is not equal to 0
// add a new NumberOperations object to numOpsList based on user
input
// get the next user input using in.nextInt()
while (number != 0) {
numOpsList.add(new NumberOperations(number));
number = in.nextInt();
}
int index = 0;
while (index < numOpsList.size()) {
NumberOperations num = numOpsList.get(index);
System.out.println("For: " + num);
// add print statement for odds under num
// add print statement for powers of 2 under num
index++;
}
public class NumberOperations {
// instance variables
private int number;
// constructor
/**
* @param numberIn is number
*/
public NumberOperations (int numberIn) {
number = numberIn;
}
// methods
/**
* @return value
*/
public int getValue()
{
return number;
}
public String oddsUnder()
{
String output = "";
int i = 0;
while (i < number) {
if(i % 2 != 0) {
output += i + "\t";
}
i++;
}
return output;
}
public String powersTwoUnder()
{
String output = "";
int powers = 1;
while (powers < number) {
output += powers + "\t";
powers = powers * 2;
}
return output;
}
public int isGreater (int compareNumber)
{
if (number > compareNumber)
{
return 1;
}
else if (number < compareNumber)
{
return -1;
}
else
{
return 0;
}
}
public String toString()
{
String output = "";
return number + "";
}
}
我得到的错误是编译器无法在任何地方找到“NumberOperations”。这可能是我的一个非常基本的问题,但我迷路了。
编辑:我添加了数字操作的类,以防它有用。我认为尽管如此,我做的一切都是正确的。
答案 0 :(得分:0)
Enter a list of positive integers separated with a space followed by 0:
1 2 3 4 0
For: 1
For: 2
For: 3
For: 4
我用类
修饰了main方法import java.util.*;
public class NumberOperationsTest {
在类NumberOperations之前关闭了类,我从公共声明中解放出来,让它可以在一个文件中编译。
在另一个文件中, public 关键字很好。
在编译时,你必须告诉类路径,查看当前目录:
javac -cp . NumberOperationsTest.java