标题:关于Java,自己编写一个constructor来实现文件里面数据的随机抽取
只看楼主
毛毛猫猫
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-4-19
 问题点数:0 回复次数:5 
关于Java,自己编写一个constructor来实现文件里面数据的随机抽取
关于Java,自己编写一个constructor来实现文件里面数据的随机抽取Your Community Supported Agriculture(CSA) farm delivers a box of fresh fruits and
 vegetables to your house once a week. For this Programming, define the class BoxOfProduce that contains exactly three bundles of fruits or vegetables. You can represent the fruits or vegetables in three fields of type String. You can create a no-arg constructor that randomly selected three items for the three fields from this list:
 Broccoli
 Tomato
 Kiwi
 ale
 Tomatillo
 This list is stored in the text file, itemList.txt that is read by your program.Add accessor and mutator methods to get or set the fruits or vegetables stored in these
 fields. Also write an output method that displays the complete contents of the box.
 Write a main method that creates a BoxOfProduce object. Next, the main method should display the contents of the box and allow the user to substitute any one of the
 five possible fruits or vegetables for any of the fruits or vegetables selected for the box. After the user is done with substitutions, output the final contents of
 the box to be delivered. The program should produce output similar to that shown below.
 The contents of the box:
 Item1: Tomatillo
 Item2: Broccoli
 Item3: Broccoli
 Do you agree with the selected item1,
 Tomatillo(Yes or No): Yes
 Do you agree with the selected item2,
 Broccoli(Yes or No): No
 Enter the substituting fruit or vegetable
 for item2: Tomato
 Do you agree with the selected item3,
 Broccoli(Yes or No): No
 Enter the substituting fruit or vegetable
 for item3: Kiwi
 The contents of the box:
 Item1: Tomatillo
 Item2: Tomato
 Item3: Kiwi
接下来是我自尝试写的类,但是不知道应该在main函数里要怎么用,没有什么思路,感觉一整道题目下来思路都不清楚。
public class BoxOfProduce {
 private String Item1;
 private String Item2;
 private String Item3;
 private String names;
 public String line(int num) throws IOException {
  int i;
  String Item = null;
  File file = new File("itemList.txt");
  Scanner inputFile = new Scanner(file);
  for (i = 0; i <= num; i++) {
   Item.equals(inputFile.next());
  }
  inputFile.close();
  return Item;
 }
public BoxOfProduce() throws IOException{
   int num;
   Random rd = new Random();
   num = rd.nextInt(5);
   System.out.println("Item1:"+Item1.equals(line(num)));
   num = rd.nextInt(5);
   System.out.println("Item2:"+Item2.equals(line(num)));
   num = rd.nextInt(5);
   System.out.println("Item3:"+Item3.equals(line(num)));
 }
 public void mutator(String fruitsname) {
   names = fruitsname;
 }
 public String accessor() {
   return names;
 }
     

 public static void main(String[] arrgs) throws IOException{
   BoxOfProduce BoxOfProduce=new BoxOfProduce();
   System.out.println("Do you agree with the selected "+"(Yes or No):");//这句话是我随便写的
  
 }
   
 }    

