使用JOptionPane的java中的Celsius,Fahrenheit转换器

时间:2015-10-26 20:20:44

标签: converter joptionpane

我试图写下比简单华氏度更复杂的东西到摄氏度,反之亦然。我试图使用JoptionPane更好地摔倒但是我被困在一个地方并且不知道如何解决这个问题(第32和37行 - 方法不适用于参数())任何帮助将不胜感激

import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class ex74v3 {

    public static void main(String[] args) {
        temp();
        new ex74v3();
    }

    public ex74v3() {

        boolean done=false;
        while(!done){

            done=true;
            String[] ans=new String[11];
            String[] choice={
                    "(°F) to (°C)",
                    "(°C) to (°F)",
            };
            int choice_indx=JOptionPane.showOptionDialog(null, "Choose  type of conversion", "Choice",
                    0,JOptionPane.QUESTION_MESSAGE ,
                    null,choice,0);
            ans[0]=choice[choice_indx];
            if(choice_indx==1 || choice_indx==2) {
                done=false;
            }else{
                choice_indx=JOptionPane.showMessageDialog(null, "Fahrenheit to Celsius: " + baseFtC() + " (°C)");
            }
            if(choice_indx==2) {
                done=false;
            }else{
                choice_indx=JOptionPane.showMessageDialog(null, "Celsius to Fahrenheit : " + baseCtF() + " (°F)");
            }
        }
    }

    public static int temp() {
        String value = JOptionPane.showInputDialog(null, "Enter value ");
        @SuppressWarnings("unused")
        int log;
        return log = Integer.parseInt(value);
    }


    public int baseCtF(int value) {
        int conversion = (int) (temp() * 1.8 + 32);
        return conversion;
    }

    public int baseFtC(int value) {

        int conversion = (int) ((temp() - 32) / 1.8);
        return conversion;
    }
}

1 个答案:

答案 0 :(得分:0)

好的,还有另一种方式,更容易,无论如何,谢谢;]

import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ex74v4 {


public static void main(String[] a) {
  DecimalFormat digit = new DecimalFormat("0.00");
  String value = JOptionPane.showInputDialog(null, "Enter value ");
  float log;
  log = Float.parseFloat(value);
  JFrame frame = new JFrame();
  Object stringArray[] = { "Celsius to Fahrenheit", "Fahrenheit to Celsius" };
  int reply = JOptionPane.showOptionDialog(frame, "Choose conversion type of value: " + digit.format(log), "MiniConverter",
    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, stringArray,
    stringArray[0]);
  if (reply == JOptionPane.YES_OPTION) {
      DecimalFormat digit2 = new DecimalFormat("0.00");
      double conversion = log * 1.8 + 32;
      JOptionPane.showMessageDialog(frame, log + " " + "(°C) equals " + digit2.format(conversion) + " (°F)", "MiniConverter", JOptionPane.INFORMATION_MESSAGE);
      System.exit(0);
    }
  else {
      DecimalFormat digit3 = new DecimalFormat("0.00");
      double conversion = (log - 32) / 1.8;
      JOptionPane.showMessageDialog(frame, log + " " + "(°F) equals " + digit3.format(conversion) + " (°C)", "MiniConverter", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
    }

}

}