有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

方法内部的java用户输入和对象创建

我有一个名为Real的Java类

public class Real {
  private long wholeNumPart;
  private long decimalPart;
  public Real(){
        wholeNumPart =0;
        decimalPart=0;
    }
    public Real(long wholeNumPart, long decimalPart) {
        this.wholeNumPart =wholeNumPart;
        this.decimalPart = decimalPart;
    }
    
    public long getWholeNumPart() {
        return wholeNumPart;
    }
    public long getDecimalPart() {
        return decimalPart;
    }}

我有另一个类名RealApplication,我需要在其中创建两个方法

  1. createRealObject(),允许用户输入实数并创建表示 那个号码。 2.createRealNumber(),它接受Real类型的对象并返回表示的实数 通过那个物体

我很难创建这两种方法 这是我到目前为止所做的

import java.util.Scanner;
public class RealApplication {
    
    public void createRealNumber() {
        Scanner sc=new Scanner(System.in);
        
        //Allows user input
        System.out.print("Please, enter a real number: ");
        long n = sc.nextLong();
        
        
        //Creates Real object ( is this correct????)
        Real in = new Real();
        
    }
    
    public long createRealNumber(Real num) {
        long realNum=0;

        //I do not know what to write here :(

        return realNum;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    通过对RealApplication类进行一些更改,您的Real类看起来不错我们可以实现您想要的:

    import java.util.Scanner;
    public class RealApplication {
    
    public static Real createRealObject() {
        Scanner sc=new Scanner(System.in);
    
        //Allows user input
        System.out.print("Please, enter a real number: ");
        String n = sc.nextLine();
        String[] pieces = n.split("[.,]+");     //special pattern to recognize comma and dot
        long wholeNumPart;
        long decimalPart;
        try {
            wholeNumPart = Long.valueOf(pieces[0]);
            decimalPart = Long.valueOf(pieces[1]);
        }
        catch (NumberFormatException e) {
            System.out.println("You should enter a number!");
            return null;
        }
        Real in = new Real(wholeNumPart, decimalPart);
        sc.close();
        return in;
    }
    

    重要的一点是,我将这两个方法声明为静态的,这样您就可以使用这些方法,而无需创建RealApplication类的实例

    您应该使用“double”原语类型来存储小数,而不是“long”。现在,返回该对象的等效数字的方法:

    public static double createRealNumber(Real num) {
        double realNum;
        long wholeNumPart = num.getWholeNumPart();
        long decimalPart = num.getDecimalPart();
        int numOfDigits = (int)(Math.log10(decimalPart)+1);
    
        realNum = wholeNumPart + (decimalPart / Math.pow(10,numOfDigits));
    
        return realNum;
    }
    

    如果我们写一个主方法:

    public class Main {
    
    public static void main(String[] args) {
    
    
    
        Real realObj = new Real(10,2323232);
    
        double number = RealApplication.createRealNumber(realObj);
    
        System.out.println("Equivalent number for the object: " + number);
    
    
        Real newObj = RealApplication.createRealObject();
    
        if (newObj != null) {
            System.out.println("New object's number part: " + newObj.getWholeNumPart());
            System.out.println("New object's decimal part: " + newObj.getDecimalPart());
        }
        else{
            return;
        }
    
      }
    }
    

    由于我们使用的正则表达式模式,输入用“.”分隔和“,”是允许的,如10.23 10,23