2014-12-16 2 views
-2

모든 백 엔드 "계산"내 while 루프를 수행하는 클래스 중 하나는 한 번만 실행되는 것 같습니다. 내 코드는 모두 작동하는 것처럼 보이고 심지어 출력도 나오지만 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

버디를 한 번 확인해보십시오. 버디는 프로그래머 코드에서 가장 큰 단서입니다. –

+2

코드 서식이 좋지 않다는 점에서 @BhargavRao를 두 번 사용하게됩니다. 그러면 코드 작성이 어려워 질 수 있습니다. 코드 서식, 특히 들여 쓰기는 코드를 이해하기 쉽게 만들어 주며,보기 좋게 보이도록합니다. 제쳐두고, 당신은 그 코드에서 과도한 정적 ** 수정자를 과도하게 사용하고 있으며 정적 수정의 90 % 이상을 제거해야합니다. Java는 코드의 복잡성을 줄이고 코드 재사용을 늘리는 데 도움이되는 OOP 토대를 갖추고 있습니다. 코드를 정적으로 과도하게 사용하여 비 OOP로 만들면 길을 잃을 수 있습니다. –

+0

240 줄을 줄일 수있는 최소 코드입니다. – khelwood

답변

2

breakswitch 성명 외부로 호출 할 때마다 즉시 while 루프를 종료합니다.

루프의 시작 부분으로 이동하려면 continue을 대신 사용하십시오.

+0

스위치 외부에 휴식이 없습니다. 실제로는 if- 그렇지 않으면 차단하십시오. –

+0

@maremp 예 있습니다. 전환 후 if 블록에서. – kiheru

+0

예, 바보입니다. 예전에 사례 구조를 사용하려고했는데 실수로 ifs로 바꿔 놓았습니다. –

관련 문제