修复一个简单的java程序的问题

时间:2013-12-12 14:12:01

标签: java

问题解决了,不再需要,因为我意识到这是一个愚蠢的问题

4 个答案:

答案 0 :(得分:2)

更改

    String str = next();
    str= toUpperCase();

    String str = s.next();
    str = str.toUpperCase();

并且没有方法fahrenheitToCelcius

另一个问题是for(int i=0; i<=10; i++);无法工作。 这是固定代码:

import java.util.Scanner;

/**
 *
 * @author rr594
 */
public class ConverTemp {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    double t = 0.0;
    System.out.println ("Enter the temperature to be converted: ");
    double temp = s.nextDouble();
    char c ;
    do{
        System.out.println("Enter\n\tf for fahrenheit\n\tc for celcius");
        String str = s.next();
        str = str.toUpperCase();
        c= str.charAt(0);
        if (c=='C')
            t= celciusToFahrenheit(temp);
        else if (c=='F');
//        t= fahrenheitToCelcius(temp);
    }while(c!='C' && c != 'F');
    printResult(c,temp,t);
     }
    public static double celciusToFahrenheit(double temp_celcius){
        double temp_fahrenheit;
        temp_fahrenheit = 32.0 +9.0/5.0 *temp_celcius;
        return temp_fahrenheit;
    }
    public static void printResult(char ch, double t_in, double t_out){
        String unit1 = (ch=='C')? "Celcius" : "Fahrenheit";
        String unit2 = (ch != 'C')? "Celcius" : "Fahrenheit";
        System.out.println(t_in + "degree" + unit1 +"="+t_out+ "degree" + unit2);
    }
    public static void conversions(double celciusToFahrenheit){
    double c;
    double f;
    for(int i=0; i<=10; i++) {
        c= 10.0 *(double) i;
        f = celciusToFahrenheit(c);
        System.out.printf("%f degree Celcius = %f degree Fahrenheit\n", c,f);
    }

    }
}

您需要做的是实施fahrenheitToCelcius。你需要用大写字母输入c或f,否则你会神经紧张地离开while循环。

输入/输出示例:

Enter the temperature to be converted: 
40
Enter
    f for fahrenheit
    c for celcius
C
40.0degreeCelcius=104.0degreeFahrenheit

答案 1 :(得分:0)

在此处删除分号:

 else if (c=='F');

这样你就得到了

 if (c=='C') {
    t= celciusToFahrenheit(temp);
 } else if (c=='F') {
    t= fahrenheitToCelcius(temp);
 }

使用括号,更容易发现错误。

同时阅读所有其他答案,因为他们指出了程序中的其他问题; - )

答案 2 :(得分:0)

从elseif

中删除分号
c= str.charAt(0);
        if (c=='C')
            t= celciusToFahrenheit(temp);
        else if (c=='F')
        ----------------^

答案 3 :(得分:0)

这是你的工作解决方案(包括celcius到farenheit):

package convertemp;

import java.util.Scanner;

/**
 *
 * @author rr594
 */
public class Convertemp {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    double t = 0.0;
    System.out.println ("Enter the temperature to be converted: ");
    double temp = s.nextDouble();
    char c ;
    do{
        System.out.println("Enter\n\tF for fahrenheit\n\tC for celcius");
        String str = s.next();
        str.toUpperCase();
        c= str.charAt(0);
        if (c=='C')
            t= celciusToFahrenheit(temp);
        else if (c=='F')
        t= fahrenheitToCelcius(temp);
    }while(c!='C' && c != 'F');
    printResult(c,temp,t);
     }
public static double celciusToFahrenheit(double temp_celcius){
    double temp_fahrenheit;
    temp_fahrenheit = 32.0 +9.0/5.0 *temp_celcius;
    return temp_fahrenheit;
}

public static double fahrenheitToCelcius(double temp_fahrenheit){
    double temp_celcius;
    temp_celcius = (temp_fahrenheit-32)*5.0/9.0;
    return temp_celcius;
}
public static void printResult(char ch, double t_in, double t_out){
    String unit1 = (ch=='C')? "Celcius" : "Fahrenheit";
    String unit2 = (ch != 'C')? "Celcius" : "Fahrenheit";
    System.out.println(t_in + "degree" + unit1 +"="+t_out+ "degree" + unit2);
}
public static void conversions(double celciusToFahrenheit){
double c = 0;
double f;
for(int i=0; i<=10; i++)
    c= 10.0 *(double) i;
    f = celciusToFahrenheit(c);
    System.out.printf("%f degree Celcius = %f degree Fahrenheit\n", c,f);

}


}