Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-创建Ackermann值图_Java_Ackermann - Fatal编程技术网

Java-创建Ackermann值图

Java-创建Ackermann值图,java,ackermann,Java,Ackermann,老师让我编辑这个图形代码。图表已经绘制好了。他想让我做的是调整代码,使其绘制rescursive ackermann值。所以,如果m=3,n=2,它就会把它画出来。我如何调整我的阿克曼方法?有什么建议吗 import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.G

老师让我编辑这个图形代码。图表已经绘制好了。他想让我做的是调整代码,使其绘制rescursive ackermann值。所以,如果m=3,n=2,它就会把它画出来。我如何调整我的阿克曼方法?有什么建议吗

import java.awt.Color;
        import java.awt.Container;
        import java.awt.Dimension;
        import java.awt.Font;
        import java.awt.Graphics;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import javax.swing.JApplet;
        import javax.swing.JButton;
        import javax.swing.JTextField;
        public class ArckermannGraph extends JApplet implements ActionListener {



            private static final long serialVersionUID = 1L;
            private static int temp;
            private static int count;
            private static int countTotal = 0;
            int forColor = 1;
            int m, n;
            boolean paint = false;
            Font titleFont;
            Font regularFont;
            //*** two buttons are declared
            JButton DrawLinesButton;
            JButton clearLinesButton;

            //*** two input values declared
            JTextField inputLineNumber1;
            JTextField inputLineNumber2;

            Container contentPane;
            //*** all JApplet have a method paint to draw the graphics
            //*** Graphics object is brought in
            //*** paint() is automatically called
            public void paint (Graphics page)
            {
                if(!paint)
                    graphInitialize(page);
                super.paint(page);
                setBackground(Color.WHITE);
                graph(page);

            }
            //*** The Applet class defines a method that is essential for programming applets, 
            //*** the init() method. This method is called just after the applet object has 
            //*** been created and before it appears on the screen. Its purpose is to give
            //*** the applet a chance to do any necessary initialization. This method is
            //*** called by the system, not by your program. Your job as a programmer 
            //*** is just to provide a definition of the init() method. 
            public void init()
            {
                System.out.println("init has been called");
                contentPane = getContentPane();
                contentPane.setLayout(null);
                //*** 32 and 18 are pixel size
                titleFont = new Font("Arial", Font.BOLD, 32);
                regularFont = new Font("Arial", Font.PLAIN, 18);

                //*** memory is allocated for Button
                DrawLinesButton = new JButton("Draw Lines");
                //*** this button is active and will be read
                //*** by method actionPerformed(ActionEvent e)
                DrawLinesButton.addActionListener(this);
                //*** Location of button is created 
                DrawLinesButton.setBounds(350,550,150,40);

                //*** memory is allocated for Button
                clearLinesButton = new JButton("Clear Lines");
                //*** this button is active and will be read
                //*** by method actionPerformed(ActionEvent e)
                clearLinesButton.addActionListener(this);
                //*** Location of button is created 
                clearLinesButton.setBounds(350,600,150,40);

                //*** setBounds() creates the input box
                //*** memory is allocated for TextField
                inputLineNumber1 = new JTextField(15);
                //*** Location of TextField is created 
                inputLineNumber1.setBounds(210,560,50,30);

                //*** memory is allocated for TextField
                inputLineNumber2 = new JTextField(15);
                //*** Location of TextField is created 
                inputLineNumber2.setBounds(210,590,50,30);

                //*** add them to contentPane
                contentPane.add(DrawLinesButton);
                contentPane.add(clearLinesButton);
                contentPane.add(inputLineNumber1);
                contentPane.add(inputLineNumber2);
            }
            public void graphInitialize (Graphics page)
            {
                Graphics g = getGraphics(); 
                Color c = getBackground(); 
                g.setColor(c); 
                for(int x=0;x<1250;x++)
                    g.drawLine (x,900, x,0);
                System.out.println("graphInitialize has been called");
                page.setColor(Color.BLACK);
                page.setFont(titleFont);
                page.setFont(regularFont);
                //*** Strings line up with setBounds for each JTextField
            //  page.drawString("Enter line number 1: ",40, 580);
            //  page.drawString("Enter line number 2: ",40, 610);
                //*** initialize input to 0
                inputLineNumber1.setText("0");
                inputLineNumber2.setText("0");
            }
            public void graph(Graphics page)
            {
                System.out.println("graph has been called");
                if(!inputLineNumber1.getText().equals("")&&!inputLineNumber2.getText().equals(""))
                {
                    //*** text must be changed to int using the wrapper class Integer
                    int lineNumber1 = Integer.parseInt(inputLineNumber1.getText());
                    int lineNumber2 = Integer.parseInt(inputLineNumber2.getText());


                    //*** method called here
                    int result = drawLines(lineNumber1, lineNumber2, page);
                    //*** method called here

                    //*** 3 strings are placed in the window
                    page.setColor(Color.BLACK);
                    page.setFont(titleFont);
                    page.drawString("Practice Graph", 540, 50);
                    page.setFont(regularFont);
                    page.drawString("Number of lines:  " + result, 600, 580);
                    page.drawString("Value of lines again:  " + result, 600, 610);
                    page.drawString("Enter line number 1: ",40, 580);
                    page.drawString("Enter line number 2: ",40, 610);
                }
            }

            private int drawLines(int lineNumber1, int lineNumber2, Graphics page) 
            {   //*********************************************************
                //*** this method could be the recursive Ackermann function
                //*********************************************************
                count = 200;
                countTotal = 0;
                setNewColor(page,(int) (Math.random() * 12)+1);
                for(int i=0;i<lineNumber1;i++)
                {
                    page.drawLine (count,500, count,400);
                    count++;
                    countTotal++;
                }
                setNewColor(page,(int) (Math.random() * 12)+1);
                for(int i=0;i<lineNumber2;i++)
                {
                    page.drawLine (count,500, count,300);
                    count++;
                    countTotal++;
                }
                return countTotal;
            }

            private void setNewColor(Graphics page, int forColor)
            {
                switch (forColor)
                {
                case 1:
                    page.setColor(Color.BLACK);
                    break;
                case 2:
                    page.setColor(Color.BLUE);
                    break;
                case 3:
                    page.setColor(Color.CYAN);
                    break;
                case 4:
                    page.setColor(Color.DARK_GRAY);
                    break;
                case 5:
                    page.setColor(Color.GRAY);
                    break;
                case 6:
                    page.setColor(Color.GREEN);
                    break;
                case 7:
                    page.setColor(Color.LIGHT_GRAY);
                    break;
                case 8:
                    page.setColor(Color.DARK_GRAY);
                    break;
                case 9:
                    page.setColor(Color.MAGENTA);
                    break;
                case 10:
                    page.setColor(Color.ORANGE);
                    break;
                case 11:
                    page.setColor(Color.PINK);
                    break;
                case 12:
                    page.setColor(Color.YELLOW);
                    break;
                }
            }
            public void actionPerformed(ActionEvent e)
            {
                //*** buttons are active
                String actionCommand = e.getActionCommand();
                //*** one of two possible values of actionCommand
                if (actionCommand.equalsIgnoreCase("Draw Lines"))
                {
                    System.out.println("Draw Lines has been called");
                    paint = true;
                    countTotal = 0;
                    count = 0;
                    repaint();//*** method paint() is called again
                }
                else if (actionCommand.equalsIgnoreCase("Clear Lines"))
                {
                    System.out.println("Clear Lines has been called");
                    this.clear();
                    inputLineNumber1.setText("");
                    inputLineNumber2.setText("");
                }
            }

            public void clear() 
            { 
                //*** Graphics object is gotten
                Graphics g = getGraphics(); 
                Color c = getBackground(); 
                g.setColor(c); 
                //*** lines of the background color cover up the image
                for(int i=100;i<1250;i++)
                    g.drawLine (i,500, i,150);
                for(int i=600;i<1000;i++)
                    g.drawLine (i,700, i,75);

            }
            private  static int ackermann(int M,int N){
                int value = 0;
                if(M == 0){
                    value = N+1;


                }else
                    if(N == 0){
                        value = ackermann(M-1,1);
                    }else

                        value = ackermann(M-1,ackermann(M,N-1)) ;
                        return value;
                }
        }
