يمكن لأي شخص مساعدتي في 2 قضايا جافا لدي. 1 هو محاولة الصيد 2 وحيث أن وضع قطعة من التعليمات البرمجية

StackOverflow https://stackoverflow.com/questions/1633763

  •  06-07-2019
  •  | 
  •  

سؤال

ولدي قطعة من الترميز وأنا أعمل على لمهمة ليوني وفي الحقيقة أنا لم يكن كبيرا مع جافا، ولكن لقد حاولت. أنا أحاول الحصول على بلدي محاولة الصيد في العمل لكنه لم يبدو أن تفعل ما هو عليه suppoes ل. أعتقد أن لدينا باستثناء خطأ ولكن لست متأكدا من استثناء لاستخدام لهذه المشكلة وأنا في محاولة لوقف الرسائل التي تكتب في وأرقام فقط.

والمشكلة الأخيرة أواجه هي أنني لا أعرف من أين لوضع هذه القطعة من الترميز:

 {
                   while (true)
        {
            cancel();
        }
 } 

ولست متأكدا في أي جزء هو suppoes للذهاب.

وأي مساعدة سيكون موضع و appricated إلى حد كبير.

وهذا هو قانون بلدي، أنا آسف إذا كان القليل من الفوضى. الترميز

هل كانت مفيدة؟

المحلول

والتعليمات البرمجية غير مطول جدا - معظم التعليقات غير ضرورية (حتى ولو كنت قد دعا إلى إدراجها) وذلك بدلا من:

minutesfromhours = 60*minutes;
                //To change the hours into minutes by diving the hour by 60 for departure.

هل يمكن كتابة:

public final static int MINUTES_PER_HOUR = 60;
//...

minutes = MINUTES_PER_HOUR * hours;

وعندما يكون لديك مرتب رمز سيكون من الأسهل كثيرا أن نرى المنطق.

نصائح أخرى

وأنصحك لتعلم كيفية استخدام المصحح IDE الخاص بك. هناك بعض عظيمة مقاطع الفيديو التعليمية المجانية هنا .

ومع المصحح لم تعد سوف تضطر إلى وضع البيانات println جميع أنحاء التعليمات البرمجية.

وحقا، تأخذ من الوقت.

ولديك حاول / catch ربما لا العمل

 try
 {
    depart= Double.parseDouble(departtime);
    System.out.println(depart);
 }
 catch (NumberFormatException e)
 {
     System.out.println(e.getMessage());
 }
 System.out.println("Time accepted");

ولكن كنت تقع من خلال التعليمات البرمجية التي ثم بمعالجة المدخلات <م> بغض النظر . تحتاج إلى الخروج من روتين في كتلة catch الخاص بك، ومعالجة المدخلات صالح في كتلة try. منها مثلا.

try
 {
    depart= Double.parseDouble(departtime);
    System.out.println(depart);
    System.out.println("Time accepted");
    // do further processing here
 }
 catch (NumberFormatException e)
 {
     System.out.println(e.getMessage());
     // exit here
 }

وهنا هو الإصلاح. على محمل الجد، لأول مرة اعتقدت الخاص بك لا يعمل. ولكن أنا تنسيقه في IDE بلدي، وأضاف مجرد الأسلوب الرئيسي لاستدعاء الأسلوب الخاص بك إلغاء (). أنه يعمل.

import javax.swing.JOptionPane;

public class Time
{
   public int difference =0;
// Stores the difference of departure and arrival time.

   public static void main(String args [])
   {
       Time obj=new Time();

            while (true)

              obj.cancel();

   }

public Time()
    {
      String departtime = JOptionPane.showInputDialog("Enter Depart Time in 24 hour time:");
// Allows the user to input their departure time into a JOptionPane.
      String arrivaltime = JOptionPane.showInputDialog("Enter Arrival Time in 24 hour time:");
// Allows the user to input their arrival time into a JOptionPane.

      double depart = 0;
// Store the time the user first inputs as a double.
      double arrival = 0;
// Store the time the user inputs secondly as a double.

      int minutes = 0;
// To store the hours for departure.

      int minutes2 = 0;
// To store the minutes of departure.

      int totalminutesfordeparture = 0;
// To store the full time for departure as minutes.

      int minutesfromhours = 0;
// To store the hours as minutes for depature

     int arrivals = 0;
// To store the hours arrival.

     int arrival2 = 0;
// To store the minutes for arrival.

      int arrivalhoursasminutes = 0;
// To store the arrival hours as minutes.

      int totalminutesforarrival = 0;
// To store the full time for departure in minutes.

     int actualtimehours= 0;
// The number of hours it will take on the journey.

     int actualtimeminutes=0;
// The number of minutes it will take on the journey.






// **Start of removing the decimal for time 1 and time 2**\\
     {
         // Doesn't work
         try
         {
         depart= Double.parseDouble(departtime);
         System.out.println(depart);
         }
         catch (NumberFormatException e)
         {
             System.out.println(e.getMessage());
         }
         System.out.println("Time accepted");

         arrival= Double.parseDouble(arrivaltime);



         int time = (int)(depart*100);
            // Gets rid of the decimal point in the departure time.

         System.out.println ("Time with no decimal point "+ time);
             // Check the decimal is being removed.

         int time2=(int)(arrival*100);
             // Gets rid of the decimal point in arrival time.

         System.out.println ("Time with no decimal point "+ time2);
             // Check the decimal is being removed in arrival.

// **End of removing the decimal in the times**\\

// **Start of seperating the hours and minutes in departure time**\\
         {

             minutes2=time%100;
                 // To separate the minutes from the hours for departure.

            minutes=time/100;
                 // To seperate the hours ready to change them into minutes.

            System.out.println("Hours of departure "+ minutes);
                 // Check that the hours are seperating from the minutes for
                    // departure.

            System.out.println("Minutes of departure "+ minutes2);
                // Check that the minutes are seperating from the hour for
                // departure.

           arrival2=time2%100;
                 // To separate the minutes from the hours.

            arrivals=time2/100;
                 // To seperate the hours ready to change them into minutes.

            System.out.println("Hours of arrival "+ arrivals);
                // Check that the hours are seperating from the minutes for
                // arrivals.

            System.out.println("Minutes of arrival "+ arrival2);
                // Check that the minutes are seperating from the hour for
                // arrivals.
         }
// **End of seperating the hours and minutes in departure time**\\

// **Start of hours being changed to minutes and adding it all up**\\
         {
             minutesfromhours = 60*minutes;
                // To change the hours into minutes by diving the hour by 60 for
                // departure.

             System.out.println("Hours into minutes "+ minutesfromhours);
                // Checks that the hours are being turned into minutes for
                // departure.

             totalminutesfordeparture= minutesfromhours+minutes2;
                // To add together the hour minutes and the minutes from the
                // time to give the total time in minutes for departure.

             System.out.println("Total Departure time in minutes "+ totalminutesfordeparture);
                // Checks that the hours as minutes are being added up with the
                // minutes of the time for departure.


             arrivalhoursasminutes = 60*arrivals;
                // To change the hours into minutes for arrivals by diving the
                // hours by 60 for arrivals.

             System.out.println("Hours into minutes for arrival "+ arrivalhoursasminutes);
                // Check that it is adding up the hour minutes and the minutes
                // for departure.

             totalminutesforarrival= arrivalhoursasminutes+arrival2;
                // To add together the hour minutes and the minutes from the
                // arrivals.

             System.out.println("Total arrival time in minutes "+ totalminutesforarrival);
                // Check that it is adding up the hour minutes and the minutes
                // for arrivals
         }

// **End of hours being changed to minutes and adding up**\\

// **Start of Finding the difference in minutes**\\
         {

             difference=totalminutesforarrival-totalminutesfordeparture;
                // Works out the difference of minutes by taking arrival time in
                // minutes away from the departure time in minutes.
             System.out.println("Difference "+ difference);
                // Check to see that it is taking arrival from departure.

             JOptionPane.showMessageDialog(null, "It will take " + difference);
         }
            // **End of finding the difference in minutes**\\

         // **start of not working changing minutes back to hours.**\\

         {
             actualtimehours= difference/60;
             actualtimeminutes= difference/60;

             System.out.println("It will take "+ actualtimehours);
             System.out.println("It will take "+ actualtimeminutes);


         }

     }

    }

// ** Method incase cancel button is pressed **\\
        public void cancel()
    {
        String input=JOptionPane.showInputDialog("Cancel button was pressed");
        if (input==null)
        {
                System.exit(0);
        }
        }
}

