Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 而循环在我的多类程序中只运行一次_Java_Swing_While Loop - Fatal编程技术网

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

Java 而循环在我的多类程序中只运行一次,java,swing,while-loop,Java,Swing,While Loop,在我的一个完成所有后端“计算”的类中,while循环似乎只运行一次。我的代码似乎都能正常工作,我甚至得到了一个输出,但只有在while循环只运行了一次之后。该类的所有输入都来自另一个类,并且输出打印在从中获取输入的同一个类中。任何额外的帮助也将不胜感激 calculating class code: package clock; public class basic { static displayFrame outputs = new displayFrame(); p

在我的一个完成所有后端“计算”的类中,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;
}
}

非常感谢您的帮助,谢谢

无论何时在
switch
语句之外调用
break
,它都会立即退出while循环


如果您想进入循环的开头,请使用
继续

检查括号,伙计,它们是程序员代码中最大的错误。我支持@BhargavRao,因为您的代码格式不好,这使得您和我们都很难调试代码。理解代码格式,特别是缩进,是为了使代码易于理解,而不是为了使代码看起来美观。顺便说一句,您在代码中使用了大量的静态修改器,应该去掉90%以上的静态修改。java有一个OOP基础,帮助减少代码复杂度和增加代码重用,如果你的代码不使用静态的UOP,那么两个东西会被扔到路边。240行是你可以减少的最小代码量。我建议你学习如何使用调试器,这样你就可以自行调试这样的问题。这可以通过调试程序在几秒钟内解决。@JacksonHaile
displayFrame
output
不需要是
静态的
,没有理由
basic
需要引用
displayFrame
,并且您应该始终计算日期/时间,永远不要直接引用
输出
值…开关外部没有中断,实际上是if-else块。@maremp是的,有。在切换后的if块中。是的,我很愚蠢,我之前试图使用一个案例结构来处理中断,但当我用ifsHow替换它时,不小心把它们留在了那里。那是外部切换吗?if-else块有正确定位的花括号,中断是为了这个案例,它不是在我能理解的范围之外。@maremp不是if-else块,那些在切换之后的。
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();
            }
        }
    );
}   
}