如何将数字答案转换为一串星号

时间:2015-03-05 19:50:59

标签: java

import java.io.*;
import java.util.Scanner;


public class Popcorn 
{

   public static void main(String [] args) throws IOException{
   String farm ; 
   double acre = 0;
   int jar = 0;

    Scanner input = new Scanner (System.in);

    System.out.print("Input the file name with .txt extention : "); // for the user to input the file name with .txt extesion.

    File fileName = new File(input.nextLine());// Gets the File Name
while(!fileName.exists()) {       // It will ask the user to enter the file  name again if it is not in directory.
    // Prompts for input file name

    System.out.print("The file is not in this directory. Please re-enter the file name: "); 
    fileName = new File(input.nextLine()); //Gets the File Name
    input = new Scanner (System.in);  // Constructs File object 
 }

这是输入文件:

   Orville's Acres, 114.8  43801
   Hoffman's Hills, 77.2  36229
   Jiffy Quick Farm, 89.4  24812
   Jolly Good Plantation,  183.2  104570

这是它应该输出的方式:

Popcorn Co-op
      Production in Hundreds
      of Pint Jars per Acre
Farm Name                 1   2   3   4   5   6
                     ---|---|---|---|---|---|
Orville's Acres         ****************|
Hoffman's Hills         ****************#***
Jiffy Quick Farm        ************    |
Jolly Good Plantation   ****************#***
Organically Grown Inc.  ************    |

这是我在运行程序时得到的结果:

               Popcorn Co-op
                         Production in Hundreds
                         of Pint Jars per Acre
Farm Name                    1   2   3   4   5   6
                      ---|---|---|---|---|---|
Orville's Acres    15
Hoffman's Hills    18
#Jiffy Quick Farm    11
Jolly Good Plantation    22 
#> 

如果有人能告诉我如何将我得到的数字转换为星星,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

不确定我是否帮你解决了问题,但可能会为你解决这个问题吗?

String str = "";
for (int i = 0; i < numberOfStars; i++) {
  str += "*";
}

答案 1 :(得分:0)

在代码的某处,在你提供的内容中看不到,应该有一些东西可以打印数字15,18,11和22.而不是打印它们,创建一个可以添加星星的循环,就像在上一个答案:

String str = ""; for (int i = 0; i < numberOfStars; i++) { str += "*"; }