当前位置: 代码迷 >> Java相关 >> HELP!JAVA题。(菜鸟级)问题见代码!
  详细解决方案

HELP!JAVA题。(菜鸟级)问题见代码!

热度:166   发布时间:2005-08-15 14:32:00.0
HELP!JAVA题。(菜鸟级)问题见代码!

我从论坛里找了这么一道题,然后想增加修改一下,可是因为刚接触JAVA,所以有些问题不会改,求助斑竹和各位大侠! 原题是,输入三个数,最后得出乘积。 改后想法,输入三个数和一个符号(+ ,- ,* ,/),根据判断后进行相应的运算并得出结果! 代码写的有点垃圾,请谅解! 问题? 怎么能取到用户输入的运算符号。 import javax.swing.JOptionPane; public class G10s { public static void main(String[] args) { String firstNumber; String secondNumber; String thirdNumber; String F[] = new String[3]; int one; int two; int three; int fuhao; int result; F[0]="+"; F[1]="-"; F[2]="*"; F[3]="/";

firstNumber=JOptionPane.showInputDialog("输入第一个整数"); secondNumber=JOptionPane.showInputDialog("输入第二个整数"); thirdNumber=JOptionPane.showInputDialog("输入第三个整数"); F[3]=JOptionPane.showInputDialog("输入第四个整数");

one = Integer.parseInt(firstNumber); two= Integer.parseInt(secondNumber); three = Integer.parseInt(thirdNumber); fuhao= Integer.parseInt(F[3]);

switch(JOptionPane.showInputDialog (F[3])){ case 0: result=one+two+three; break; case 1: result=one-two-three; break; case 2: result=one*two*three; break; case 3: result=one/two/three; break; default : System.out.print("输入有误"); }

JOptionPane.showMessageDialog(null,"结果是" + result,"求3个整数算法", JOptionPane.PLAIN_MESSAGE);

} } 33行有误。 found: java.lang.string

搜索更多相关的解决方案: JAVA  HELP  String  代码  

----------------解决方案--------------------------------------------------------
看了看又改了改!

import javax.swing.JOptionPane; public class G10s { public static void main(String[] args) { String firstNumber; String secondNumber; String thirdNumber; String F[] = new String[3]; int one; int two; int three; String fuhao; int result; F[0]="+"; F[1]="-"; F[2]="*"; F[3]="/";

firstNumber=JOptionPane.showInputDialog("输入第一个整数"); secondNumber=JOptionPane.showInputDialog("输入第二个整数"); thirdNumber=JOptionPane.showInputDialog("输入第三个整数"); F[3]=JOptionPane.showInputDialog("输入符号");

one = Integer.parseInt(firstNumber); two= Integer.parseInt(secondNumber); three = Integer.parseInt(thirdNumber); fuhao= Integer.parseInt(F[3]);

switch(JOptionPane.showInputDialog (F[3])){ case 0: result=one+two+three; break; case 1: result=one-two-three; break; case 2: result=one*two*three; break; case 3: result=one/two/three; break; default : System.out.print("输入有误"); }

JOptionPane.showMessageDialog(null,"结果是" + result,"求3个整数算法", JOptionPane.PLAIN_MESSAGE);

} }


----------------解决方案--------------------------------------------------------
import javax.swing.JOptionPane;
public class G10s
{
  public static void main(String[] args)
  {
    String firstNumber;
    String secondNumber;
    String thirdNumber;
    String F[] = new String[4];
  
    int one;
    int two;
    int three;
    String fuhao;
    int result = 0;
    boolean inputValid = true;  
    F[0]="+";
    F[1]="-";
    F[2]="*";
    F[3]="/";

    firstNumber=JOptionPane.showInputDialog("输入第一个整数");
    secondNumber=JOptionPane.showInputDialog("输入第二个整数");
    thirdNumber=JOptionPane.showInputDialog("输入第三个整数");
    fuhao = JOptionPane.showInputDialog("输入符号");

    one = Integer.parseInt(firstNumber);
    two= Integer.parseInt(secondNumber);
    three = Integer.parseInt(thirdNumber);
   
    if(fuhao.equals("+"))
      result = one + two + three;
    else if(fuhao.equals("-"))
      result = one - two - three;
    else if(fuhao.equals("*"))
      result = one * two * three;
    else if(fuhao.equals("/") && two != 0 && three != 0)
      result = one / two / three;
    else
      inputValid = false;
    if(inputValid)   
      JOptionPane.showMessageDialog(null,"结果是 " + result,"求3个整数算法",
                               JOptionPane.PLAIN_MESSAGE);
    else
      JOptionPane.showMessageDialog(null, "Input invalid.",
                                   "Input error", JOptionPane.ERROR_MESSAGE);
  }
}
----------------解决方案--------------------------------------------------------
thanks!
----------------解决方案--------------------------------------------------------
版主,你这两句是什么意思?
boolean inputValid = true;
JOptionPane.showMessageDialog(null, "Input invalid.",
                                   "Input error", JOptionPane.ERROR_MESSAGE);