[ 本帖最后由 毛毛猫猫 于 2015-4-19 20:05 编辑 ]
搜索更多相关主题的帖子: exactly selected represent Community create 
2015-04-19 20:02
毛毛猫猫
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-4-19
得分:0 
为什么没有人回我呢?那我把自己后来做的贴上来了
public class BoxOfProduce {
 private static String Item1;
 private static String Item2;
 private static String Item3;
 /**
  * 用来随机抽取文件内的水果名称,并且复制给Lineread
  *
  * @return Lineread
  * @throws IOException
  */
 private String randomlySelect() throws IOException {
  int Linenum;
  String Lineread = null;
  // 创建Random类,随机生成0~4之间的一个数
  Random rd = new Random();
  Linenum = rd.nextInt(5);
  // 打开文件
  File file = new File("itemList.txt");
  Scanner inputFile = new Scanner(file);
  // 用for循环给Lineread赋值
  for (int i = 0; i <= Linenum; i++) {
   Lineread = inputFile.nextLine();
  }
  // 关闭文件
  inputFile.close();
  // 返回Lineread
  return Lineread;
 }
 /**
  * 创建一个constructor,给Item1,Item2,Item3赋值
  *
  * @throws IOException
  */
 public BoxOfProduce() throws IOException {
  Item1 = randomlySelect();
  Item2 = randomlySelect();
  Item3 = randomlySelect();
 }
 /**
  * 用来返回Item1
  *
  * @return Item1
  */
 public static String getItem1() {
  return Item1;
 }
 /**
  * 可以用来改变Item1的值
  *
  * @param item1
  */
 public static void setItem1(String item1) {
  Item1 = item1;
 }
 /**
  * 用来返回Item2
  *
  * @return Item2
  */
 public static String getItem2() {
  return Item2;
 }
 /**
  * 可以用来改变Item2的值
  *
  * @param item2
  */
 public static void setItem2(String item2) {
  Item2 = item2;
 }
 /**
  * 用来返回Item3
  *
  * @return Item3
  */
 public static String getItem3() {
  return Item3;
 }
 /**
  * 可以用来改变Item3的值
  *
  * @param item3
  */
 public static void setItem3(String item3) {
  Item3 = item3;
 }
 /**
  * 输出Item1,Item2,Item3的值
  */
 public void display() {
  System.out.println("The contents of the box:");
  System.out.println("Item1:" + Item1);
  System.out.println("Item2:" + Item2);
  System.out.println("Item3:" + Item3);
  System.out.println();
 }
 public static void main(String[] args) throws IOException {
  String answer;  //用来得到用户的答案
  String substituteItem;//用户输入的用来替换的水果名称
  
  //创建Scanner类
  Scanner kb = new Scanner(System.in);
  //创建BoxOfProduce类
  BoxOfProduce boxOfProduce = new BoxOfProduce();
  boxOfProduce.display();
  //询问用户是否满意第一种水果
  System.out.print("Do you agree with the selected item1,"
    + boxOfProduce.getItem1() + "(Yes or No):");
  answer = kb.nextLine();
  if (answer.equals("No")) {
   System.out
     .print("Enter the substituting fruit or vegetable for item1:");
   //得到替换水果的名称
   substituteItem = kb.nextLine();
   boxOfProduce.setItem1(substituteItem);
  }
  System.out.print("Do you agree with the selected item2,"
    + boxOfProduce.getItem2() + "(Yes or No):");
  answer = kb.nextLine();
  if (answer.equals("No")) {
   System.out
     .print("Enter the substituting fruit or vegetable for item2:");
   substituteItem = kb.nextLine();
   boxOfProduce.setItem2(substituteItem);
  }
  System.out.print("Do you agree with the selected item3,"
    + boxOfProduce.getItem3() + "(Yes or No):");
  answer = kb.nextLine();
  if (answer.equals("No")) {
   System.out
     .print("Enter the substituting fruit or vegetable for item3:");
   substituteItem = kb.nextLine();
   boxOfProduce.setItem3(substituteItem);
  }
  System.out.println();
  //输出最终结果
  boxOfProduce.display();
 }
}
2015-04-23 21:05
gbooks
Rank: 1
等 级:新手上路
帖 子:14
专家分:4
注 册:2015-9-6
得分:0 
看着论坛的密密麻麻的代码 号蛋疼,是不是我还没习惯看代码....刚入门
2015-09-06 21:09
liuyijie
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2015-9-8
得分:0 
看不懂我还没开始学
2015-09-08 14:08
华山大师兄
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-10-19
得分:0 
看的有点头疼
2015-09-21 22:56
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
得分:0 
哎...挺简单一个题目啊,哈哈哈哈

淘宝杜琨
2015-10-07 00:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-444258-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.114139 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved