【问题标题】:creating a class, beginning to use OOP. java program doesn't display anything?创建一个类,开始使用OOP。 java程序什么都不显示?
【发布时间】:2015-12-15 02:35:37
【问题描述】:

主要问题是当我运行我的程序时没有任何反应,但没有任何错误。我的程序应该使用一些方法创建一个新类 (Fraction),这些方法将操纵分数并用几个不同的值对其进行测试。这是一个两个文件程序,我在构造函数方法上遇到了一些麻烦。

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

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

public Fraction(int n, int d)
{
    //not sure what values to enter below
    numerator = ;
    denominator = ;
}

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

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

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;
   }
}

第二个(测试)文件:

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?");
    }
}
}

【问题讨论】:

    标签: java class oop object constructor


    【解决方案1】:

    你当然想要

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

    【讨论】:

      【解决方案2】:

      我认为问题在于您在 main 中使用 JOptionPane 的方式。

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

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

      这是一个很好的关于如何制作对话的指南:https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-29
        • 2021-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多