问题描述
因此,经过反复尝试,我将水平直方图至少部分转换为垂直直方图。
How many input values [max:30]?
5
Enter 5 numbers.
2
1
2
0
2
Number Occurrence
0 1
1 1
2 3
========= Vertical Bar ========
2 | *
1 | * * *
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================
How many input values [max:30]?
1
Enter 1 numbers.
5
Number Occurrence
5 1
========= Vertical Bar ========
5 |
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================
不打印
5
Enter 5 numbers.
3
3
3
3
3
Number Occurrence
3 5
========= Vertical Bar ========
3 | *
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================
How many input values [max:30]?
10
Enter 10 numbers.
5
4
3
2
1
1
2
3
4
5
Number Occurrence
1 2
2 2
3 2
4 2
5 2
========= Vertical Bar ========
5 |
4 |
3 |
2 | * * * * *
1 | * * * * *
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================
为5-3添加空格
10
Enter 10 numbers.
2
2
3
3
3
4
4
4
4
1
Number Occurrence
1 1
2 2
3 3
4 4
========= Vertical Bar ========
4 | *
3 | * *
2 | * * *
1 | * * * *
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================
尽管不是设计使然,但仍可按预期工作
public class Histogram
{
public static void main(String[] args) {
//variables
Scanner keyboard = new Scanner(system.in);
int numInputs = 1,temp,maximum = 0;
int[] numbers = new int[31];
int[] count = new int[31];
boolean success = false;
//start of program
System.out.println("How many input values [max:30]?");
//while no valid input
while (!success) {
try {
numInputs = keyboard.nextInt(); //get a number
numInputChecker(numInputs); //is it valid?
success = true; //ok
} catch (Exception e) //else get a new number
{
keyboard.nextLine();
System.out.println("Whole numbers 1 through 30 only,please.");
}
}
//reset the loop checker
success = false;
//read numbers to fill that array
System.out.println("Enter " + numInputs + " numbers.");
for (int i = 0; i < numInputs; i++) //from 0 to max number
{
while (!success) //while no valid number
{
try {
numbers[i] = keyboard.nextInt(); //fill the current cell with a number
numberChecker(numbers[i]); //is it valid?
success = true; //ok
} catch (Exception e) //else get a new number
{
keyboard.nextLine();
System.out.println("Whole numbers 0 through 9 only,please.");
}
}
success = false;
}
//for cells not used
for (int i = numInputs; i < numbers.length; i++) {
numbers[i] = 10; //fill with garbage data (to prevent false positive 0s)
}
//take the input and count each use of element
for (int i : numbers) //for 0 to max number
{
temp = i; //get the current value of the cell
count[temp]++; //add the use of that value to a new array's cell
}
System.out.println("Number Occurrence");
for (int i = 0; i < count.length; i++) //from 0 to 9 (expected)
{
if ((count[i] > 0) && (count[i] <= 9)) //if cell not empty and has valid data
{
System.out.println(i + " " + count[i]); //print the current cell and how many times it was used
}
}
System.out.println(); //spacer
//histogram segment
//find the highest-used number
for (int i : count) //for each number
{
if(i > maximum) //if greater than the current max
{
maximum = i; //set to max
}
}
System.out.println("========= Vertical Bar ========");
for (int i = maximum; i > 0; i--) //max through 1
{
if ((count[i] > 0) && (count[i] <=9)) //if has valid data
{
System.out.print((i) + "\t | "); // print the number and a nice line for readability
for (int j = 0; j < count.length; j++) //for the number of times that number was used
{
if ((count[j] > 0) && (count[j] <=9)) //if has valid data
{
if (count[j] >= i) //if that number the max
{
System.out.print("* "); //print an asterisk
}
else
{
System.out.print(" "); //"skip" and keep alignment
}
}
}
System.out.println(); //make a new line
}
}
System.out.println("==============================="); //footer
System.out.println("| No | 0 1 2 3 4 5 6 7 8 9");
System.out.println("===============================");
}
static void numInputChecker(int integer) throws Exception
{
if ((integer < 1) || (integer > 30)) //if 0 or negative,or if 31+
{
throw new Exception(); //say no
}
}
static void numberChecker(int integer) throws Exception
{
if ((integer < 0) || (integer > 9)) //if negative or 10+
{
throw new Exception(); //say no
}
}
}
解决方法
您的问题是,星号输出逻辑中的if
块过多。您要根据数据决定不在某些水平位置上打印任何内容,而您真正想要做的是在每个位置上水平打印两个字符(一个空格加一个星号或两个空格),无论您是否看到任何字符该位置的数字。因此,如果您取出一个if
以外的所有代码块,就可以使打印代码变得更简单,并且可以做正确的事情:
System.out.println("========= Vertical Bar ========");
// for each count,starting from the max...
for (int i = maximum; i > 0; i--)
{
System.out.print((i) + "\t | ");
// for each number from 0 to the largest number we saw
for (int j = 0; j < count.length; j++)
{
// If the count at this position horizontally is greater than or
// equal to the count vertically (the line number we're on),then
// print an asterisk,else print a blank space.
if (count[j] >= i).
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("===============================");
System.out.println("| No | 0 1 2 3 4 5 6 7 8 9");
System.out.println("===============================");
这是我用来测试的一个非常复杂的结果:
输入:
int[] numbers = { 1,2,3,4,5,1,7 };
结果:
Number Occurrence
1 3
2 1
3 5
4 1
5 5
7 1
========= Vertical Bar ========
5 | * *
4 | * *
3 | * * *
2 | * * *
1 | * * * * * *
===============================
| No | 0 1 2 3 4 5 6 7 8 9
===============================
,
public class Histogram
{
public static void main(String[] args)
{
//variables
Scanner keyboard = new Scanner(System.in);
int numInputs = 1,temp,maximum = 0;
int[] numbers = new int[31];
int[] count = new int[31];
boolean success = false;
//start of program
System.out.println("How many input values [max:30]?");
//while no valid input
while (!success)
{
try
{
numInputs = keyboard.nextInt(); //get a number
numInputChecker(numInputs); //is it valid?
success = true; //ok
} catch (Exception e) //else get a new number
{
keyboard.nextLine();
System.out.println("Whole numbers 1 through 30 only,please.");
}
}
//reset the loop checker
success = false;
//read numbers to fill that array
System.out.println("Enter " + numInputs + " numbers.");
for (int i = 0; i < numInputs; i++) //from 0 to max number
{
while (!success) //while no valid number
{
try
{
numbers[i] = keyboard.nextInt(); //fill the current cell with a number
numberChecker(numbers[i]); //is it valid?
success = true; //ok
} catch (Exception e) //else get a new number
{
keyboard.nextLine();
System.out.println("Whole numbers 0 through 9 only,please.");
}
}
success = false;
}
//for cells not used
for (int i = numInputs; i < numbers.length; i++)
{
numbers[i] = -1; //fill with garbage data (to prevent false positive 0s)
}
//take the input and count each use of element
for (int i : numbers) //for 0 to max number
{
if (i != -1) //if valid data
{
temp = i; //get the current value of the cell
count[temp]++; //add the use of that value to a new array's cell
}
}
System.out.println("Number Occurrence");
for (int i = 0; i < count.length; i++) //from 0 to 9 (expected)
{
if (count[i] > 0) //if cell has valid data
{
System.out.println(i + " " + count[i]); //print the current cell and how many times it was used
}
}
System.out.println(); //spacer
//histogram segment
//find the highest-used number
for (int k : count) //for 0 to 9
{
if (k > maximum) //if greater than the current max
{
maximum = k; //set to max
}
}
System.out.println("========= Vertical Bar ========");
for (int i = maximum; i > 0; i--) // from max to 1
{
System.out.print((i) + "\t | "); //print the number and a spacer for visibility
for (int j = 0; j < count.length; j++) // from 0 to max
{
if (count[j] >= i) // If the count at this position horizontally is greater than or
// equal to the count vertically (the line number we're on)
{
System.out.print("* "); //print an asterisk
}
else
{
System.out.print(" "); //else print a blank
}
}
System.out.println(); //spacer
}
//footer
System.out.println("===============================");
System.out.println("| No | 0 1 2 3 4 5 6 7 8 9");
System.out.println("===============================");
}
static void numInputChecker(int integer) throws Exception
{
if ((integer < 1) || (integer > 30)) //if 0 or negative,or if 31+
{
throw new Exception(); //say no
}
}
static void numberChecker(int integer) throws Exception
{
if ((integer < 0) || (integer > 9)) //if negative or 10+
{
throw new Exception(); //say no
}
}
}