while循环只在我的多类程序中运行一次

时间:2014-12-16 20:42:42

标签: java swing while-loop

在我的一个类中,所有后端“计算”我的while循环似乎只运行一次。我的代码似乎都工作,我甚至得到一个输出,但只有在while循环只运行一次。该类的所有输入都来自另一个类,输出打印在输入来自的同一个类中。任何额外的帮助也将不胜感激。

calculating class code:

package clock;

public class basic {

    static displayFrame outputs = new displayFrame();

    public static String output;

    public static String findDate(int year, int month, int day, int hour, int timeToAdd){

        double yearD = year;
        double leapYear = yearD/4;
        int daysInMonth = 0;
        int daysInYear = 365;
        int casePick = 0;
        int x = 0;

    //Making sure that the number added isn't negative, ones for other inputs MAY come...
    while(x == 0)
    {
    if(Math.abs(timeToAdd) != timeToAdd){
        continue;
    }else{
        x++;
    }

    System.out.println(year);
    System.out.println(month);
    System.out.println(day);
    System.out.println(hour);
    System.out.println(timeToAdd);

    /*Starts the counting loop and for each month value, it will determine how many days are in it
    this will allow for exact measurement.*/
    while(timeToAdd != 0){
        System.out.println(timeToAdd);
        switch(month)
        {
        case 1:
            daysInMonth = 31;
            break;
        case 2:
            if(leapYear % 1 == 0){
            daysInYear = 366;
            daysInMonth = 29;
        }else{
            daysInMonth = 28;
        }
        break;
        case 3:
            daysInMonth = 31;
            break;
        case 4:
            daysInMonth = 30;
            break;
        case 5:
            daysInMonth = 31;
            break;
        case 6:
            daysInMonth = 30;
            break;
        case 7:
            daysInMonth = 31;
            break;
        case 8:
            daysInMonth = 31;
            break;
        case 9:
            daysInMonth = 30;
            break;
        case 10:
            daysInMonth = 31;
            break;
        case 11:
            daysInMonth = 30;
            break;
        case 12:
            daysInMonth = 31;
            break;
        }


        /*Decides whether a year, month, day or hour is left, adds one to its counter,
          and then takes away that amount of time from the hours added that are left*/
        if(timeToAdd >= daysInYear * 24){
            timeToAdd = timeToAdd - (daysInYear * 24);
            year++;
            break;
        }

        if(timeToAdd >= daysInMonth * 24 && timeToAdd < daysInYear * 24){
            timeToAdd =  timeToAdd - (daysInMonth * 24);
            month++;
            break;
        }

        if(timeToAdd >= 24 && timeToAdd < daysInMonth * 24){
            timeToAdd = timeToAdd - 24;
            day++;
            break;
        }

        if(timeToAdd >= 1 && timeToAdd < 24){
            timeToAdd = timeToAdd - 1;
            hour++;
            break;
        }   

    //Makes sure there are never 25 hours, 32 days or 13 months
    if(hour > 24){
        day++;
        hour = 1;
    }

    if(day > daysInMonth){
        month++;
        day = 1;
    }

    if(month > 12){
        year++;
        month = 1;

        System.out.println("The date you have requested is: " + month + "/" + day + "/" + year + " Hour:" + hour);
    }
}   
        output = "The date you have requested is: " + month + "/" + day + "/" + year + " Hour:" + hour;

    }
    System.out.println(timeToAdd);
    return output;
}
}

JFrame类代码:

package clock;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class displayFrame extends JPanel{
    private static final long serialVersionUID = 1L;
    static basic out = new basic();

public static int hrsIn;
public static int daysIn;
public static int monthsIn;
public static int yearsIn;
public static int timeAdd;
public static JButton enter;
public static JLabel dayLabel, hourLabel, monthLabel, yearLabel, timeAdded1, newTime;
public static JTextField dayField, hourField, monthField, yearField, timeAdded;
public static JFrame frame;
public static JPanel newTimePanel;
public static String output = basic.findDate(yearsIn, monthsIn, daysIn, hrsIn, timeAdd);


public displayFrame()
{
    super(new BorderLayout());

    enter = new JButton("Okay");

    dayField = new JTextField(20);
    hourField = new JTextField(20);
    monthField = new JTextField(20);
    yearField = new JTextField(20);
    timeAdded = new JTextField(20);


    hourLabel = new JLabel("Hour: ", JLabel.CENTER);
    dayLabel = new JLabel("Day: ", JLabel.CENTER);
    monthLabel = new JLabel("Month: ", JLabel.CENTER);
    yearLabel = new JLabel("Year: ", JLabel.CENTER);
    timeAdded1 = new JLabel("Added: ", JLabel.CENTER);
    newTime = new JLabel(output, JLabel.CENTER);

    JPanel button = new JPanel(new GridLayout(0,1));
    button.add(enter);

    JPanel fieldPane = new JPanel(new GridLayout(0,1));
    fieldPane.add(hourField);
    fieldPane.add(dayField);
    fieldPane.add(monthField);
    fieldPane.add(yearField);
    fieldPane.add(timeAdded);

    JPanel newTimePanel = new JPanel(new FlowLayout());
    newTimePanel.add(newTime);

    JPanel labels = new JPanel(new GridLayout(0,1));
    labels.add(hourLabel);
    labels.add(dayLabel);
    labels.add(monthLabel);
    labels.add(yearLabel);
    labels.add(timeAdded1);

    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    add(fieldPane, BorderLayout.LINE_END);
    add(labels, BorderLayout.CENTER);
    add(button, BorderLayout.PAGE_END);
}


private static void createAndShowGUI()
{
    frame = new JFrame("Clock Adder");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.add(new displayFrame());
    frame.pack();
    frame.setVisible(true);

    enter.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            hrsIn = Integer.parseInt(hourField.getText());
            daysIn = Integer.parseInt(dayField.getText());
            monthsIn = Integer.parseInt(monthField.getText());
            yearsIn = Integer.parseInt(yearField.getText());
            timeAdd = Integer.parseInt(timeAdded.getText());
            String result = basic.findDate(yearsIn, monthsIn, daysIn, hrsIn, timeAdd);
            JOptionPane.showMessageDialog(frame, result);
        }
    });
}
public static void main(String[] args) {
    System.out.println(output);
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run(){
            createAndShowGUI();
            }
        }
    );
}   
}

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:2)

每当您在break语句之外调用switch时,它都会立即退出您的while循环。

如果您想要转到循环的开头,请改用continue