[英]How can i print my array and my histogram on the same line?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Histogram
{
public static void main(String[] args) throws FileNotFoundException
{
Row[] numbers = {
new Row("1 - 10"), new Row("11 - 20"), new Row("21 - 30")
new Row("31 - 40"), new Row("41 - 50"), new Row("51 - 60")
new Row("61 - 70"), new Row("71 - 80"), new Row("81 - 90"),
new Row("91 - 100")
};
for(Row number : numbers)
System.out.print(number);
Counter section = new Counter();
section.StarCounter();
}
it prints out something like this: 它打印出这样的内容:
1 - 10|
11 - 20|
21 - 30|
*****************
*********
***
ect.
I would like it to print out something like: 我希望它打印出类似以下内容:
1 - 10|*****************
11 - 20|*********
21 - 30|***
ect.
thanks in advance for any pointers, I'm probably just overlooking the obvious. 在此先感谢您提供任何指导,我可能只是忽略了显而易见的内容。
Counter class: 柜台类:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Counter
{
public void StarCounter() throws FileNotFoundException
{
File file = new File("alotofnumbers.txt");
Scanner scan = new Scanner(file);
int[] integers = new int[1000];
int start = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;
int count8 = 0;
int count9 = 0;
int count10 = 0;
while (scan.hasNextInt())
{
integers[start++] = scan.nextInt();
}
for (int input : integers)
{
if(input <= 10)
{
count1++;
}
else if(input > 10 && input <= 20)
{
count2++;
}
else if(input > 20 && input <= 30)
{
count3++;
}
else if (input > 30 && input <= 40)
{
count4++;
}
else if (input > 40 && input <= 50)
{
count5++;
}
else if (input > 50 && input <= 60)
{
count6++;
}
else if (input > 60 && input <= 70)
{
count7++;
}
else if (input > 70 && input <= 80)
{
count8++;
}
else if (input > 80 && input <= 90)
{
count9++;
}
else if (input > 90 && input <= 100)
{
count10++;
}
}
double counted1 = count1 / 2.7;
double counted2 = count2 / 2.7;
double counted3 = count3 / 2.7;
double counted4 = count4 / 2.7;
double counted5 = count5 / 2.7;
double counted6 = count6 / 2.7;
double counted7 = count7 / 2.7;
double counted8 = count8 / 2.7;
double counted9 = count9 / 2.7;
double counted10 = count10 / 2.7;
for(int star = 0; star <= counted1 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted2 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted3 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted4 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted5 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted6 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted7 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted8 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted9 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted10 ; star++)
{
System.out.print("*");
}
System.out.println();
}
}
Apparently my post has to much code and I don't really have much more info to give so it wants me to type something random up. 显然我的帖子有很多代码,我实际上没有太多要提供的信息,因此它希望我随意输入。 public class Row { private String rows;
公共类Row {私有字符串行;
public Row(String colums)
{
rows = colums ;
}
public String toString()
{
Counter section = new Counter();
return rows + "|" + "\n";
}
public void setRow(String colums)
{
rows = colums;
}
public String getRow()
{
return rows;
}
}
Here is a solution to your problem. 这是您的问题的解决方案。 The problem was that you were printing all of your
Row
data first and then all of your Integer
data. 问题在于您要先打印所有
Row
数据,然后再打印所有Integer
数据。 You need to print them both at once. 您需要同时打印它们。 Not one after the other.
没有一个接一个的。 Also just fyi, there is about a million different ways to make your initial code better.
同样,仅供参考,有大约一百万种方法可以使您的初始代码变得更好。 I would try and sit down and think of a better way to do this so you can learn (after you turn in your assignment so you are not rushed, we can all help. This is a good start though.)
我会尝试坐下来,想出一个更好的方法来这样做,以便您可以学习(上交作业后,您就不会着急了,我们都可以提供帮助。但这是一个好的开始。)
First I erased the printing of the Row
data, and added a constructor to your Counter
class to take in your Row
data. 首先,我删除了
Row
数据的打印,并向您的Counter
类添加了一个构造函数以接收Row
数据。
Row[] numbers = {
new Row("1 - 10"), new Row("11 - 20"), new Row("21 - 30"),
new Row("31 - 40"), new Row("41 - 50"), new Row("51 - 60"),
new Row("61 - 70"), new Row("71 - 80"), new Row("81 - 90"),
new Row("91 - 100")
};
Counter section = new Counter(numbers);
section.StarCounter();
New Constructor: 新构造函数:
public class Counter
{
Row[] numbers;
public Counter(Row[] numbers) {
this.numbers = numbers;
}
//Rest of your methods
}
Then printed the Row
data when you print your Integer
data. 然后在打印
Integer
数据时打印Row
数据。
System.out.print(numbers[0]); //new line
for(int star = 0; star <= counted1 ; star++)
{
System.out.print("*");
}
System.out.println();
System.out.print(numbers[1]); //new line
for(int star = 0; star <= counted2 ; star++)
{
System.out.print("*");
}
//Do the same for the rest
Hope this helps! 希望这可以帮助!
Oh also got rid of the newline in your Row
class. 哦,在
Row
类中也摆脱了换行符。
public String toString()
{
return rows + "|";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.