繁体   English   中英

Java - 仅包含字符串的重载构造函数的问题

[英]Java - Problems with overload constructor containing only a string

我无法弄清楚如何正确地向用户询问 3 个表示属性的输入,这些属性将用于创建将打印在屏幕上的对象,并确保输入不是错误的值。 例如,如果用户输入 0.5 作为直径,它将更改为 1。重载构造函数应该表示一个文件的名称,该文件将包含正在创建的对象的名称、直径和温度。

当我尝试运行它时,会出现缺少符号的错误。 我怎样才能得到它,以便将字符串作为其唯一参数的重载构造函数被调用到 Object2 类中以创建对象?

public class Object1
{
   //attributes
   private double yyy;
   private int zzz;
   private String xxx;
   private String fileName;

   //symbolic constants
   private final double MINDIAMETER = 1;
   private final int MINTEMP = 300;
   private final String NA = "N/A";

   // getter/setter methods for name
   public void setName(String n)
   {
   if (n != "")
   xxx = n;

   else 
   xxx = NA;
   }

   public String getName()
   {
   return xxx;
   }

   // getter/setter methods for diameter
   public void setDiameter(double d)
   {
   if (d >= MINDIAMETER)
   yyy = d;

   else 
   yyy = MINDIAMETER;
   }

   public double getDiameter()
   {
   return yyy;
   }

   // getter/setter methods for temperature
   public void setTemp(int t)
   {
   if (t >= MINTEMP)
   zzz = t;

   else 
   zzz = MINTEMP;
   }

   public int getTemp()
   {
   return zzz;
   }

   public Object1()
   {
      xxx = NA;
      yyy = MINDIAMETER;
      zzz = MINTEMP;
   }

   public Object1(String n, double d, int t)
   {
      setName(n);
      setDiameter(d);
      setTemp(t);
   }

   public Object1(String f)
   {
        fileName = f; //Initialize the object's fileName with f
        System.out.print("Enter the filename: ");


   }

   public String toString()
   {
      return "A object, named " + xxx + ", with a diameter of " + yyy + " miles, and a temperature of " + zzz + " Kelvin.";
   }
}

单独的文件代码

import java.util.Scanner;
import java.io.FileWriter;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.IOException;

public class Object2
{
   public static void main(String[] args) throws IOException
   {

         // object with overload
        Object1 z;
        z = new Object1("");

        Scanner kb = new Scanner(System.in);


        fileName = kb.nextLine();
        File file = new File(fileName);
        PrintWriter outputFile = new PrintWriter(file);

        System.out.println("please enter name: ");
        while (kb.hasNext())
        {

        name = kb.nextLine();
        System.out.println("please enter the diameter: ");
        diameter = kb.nextDouble();
        System.out.println("please enter the temperature: ");
        temp = kb.nextInt();

        System.out.println(name + " " + diameter + " " + temp);

        outputFile.println(name);
        outputFile.println(diameter);
        outputFile.println(temp);
        }


        outputFile.close();
        System.out.println(z);
    }
}

首先,您的 Object1 类中必须进行一些更改。

public class Object1
{
   ...

   public void setName(String n)
   {
   if (!n.equals(""))
   xxx = n;

   else 
   xxx = NA;
   }

   ...
}

其次,您需要将扫描仪值实现到您的对象。

z.setName(kb.nextLine());
System.out.println("please enter the diameter: ");
z.setDiameter(kb.nextDouble());
System.out.println("please enter the temperature: ");
z.setTemp(kb.nextInt());

System.out.println(z.getName + " " + z.getDiameter + " " + z.getTemp);

outputFile.println(z.getName);
outputFile.println(z.getDiameter);
outputFile.println(z.getTemp);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM