Может кто-нибудь помочь мне организовать это методами, так как я не понимаю, как: - PullRequest
0 голосов
/ 07 мая 2018

У меня есть программа, которая позволяет пользователю выбрать преобразование температуры и распечатать его в файле, хранящемся локально. Я создавал эту программу в течение двух недель, поэтому я скорректировал ее, узнав что-то новое, но теперь я хочу, чтобы это было понятно любому программисту, который читает его с помощью методов. Может кто-нибудь помочь мне организовать это методами, так как я не понимаю, как:

import java.util.Scanner;

import java.io.*;

 public class convertOut
  {
   public static void main (String [] args) throws IOException
    {
     File file = new File ("E:\\Csc110\\Tempreture.txt ");
     Scanner inputFile = new Scanner (file);
     Scanner keyboard = new Scanner (System.in);//Scanner
     PrintWriter outputFile = new PrintWriter ("E:\\Csc110\\TempOut.txt " ); 
     String str;
     int loop = 10;

      while (loop <=10)
       {

     //  Separator 



      `
      // Print the invitation

         System.out.println ("For Celcius to Fahrenheit, Press 1"); // this has nothing to do with the code
         System.out.println ("For Fahrenheit to Celcius , Press 2 "); // no actual call to action
         System.out.println ("For fahrenheit to Kelvin, Press 3 "); // this has nothing to do with the code
         System.out.println ("For kelvin to Fahrenheit , Press 4"); // no actual call to action
         System.out.println ("For celcius to kelvin, Press 5"); // this has nothing to do with the code
         System.out.println ("For kelvin to 1celcius , Press 6 "); // no actual call to action


        char A;
         Scanner op = new Scanner (System.in);
         A = keyboard.next().charAt(0);
         if (A=='1') // C2f

     {



     while (inputFile.hasNext())
      { 
       str = inputFile.nextLine (); 
      int y = Integer.parseInt(str);
       double x = y * 9/5 + 32;


       outputFile.println (str + " degrees Celsius  converted to Fahrenheit  is : " + x + " Degrees" );
       }
       }
       else  if (A=='2') // f2c

     {



     while (inputFile.hasNext())
      { 
       str = inputFile.nextLine (); 
      int y = Integer.parseInt(str);
       double x = (y-32)*5/9;


       outputFile.println (str + " degrees Fahrenheit  converted to Celcius  is : " + x + " Degrees" );
       }
       }

       else  if (A=='3') // f2k

     {



     while (inputFile.hasNext())
      { 
       str = inputFile.nextLine (); 
      int y = Integer.parseInt(str);
       double x = (y+ 459.67) * 5/9;


       outputFile.println (str + " degrees Fahrenheit  converted to Kelvin  is : " + x + " Degrees" );
       }
       }

           else  if (A=='4') // k2f

     {



     while (inputFile.hasNext())
      { 
       str = inputFile.nextLine (); 
      int y = Integer.parseInt(str);
       double x = y *9/5 - 459.67;


       outputFile.println (str + " degrees Kelvin  converted to Fahrenheit  is : " + x + " Degrees");
       }
       }


                else  if (A=='5') // C2K

     {



     while (inputFile.hasNext())
      { 
       str = inputFile.nextLine (); 
      int y = Integer.parseInt(str);
       double x = y + 273.15;


       outputFile.println (str + " degrees Celsius  converted to Kelvin  is : " + x + " Degrees" );
       }
       }



                else  if (A=='6') // K2C

     {



     while (inputFile.hasNext())
      { 
       str = inputFile.nextLine (); 
      int y = Integer.parseInt(str);
       double x = y - 273.15 ;


       outputFile.println (str + " degrees Kelvin  converted to Celcius  is : " + x + " Degrees"  );
       }
       }




        outputFile.close();
        System.out.println ("Its Converted!\nDone!\nThe file name is: TempOut.txt ");

         double F ;
            System.out.println ("\n Enter 1 to or any other number to end. ");
            F = keyboard.nextDouble(); 
            if (F==1)
             {
              loop =1;
              }
               else 
                {
                 loop=12;
                 }



        }
    }
    }

1 Ответ

0 голосов
/ 07 мая 2018

несколько предложений по улучшению вашего кода: измените if else, чтобы переключить регистр, и в каждом случае переключателя вызовите соответствующий метод: Пример метода может быть:

private void cToF(inputFile,outputFile){
while (inputFile.hasNext())
      { 
       str = inputFile.nextLine (); 
       int y = Integer.parseInt(str);
       double x = y * 9/5 + 32;
       outputFile.println (str + " degrees Celsius  converted to Fahrenheit  is : " + x + " Degrees" );
       }
       }
}

Вы можете вызывать этот метод в случае переключения как:

case '1' : cToF(inputFile,outputFile);
           break;

Прокомментируйте, если вам нужно разъяснение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...