一个项目在日食中运行需要多长时间?**固定**

好吧,所以我在我的学校有一个计算机科学项目。该项目是创建一个GUI,用于计算应该对个人收入征收多少税款。我遇到的问题是,每次我想运行程序时都需要3分钟才能使程序实际启动。包括我的老师在内的很多人都说这不正常。这是我的代码:


package me.findTax;


/*

*   Notes:

*       Fix the location of all the elements and create the math part of the program

*

*       For some reason, takes eclipse a long time on home & school computers to run this program, not entirely sure why (2+ min)

*

*       If something is not working, try looking a make sure that the change method is called after everytime that the getQuestion method is called

*/


import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;


public class Main { //gives questions (source not included, keylistener included (currently not working), actionlistener included)


static JRadioButton b1;

static JRadioButton b2;

static JFrame frame;

static JPanel panel;

static JLabel L1;

static JLabel L2;

static JTextField tfield;

static ButtonGroup bg = new ButtonGroup();

static JButton B1;

static double tax;


static boolean married;


static ArrayList<String> poss_Questions = new ArrayList<String>();


private static int q;

// Only need 2 buttons because there is only one prompt (yes or no)


public static void change() {

    if(q == 1) {

        b1.setVisible(false);

        b2.setVisible(false);

        tfield.setVisible(true);

        B1.setVisible(true);

    } else if(q == 2) {

        tfield.setVisible(false);

        B1.setVisible(false);

        L2.setText(Double.toString(tax)); //fix to make output more good

        L2.setVisible(true);

        L1.setLocation(10,20);

    }

}


public static String getQuestion(){

    String question = "";

    if(q == 0){

        question = poss_Questions.get(q);

    } else if(q == 1){

        question = poss_Questions.get(q);

    } else if(q == 2){

        doMath();

        question = poss_Questions.get(q);

    }


扬帆大鱼
浏览 114回答 2
2回答

jeck猫

我实际上键入了您的程序。问:Eclipse 程序是否需要三分钟或更长时间才能启动?答:不,除非你有一台VERRRY慢速计算机,或者你已经超过了RAM并且你正在达到交换。问:您的程序在“看到某些内容”之前是否需要长达三分钟的时间?答:是的。建议:在“main()”中设置一个断点,然后按“F6”一次单步执行一行(和/或按“F5”单步执行要检查的函数)。提示:您的程序应该会迅速启动,事情会很快发生...直到您点击“getQuestion()”;)强烈建议:“编程”的一个重要部分是学习如何进行故障排除和调试。这是一个很好的机会,可以熟悉如何使用 Eclipse 调试器来单步执行代码。

繁星淼淼

该程序会进入一个“无限”循环。如果你在上一个循环中查看 getQuestion():if (L1.getText().length() < 16) {&nbsp; &nbsp;for (int z = 16; z > L1.getText().length(); z++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L1.setLocation(L1.getX() + 1, L1.getY());&nbsp; &nbsp;}当 L1.getText().length() < 16 时,此循环从 16 开始,因此 z > L1.getText().length() 根据定义是正确的。当 z 随着每个循环递增而递增时,z 会变得更大,甚至会更符合条件。所以它是无休止地增加的。好吧,不是无休止的 - 当它变得足够大时,它会溢出并变成负面的。这是它停止的时候。在下面的代码中,我将增量替换为 z 的递减。虽然我不知道它是否在逻辑上是你想要的 - 它消除了无限循环,程序来得更快。通过显示此错误导致您的问题:import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;public class Main { //gives questions (source not included, keylistener included (currently not working), actionlistener included)&nbsp; &nbsp; static JRadioButton b1;&nbsp; &nbsp; static JRadioButton b2;&nbsp; &nbsp; static JFrame frame;&nbsp; &nbsp; static JPanel panel;&nbsp; &nbsp; static JLabel L1;&nbsp; &nbsp; static JLabel L2;&nbsp; &nbsp; static JTextField tfield;&nbsp; &nbsp; static ButtonGroup bg = new ButtonGroup();&nbsp; &nbsp; static JButton B1;&nbsp; &nbsp; static double tax;&nbsp; &nbsp; static boolean married;&nbsp; &nbsp; static ArrayList<String> poss_Questions = new ArrayList<String>();&nbsp; &nbsp; private static int q=0;// Only need 2 buttons because there is only one prompt (yes or no)&nbsp; &nbsp; public static String getQuestion() {&nbsp; &nbsp; &nbsp; &nbsp; String question = "";&nbsp; &nbsp; &nbsp; &nbsp; if (q == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; question = poss_Questions.get(q);&nbsp; &nbsp; &nbsp; &nbsp; } else if (q == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; question = poss_Questions.get(q);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b1.setVisible(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b2.setVisible(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tfield.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B1.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; } else if (q == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doMath();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; question = poss_Questions.get(q);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tfield.setVisible(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B1.setVisible(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L2.setText(Double.toString(tax)); //fix to make output more good&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L2.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; q++;&nbsp; &nbsp; &nbsp; &nbsp; L1.setLocation(190, 20);&nbsp; &nbsp; &nbsp; &nbsp; if (L1.getText().length() > 16) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int t = 16; t < L1.getText().length(); t++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L1.setLocation(L1.getX() - 1, L1.getY());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (L1.getText().length() < 16) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int z = 16; z > L1.getText().length(); z--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L1.setLocation(L1.getX() + 1, L1.getY());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return question;&nbsp; &nbsp; }&nbsp; &nbsp; public static void checkAnswer() {&nbsp; &nbsp; &nbsp; &nbsp; if (L1.getText().equals(poss_Questions.get(0))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (b1.isSelected()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; married = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (b2.isSelected()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; married = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static int num;&nbsp; &nbsp; public static void doMath() {&nbsp; &nbsp; &nbsp; &nbsp; if (married) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num = Integer.parseInt(tfield.getText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (NumberFormatException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Please enter a whole number above zero without decimal points, commas", "ERROR", JOptionPane.ERROR_MESSAGE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //may work&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (num > 0 && num <= 16000) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tax = num * 0.10; // 10%&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (num > 16000 && num <= 64000) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tax = (1600 + (0.15 * (num - 16000)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (num > 64000) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tax = (8800 + (0.25 * (num - 64000)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Please enter a value greater than 0, without decimal points, and not in a string format", "Invalid Entry", JOptionPane.ERROR_MESSAGE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else if (!married) { //if single&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num = Integer.parseInt(tfield.getText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (NumberFormatException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Please enter a whole number above zero without decimal points, commas", "ERROR", JOptionPane.ERROR_MESSAGE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //use else if loops and else (else prints out that there was an error)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (num > 0 && num <= 8000) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tax = num * 0.10; // 10%&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (num > 8000 && num <= 32000) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tax = (800 + (0.15 * (num - 8000)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (num > 32000) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tax = (4400 + (0.25 * (num - 32000)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Please enter a value greater than 0, without decimal points, and not in a string format", "Invalid Entry", JOptionPane.ERROR_MESSAGE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; poss_Questions.add("Are you married?");&nbsp; &nbsp; &nbsp; &nbsp; poss_Questions.add("How much do you make? ($$ per year)");&nbsp; &nbsp; &nbsp; &nbsp; poss_Questions.add("Here is how much tax will be taken away");&nbsp; &nbsp; &nbsp; &nbsp; b1 = new JRadioButton();&nbsp; &nbsp; &nbsp; &nbsp; b1.setText("Yes");&nbsp; &nbsp; &nbsp; &nbsp; b2 = new JRadioButton();&nbsp; &nbsp; &nbsp; &nbsp; b2.setText("No");&nbsp; &nbsp; &nbsp; &nbsp; b1.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; b2.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; b1.setBounds(75, 150, 200, 30);&nbsp; &nbsp; &nbsp; &nbsp; b2.setBounds(300, 150, 200, 30);&nbsp; &nbsp; &nbsp; &nbsp; bg.add(b1);&nbsp; &nbsp; &nbsp; &nbsp; bg.add(b2);&nbsp; &nbsp; &nbsp; &nbsp; B1 = new JButton();&nbsp; &nbsp; &nbsp; &nbsp; B1.setText("Submit");&nbsp; &nbsp; &nbsp; &nbsp; B1.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; B1.setLocation(340, 50);&nbsp; &nbsp; &nbsp; &nbsp; B1.setSize(75, 25);&nbsp; &nbsp; &nbsp; &nbsp; B1.addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkAnswer();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L1.setText(getQuestion());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; tfield = new JTextField();&nbsp; &nbsp; &nbsp; &nbsp; tfield.setVisible(false);&nbsp; &nbsp; &nbsp; &nbsp; tfield.setBounds(100, 50, 200, 20);&nbsp; &nbsp; &nbsp; &nbsp; L1 = new JLabel();&nbsp; &nbsp; &nbsp; &nbsp; L1.setText(getQuestion());&nbsp; &nbsp; &nbsp; &nbsp; L1.setSize(400, 20);&nbsp; &nbsp; &nbsp; &nbsp; L1.setLocation(10, 20);&nbsp; &nbsp; &nbsp; &nbsp; L2 = new JLabel();&nbsp; &nbsp; &nbsp; &nbsp; L2.setVisible(false);&nbsp; &nbsp; &nbsp; &nbsp; L2.setSize(400, 20);&nbsp; &nbsp; &nbsp; &nbsp; L2.setLocation(10, 60);&nbsp; &nbsp; &nbsp; &nbsp; L2.setText("Something went wrong");&nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, L2.getX() + " " + L2.getY());&nbsp; &nbsp; &nbsp; &nbsp; panel = new JPanel();&nbsp; &nbsp; &nbsp; &nbsp; panel.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; frame = new JFrame();&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; frame.add(panel);&nbsp; &nbsp; &nbsp; &nbsp; frame.setSize(new Dimension(480, 270));&nbsp; &nbsp; &nbsp; &nbsp; frame.setResizable(false);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(b1);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(b2);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(B1);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(L1);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(tfield);&nbsp; &nbsp; &nbsp; &nbsp; panel.add(L2);&nbsp; &nbsp; &nbsp; &nbsp; panel.setLayout(null);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java