использование методов в первый раз для преобразователя температуры
Я использую методы в первый раз, моя программа работает в основном, за исключением того, что она не зацикливает оператор преобразования с вопросом, который ему соответствует... Пользовательский ввод соответствует оператору преобразования, поэтому 3 раза подскажет
Конверсия № 1
...
Конверсия № 2
...
Конверсия № 3
...
также преобразование между farenheight в целис работает, но не от celsius до farenheight, любое понимание было бы полезно ниже, это мой код,
import java.util.Scanner;
import java.text.DecimalFormat;
public class TempConverter {
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("#,###.0");
System.out.println("Temperature Converter");
System.out.println("---------------------");
System.out.println();
Scanner input = new Scanner(System.in);
System.out.print("How many conversions would you like to make: ");
int conversions=input.nextInt();
for(int i = 1; i < conversions; i++){
System.out.println("Conversion # " + i++);
System.out.println();
System.out.println ("To convert from celsius to fahrenheit type 1 ");
System.out.print ("To convert from fahrenheit to celsius type 2: ");
int choice=input.nextInt();
switch(choice){
case 1:
System.out.println();
System.out.print ("Enter a celsius temperature: ");
double cels=input.nextDouble();
double result=celsiusToFahrenheit(choice,cels);
System.out.println();
System.out.println ("The conversion of "+cels+" from celcius to fahrenheit is "+df.format(result) );
break;
case 2:
System.out.println();
System.out.print("Enter a farenheight temperature: ");
double fahr=input.nextDouble();
double result1=fahrenheitToCelsius(choice,fahr);
System.out.println ("The conversion of "+fahr+" from fahrenheit to celcius is "+df.format(result1) );
}
}
}
public static double celsiusToFahrenheit(int choice, double cels)
{
double converted=0.0;
if (choice == 1)
converted=9.0/5.0*cels+32;
return converted;
}
public static double fahrenheitToCelsius(int choice, double fahr)
{
double converted2=0.0;
if (choice ==2)
converted2=5.0/9.0*(fahr-32);
return converted2;
}
}
У вас две ошибки:
(1) Эта строка неверна:
for(int i = 1; i < conversions; i++)
Это означает, что цикл будет выполняться до тех пор, пока i < conversions
. Если conversions
3, это означает, что он будет зацикливаться с i==1
и i==2
, но он не будет зацикливаться на i==3
потому что 3<3
является false
. Используйте <=
.
(2) i++
в приведенном выше for
утверждения будет увеличиваться i
каждый раз, когда он закругляется. Это то, что вы хотите. Тем не менее, вы побеждаете, i++
еще i++
в код, который увеличит его на дополнительное время.
код цикла неверен, и в случае с переключателем case2 нет break
. найти исправленный код здесь:
import java.text.DecimalFormat;
import java.util.Scanner;
public class TempConverter {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#,###.0");
System.out.println("Temperature Converter");
System.out.println("---------------------");
System.out.println();
Scanner input = new Scanner(System.in);
System.out.print("How many conversions would you like to make: ");
int conversions = input.nextInt();
for (int i = 1; i <= conversions; i++) {
System.out.println("Conversion # " + i);
System.out.println();
System.out.println("To convert from celsius to fahrenheit type 1 ");
System.out.print("To convert from fahrenheit to celsius type 2: ");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println();
System.out.print("Enter a celsius temperature: ");
double cels = input.nextDouble();
double result = celsiusToFahrenheit(choice, cels);
System.out.println();
System.out.println("The conversion of " + cels + " from celcius to fahrenheit is " + df.format(result));
break;
case 2:
System.out.println();
System.out.print("Enter a farenheight temperature: ");
double fahr = input.nextDouble();
double result1 = fahrenheitToCelsius(choice, fahr);
System.out
.println("The conversion of " + fahr + " from fahrenheit to celcius is " + df.format(result1));
break;
}
}
}
public static double celsiusToFahrenheit(int choice, double cels) {
double converted = 0.0;
if (choice == 1)
converted = 9.0 / 5.0 * cels + 32;
return converted;
}
public static double fahrenheitToCelsius(int choice, double fahr) {
double converted2 = 0.0;
if (choice == 2)
converted2 = 5.0 / 9.0 * (fahr - 32);
return converted2;
}
}