وبعض التعليقات العامة.

ويجب نقل معظم هذا الرمز من منشئ الوقت ()، وإلى أسلوب الرئيسي. هذا الرمز لم يقم أي علاقة مع instantiating كائن الوقت.

وحين حلقة الخاصة بك ينبغي أن ترفق كل ما كنت أود أن أكرر. في هذه الحالة، يطلب من المستخدم لفترة المغادرة، وقت وصوله، وحساب الفرق.

ولقد تكرار الرمز، لماذا لا يكون وسيلة ليطلب من المستخدم إدخال سلسلة الوقت، وتحليل ذلك. شيء من هذا القبيل

public class Time {
    private int hours;
    private int minutes;
    etc...
}    

// in main
while (true) {
    Time departTime = askUser("depart");
    Time arriveTime = askUser("arrive");
    calculateDifference(departTime, arriveTime);
}

// elsewhere
public Time askUser(String name) {
    String theTime = JOptionPane.showInputDialog(
        String.format("Enter %s Time in 24 hour time:", name));
    Time result = parseTime(theTime, name);
    return result;
}

وطيب لقد الرقم بها الآن :) بعد قليل من النوم والصباح الباكر والتفكير الدقيق.

أولا لقد وضعت كل الاشياء القيام هاما في أساليب تحرير منطقة منشئ بلدي مثل هذا العدد الكبير من قلت لي القيام به، ونعم أنا أتفق أنه من الأسهل كثيرا أن نرى ما يجري الآن.

لحل قضية محاولة الصيد. أدركت هذا الصباح أنني كنت وضعه في المكان الخطأ وانها لا تحاول ما أردت أن محاولة والخط الرئيسي أردت أن محاولة يعني كان لي أن أضع البعض الترميز داخل كان جيدا وبيان الصيد ينتهي الآن ما اذا كان ضرب. أنا فقط بحاجة لمعرفة كيفية حلقة مرة أخرى في جميع أنحاء بدلا من أن ينتهي.

لحل بلدي المسألة الأخرى التي كانت على زر لقد استخدمت بينما (صحيح) بيان وأيضا وضعه في حيث كان JOptionPane أيضا أن كان 2 أماكن الوحيدة في إلغاء يمكن أن يكون الضغط إلغاء ... وأنا لا أعرف إذا كان هذا هو واحد الحق في ذلك اذا كان هناك من يستطيع أن يقول لي إذا كان (أو أفضل من ذلك سوف تعلق بها في تلك الأماكن)

وحتى هنا هو رمز العمل، لا يزال هناك زوجين من الخلل في ذلك مثل ولدي لمعرفة كيفية قصرها على مجرد hh.mm كما في لحظة يمكن أن أضع أي وقت عشوائي في. أنا أيضا بحاجة إلى إيجاد على كيفية التعامل مع 24 ساعة مع 00 مرة لأنه لا يعالج ذلك على الإطلاق في الوقت الحالي، كما أنها لا ترغب في ذلك إذا وضعت 12.00 و 3.00 في أي أنها تأتي مع -9 أو ما عملت من ذلك أن يكون، مرة أخرى أن حوالي 24 ساعة إدارة وشيء صغير آخر لديه خطأ في ذلك هو أنه إذا كنت خطأ، فإنه سيتم إغلاق بدلا من حلقة والتي سوف تكون محاولة لفرز الآن.

الترميز

وشكرا لمساعدة الجميع الليلة الماضية لكم جميعا ساعدني كثيرا

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top