创建一个类,开始使用OOP。java程序不';我什么也没展示?

创建一个类,开始使用OOP。java程序不';我什么也没展示?,java,class,oop,object,constructor,Java,Class,Oop,Object,Constructor,主要的问题是,当我运行我的程序时,什么都没有发生,但是没有任何bug。我的程序应该用一些方法创建一个新类(Fraction),这些方法将操作分数并用一些不同的值对其进行测试。这是一个两个文件的程序,我在构造函数方法方面有点麻烦 我创建了一个应该初始化对象的分子和分母的方法,但我不知道在定义中应该放什么。对象的编号在测试文件中选择 这是那一块: (位于我定义类及其所有方法的文件中) 我也不确定我是否正确地执行了循环,因为当我运行程序时没有发生任何事情,它只是说“构建成功”。我试着输入一个确认对话框

主要的问题是,当我运行我的程序时,什么都没有发生,但是没有任何bug。我的程序应该用一些方法创建一个新类(
Fraction
),这些方法将操作分数并用一些不同的值对其进行测试。这是一个两个文件的程序,我在构造函数方法方面有点麻烦

我创建了一个应该初始化对象的分子和分母的方法,但我不知道在定义中应该放什么。对象的编号在测试文件中选择

这是那一块: (位于我定义类及其所有方法的文件中)

我也不确定我是否正确地执行了循环,因为当我运行程序时没有发生任何事情,它只是说“构建成功”。我试着输入一个确认对话框,但是我仍然需要初始化程序吗

下面是完整的代码。 程序中的第一个文件:

public class Fraction {

private int numerator;
private int denominator;
//returns the fraction
@Override
public String toString()
{
    return numerator + "/" + denominator;
}
//turns fraction into decimal
public double getDecimal()
{
    double decimal = numerator / denominator;
    return decimal;
}
//reduces fraction. doesn't return value
public void reduce()
{
    int gcf = 1, smaller;
    if (numerator < denominator)
    {
        smaller = numerator;
    } else
    {
        smaller = denominator;
    }
    for (int i = 1; i <= smaller; i++)
    {
        if (numerator % i == 0 && denominator % i == 0)
        {
            gcf = i;
        }
    }
    numerator = numerator / gcf;
    denominator = denominator / gcf;
    System.out.print(numerator + "/" + denominator);
}
//turns fraction into a mixed number and returns the new value
public String toMixed()
{
    int whole = numerator / denominator;      
    numerator %= denominator;
    String changedFraction = (whole + " " + numerator + "/" + denominator);
    return changedFraction;
}

public Fraction(int n, int d)
{
    //subbed in zeros below so it doesn't give me an error
    numerator = 0;
    denominator = 0;
}

//initializes the fraction to 1. Not sure how to call this method in the test file. 

   public Fraction()
   {
       numerator = 1;
       denominator = 1;
   }
}
公共类分数{
私有整数分子;
私有整数分母;
//返回分数
@凌驾
公共字符串toString()
{
返回分子+“/”+分母;
}
//将分数转换成小数
公共双精度getDecimal()
{
双十进制=分子/分母;
返回小数;
}
//减少分数。不返回值
公共空间减少()
{
int gcf=1,更小;
if(分子<分母)
{
较小=分子;
}否则
{
较小=分母;
}

对于(inti=1;i我认为问题在于您在主应用程序中使用JOptionPane的方式

首先,我认为应该为窗格创建一个父JFrame

其次,如果我理解正确的话,我认为JOptionPane.showOptionDialog方法更适合您在这里试图实现的目标

这是一个关于如何创建对话框的非常好的指南:

当然你想要

public Fraction(int n, int d)
{
    //subbed in zeros below so it doesn't give me an error
    numerator = n;
    denominator = d;
}
import javax.swing.*;

public class TestProgram
{

public static void main(String[] args)
{  
    //dialog box that runs program until user chooses to exit
    int option = JOptionPane.YES_OPTION;
    while (option == JOptionPane.YES_OPTION)
    {
 //menu that lets user choose which method they want to test the values with
    String input = JOptionPane.showInputDialog("Choose"
            + "\n" + "1. Test the toString() method"
            + "\n" + "2. Test the getDecimal() method"
            + "\n" + "3. Test the reduce() method"
            + "\n" + "4. Test the toMixed() method"
            + "\n" + "Click cancel to quit");
    int choice = Integer.parseInt(input);

    //array that holds 5 test values
    Fraction[] fractionArray = new Fraction[4];

    fractionArray[0] = new Fraction(9, 12);
    fractionArray[1] = new Fraction(31, 2);
    fractionArray[2] = new Fraction(12, 9);
    fractionArray[3] = new Fraction(12, 120);
    fractionArray[4] = new Fraction(16, 53);

    //calls the method chosen
    if (choice == 1)
    {
        for(int i = 0; i <fractionArray.length; i++)
        {
            fractionArray[i].toString();
        }
    }
    else if (choice == 2)
    {
        for(int i = 0; i <fractionArray.length; i++)
        {
            fractionArray[i].getDecimal();
        }
    }
    else if (choice == 3)
    {
        for(int i = 0; i <fractionArray.length; i++)
        {
            fractionArray[i].reduce();
        }
    }
    else if (choice == 4)
    {
        for(int i = 0; i <fractionArray.length; i++)
        {
            fractionArray[i].toMixed();
        }
    }
    //initializes the constructor method. not sure what values to put below.
       Fraction currentFraction = new Fraction(2,3);

    JOptionPane.showConfirmDialog(null, "Continue?");
    }
}
}
public Fraction(int n, int d)
{
    //subbed in zeros below so it doesn't give me an error
    numerator = n;
    denominator = d;
}