版主你真棒!!
----------------解决方案--------------------------------------------------------
作为判断啊,如果输入正常的话,就输入结果,也就是显示那个显示结果的对话框,

否则的话,就显示那个出错对话框。


----------------解决方案--------------------------------------------------------

版主,我又试了试,不用定义也行!是吧? 还要麻烦你一件事,我用switch语句实现不了这个程序吗? import javax.swing.JOptionPane; public class G10t { public static void main(String[] args) { String firstNumber; String secondNumber; String thirdNumber; int one; int two; int three; String fuhao; int result = 0; int d; boolean inputValid = true;

firstNumber=JOptionPane.showInputDialog("输入第一个整数"); secondNumber=JOptionPane.showInputDialog("输入第二个整数"); thirdNumber=JOptionPane.showInputDialog("输入第三个整数"); fuhao = JOptionPane.showInputDialog("输入符号");

one = Integer.parseInt(firstNumber); two= Integer.parseInt(secondNumber); three = Integer.parseInt(thirdNumber); d= Integer.parseInt(fuhao); switch (d) { case '+': result = one + two + three; case '-': result = one - two - three; case '*': result = one * two * three; case '/': result = one / two / three; } JOptionPane.showMessageDialog(null,"结果是 " + result,"求3个整数算法", JOptionPane.PLAIN_MESSAGE); } } 我还是太笨了!麻烦了!


----------------解决方案--------------------------------------------------------
是不用定义数组也行!

----------------解决方案--------------------------------------------------------
那个fuhao变量怎么能parse成int呢?
----------------解决方案--------------------------------------------------------
看来你很喜欢用switch, 其实也没有什么不可以,程序在两处地方做了改动,其余地方没有变动,
但必须要讲的是,你的这个程序很脆弱,因为你的这个程序的正确运行是建立在合法输入的基础上,
也就是说一旦输入不合法,那么程序就出错了,没有任何补救的可能,所以这样的程序是不合格的。
import javax.swing.JOptionPane;
public class G10t
{
  public static void main(String[] args)
  {
    String firstNumber;
    String secondNumber;
    String thirdNumber;
  
    int one;
    int two;
    int three;
    String fuhao;
    int result = 0;
    int d;
    boolean inputValid = true;  

    firstNumber=JOptionPane.showInputDialog("输入第一个整数");
    secondNumber=JOptionPane.showInputDialog("输入第二个整数");
    thirdNumber=JOptionPane.showInputDialog("输入第三个整数");
    fuhao = JOptionPane.showInputDialog("输入符号");
    char cFuhao =fuhao.charAt(0);                                 // here pay attention

    one = Integer.parseInt(firstNumber);
    two= Integer.parseInt(secondNumber);
    three = Integer.parseInt(thirdNumber);
  //  d= Integer.parseInt(fuhao);
  
   
    switch(cFuhao)                      // and here pay attention
    {
      case '+':
        result = one + two + three; break;
      case '-':
        result = one - two - three; break;
      case '*':
        result = one * two * three; break;
      case '/':
        if(two != 0 && three != 0)
          result = one / two / three; break;
    }
    JOptionPane.showMessageDialog(null,"结果是 " + result,"求3个整数算法",
                              JOptionPane.PLAIN_MESSAGE);
  }
}
----------------解决方案--------------------------------------------------------
  相关解决方案