我有一个接收数组的程序。我想让它把结果组织成范围(比如 60-69)。它还需要知道它收到的数字中有多少符合哪些范围,以便可以将它们相应地放入。我在表格中看到的创建选项卡的方式是
System.out.print("Column1/tColumn2");
如何将数组数据组织到一个范围内?你用一堆 IF 语句来指定它吗?而且我假设您使用计数器来计算每个范围内有多少个数字?
编辑:这是目前的代码。
import java.util.Scanner;
import java.lang.Integer;
import java.lang.String;
public class Salary {
int salary;
int sales;
public static void main(String[] args) {
Scanner input = new Scanner ( System.in );
int Salary;
Salary salary = new Salary();
System.out.print( "Please enter the amount each employee brought in:");
String sales = input.nextLine();
String salesArray[] = sales.split(" ");
for(int i=0; i<salesArray.length; i++){
Integer myInt = Integer.parseInt(salesArray[i]);
System.out.print(salesArray[i] + " ");
if (Integer.parseInt(salesArray[i]) <= 0) {
System.out.println("Please enter a value greater than zero");}
else {
Salary = ((myInt/100) * 9) + 200;
System.out.print(Salary); }
}
int two = 0; //declared a variable, starting it at 0 and intending to increment it
//with input from the array
System.out.print("Range/t#ofEmployees");
for(int i=0; i<salesArray.length; i++) {
if (salesArray[i] <= 200){} //Error on the if statement, using the wrong operand
else{ two++;} //Attempt at the table and increment.
}
}
}
最佳答案
我不会发布一个实现,因为这是家庭作业,但想想这个:
你的数组可以排序...
您可以迭代该数组,如果当前值小于某个边界,则向上计数...一旦它更大,您可以打印一个范围并从头开始下一个范围...
边界可以从一个范围到另一个范围变化......
关于java - 使用 Java 将数组输入组织到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7616238/