yunchou1.java

来自「一维搜索程序」· Java 代码 · 共 100 行

JAVA
100
字号
import java.lang.*;
import javax.swing.JOptionPane;
public class YunChou1
 {   
   public static void main(String args[])
     {
       String inputString;
       String outputString;
       inputString=JOptionPane.showInputDialog("请输入区间下限a0:");
       double a0=Integer.parseInt(inputString);
       inputString=JOptionPane.showInputDialog("请输入区间上限b0:");
       double b0=Integer.parseInt(inputString);
       outputString="取值区间:"+"("+a0+","+b0+ ")" ;
       JOptionPane.showMessageDialog(null,outputString,"定义域",JOptionPane.INFORMATION_MESSAGE);
       double t1 =(b0-a0)*Math.random()+a0;
       double t2 =(b0-a0)*Math.random()+a0;
       YunChou1 t=new YunChou1();
       t.init1(t1,t2,a0,b0);  
    }
  public void init1(double t1,double t2,double a0,double b0)
    {  String outputString1;
       double ft1=t1*t1+t1+1;
       double ft2=t2*t2+t2+1;
       for(int i=0;i<100;i++)
        {
          if(t1<t2)
            {
    		   if(ft1<ft2)
    		     {
    		       a0=a0;
    		       b0=t2;
    		       t1=t1;
    		       t2=(b0-a0)*Math.random()+a0;
    		       ft1=t1*t1+t1+1;
                   ft2=t2*t2+t2+1;
                   continue;
    		     }
    		  else if(ft1>=ft2)
    		     {
    			   a0=t1;
    			   b0=b0;
    			   t1=t2;
    			   t2=(b0-a0)*Math.random()+a0;
    			   ft1=t1*t1+t1+1;
                   ft2=t2*t2+t2+1;
                   continue;
    		     }
            }
    	 else if (t1>t2)
    	   {
    		  if(ft1>ft2)
    		    {
    			  a0=a0;
    			  b0=t1;
    			  t1=t2;
    			  t2=(b0-a0)*Math.random()+a0;
    			  ft1=t1*t1+t1+1;
                  ft2=t2*t2+t2+1;
                  continue;
    	  	    }
    		  else if(ft1<=ft2)
    		    {
    			  a0=t2;
    			  b0=b0;
    			  t1=t1;
    			  t2=(b0-a0)*Math.random()+a0;
    			  ft1=t1*t1+t1+1;
                  ft2=t2*t2+t2+1;
                  continue;
    		    }
    	    }
    	 else
    		 {
    		 	a0=a0;
    		 	b0=b0;
    		 	t1=t1;
    		 	t2=(a0+b0)/2;
    	        ft1=t1*t1+t1+1;
                ft2=t2*t2+t2+1;
             } 
        }
    outputString1="最后两次试点的位置为:"+"\n"+"x1="+t1+"\n"+"x2="+t2;
    
    
    if(ft1<=ft2)
    {
         outputString1=outputString1+"\n"+"Minf(x)="+ft1;
         JOptionPane.showMessageDialog(null,outputString1,"最后运行结果",JOptionPane.INFORMATION_MESSAGE); 
         System.exit(0);
    }
    else
    {
         outputString1=outputString1+"\n"+"Minf(x)="+ft2;
         JOptionPane.showMessageDialog(null,outputString1,"最后运行结果",JOptionPane.INFORMATION_MESSAGE); 
         System.exit(0);
    }
  } 
}
  
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?