导入java.awt.Color;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JApplet;
导入javax.swing.JButton;
导入javax.swing.JTextField;
公共类ArckermannGraph扩展JApplet实现ActionListener{
私有静态最终长serialVersionUID=1L;
私有静态int-temp;
私有静态整数计数;
私有静态int countTotal=0;
int forColor=1;
int m,n;
布尔绘制=假;
字体标题字体;
字体规则字体;
//***声明了两个按钮
J按钮抽绳按钮;
JButton clearLinesButton;
//***声明了两个输入值
JTextField inputLineNumber1;
JTextField inputLineNumber2;
容器内容窗格;
//***所有的日本人都有画图的方法
//***引入图形对象
//***将自动调用paint()
公共空间绘制(图形页)
{
如果(!油漆)
图形初始化(第页);
超级油漆(第页);
挫折地面(颜色:白色);
图表(第页);
}
//***小程序类定义了对小程序进行编程所必需的方法,
//***init()方法。此方法仅在小程序对象完成后调用
//***在它出现在屏幕上之前创建。其目的是
//***小程序有机会进行任何必要的初始化
//***由系统调用,而不是由您的程序调用。您作为程序员的工作
//***只是提供init()方法的定义。
公共void init()
{
System.out.println(“已调用init”);
contentPane=getContentPane();
contentPane.setLayout(null);
//***32和18是像素大小
titleFont=新字体(“Arial”,Font.BOLD,32);
regularFont=新字体(“Arial”,Font.PLAIN,18);
//***为按钮分配内存
DrawLinesButton=新的JButton(“绘制线”);
//***此按钮处于活动状态,将被读取
//***按方法actionPerformed(ActionEvent e)
DrawLinesButton.addActionListener(此);
//***按钮的位置已创建
抽绳按钮立根(350550150,40);
//***为按钮分配内存
clearLinesButton=新的JButton(“Clear Lines”);
//***此按钮处于活动状态,将被读取
//***按方法actionPerformed(ActionEvent e)
clearLinesButton.addActionListener(这个);
//***按钮的位置已创建
clearLinesButton.setBounds(350600150,40);
//***setBounds()创建输入框
//***为TextField分配内存
inputLineNumber1=新的JTextField(15);
//***已创建TextField的位置
输入号码1.立根(210560,50,30);
//***为TextField分配内存
inputLineNumber2=新的JTextField(15);
//***已创建TextField的位置
输入号码2.立根(210590,50,30);
//***将它们添加到contentPane
contentPane.add(DrawLinesButton);
添加(clearLinesButton);
contentPane.add(inputLineNumber1);
contentPane.add(inputLineNumber2);
}
公共无效图形初始化(图形页)
{
Graphics g=getGraphics();
颜色c=getBackground();
g、 setColor(c);

对于(int x=0;xRead到堆栈溢出),看起来您可能需要家庭作业帮助。虽然我们没有问题本身,请观察这些,并相应地编辑您的问题。(即使这不是作业,也请考虑该建议。)可能的副本