首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于保存二手车和新车销售总额的二维数组

用于保存二手车和新车销售总额的二维数组
EN

Stack Overflow用户
提问于 2014-03-29 03:02:06
回答 2查看 509关注 0票数 1

我必须为我的类为一家既卖新车又卖二手车的汽车经销商创建一个java程序。我们需要一个程序,可以询问用户的销售人数。将它们的名称添加到字符串数组中。接下来,分别询问新车和二手车的销售总额。这是到目前为止我的程序。我想知道是否有一种方法可以为我的表包含以下三个标题: Name、Used Sales和New Sales。

代码语言:javascript
复制
 public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int numemployees;
    System.out.println("Enter the Number of Employees: ");
    numemployees = in.nextInt();
    String[] names = new String[numemployees];
    String line;

  for (int i = 0; i < numemployees; i++)
  {
      System.out.print("Enter the name of the Salesperson: ");
      names[i] = in.next();
  }

  double [][] sales = new double [numemployees][2];
  for(int j = 0; j < numemployees; j++)
  {
      System.out.println("Enter New Car Sales: "+ names[j]);
      sales[j][0] = in.nextDouble();
      System.out.println("Enter Used Car Sales: ");
      sales[j][1] = in.nextDouble();

  }

  for(int x = 0; x < numemployees; x++)
  {
       System.out.println(names[x] + "\t" + sales[x][0] + "\t" + sales[x][0] + "\t");
  }


  }
}
EN

回答 2

Stack Overflow用户

发布于 2014-03-29 04:47:31

要打印页眉,请使用System.out.println:

代码语言:javascript
复制
System.out.println("Name \t Used Sales \t New Sales");
for(int x = 0; x < numemployees; x++)
{
      System.out.println(names[x] + "\t" + sales[x][0] + "\t" + sales[x][1] + "\t");
}

另外,要注意索引:您已经使用了System.out.println(names[x] + "\t" + sales[x][0] + "\t" + sales[x][0] + "\t");。问题是sales[x][0]只能打印两次相同的值。

票数 1
EN

Stack Overflow用户

发布于 2014-03-29 04:53:30

1为此,我建议使用String.format。然后你会得到一些很好的格式。

代码语言:javascript
复制
System.out.println(String.format("%-10s%-10s%-10s", "Name", "Used Sales", "New Sales"));
for(int x = 0; x < numemployees; x++)
{
    System.out.println(String.format("%8.2f%8.2f%8.2f", names[x], sales[x][0], sales[x][1]));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22721103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档