Pseudocode in java? [closed]

此生再无相见时 提交于 2021-02-08 11:47:20

问题


 String s = ""; 

    myf = new Finch();
    do
    {
        //Run the menu until quit or cancel is selected
        s = FinchMenu();
        //menu 1
        if (s.equals("Back and forward")) RunAccelerationTest(s);
    }

How would you convert something like this to pseudocode? For instance

String s = "";

Would the pseudocode for it be something like this?

Set s to ""

That just seems wrong to me.

Any help please? Thanks


回答1:


Pseudocode, i think, doesn't have a predefined syntax. just follow two rules:

  • It should be plain english with common programming constructs.

  • It should be generic, not specific to any language.

Following should fit:

Step 1: Initialize an empty string. (say str)

Step 2: Construct a new 'Finch' object.

Step 3: BEGIN LOOP

            Fetch 'FinchMenu' from 'Finch' object.

            assign 'FinchMenu' to 'str'

            IF 'FinchMenu' is "Back and forward"

                Call 'RunAccelerationTest' method with 'str' as argument.

            END IF

        END LOOP



回答2:


Pseudo code is just a non language specific human readable walkthrough of whatever it is you are trying to do.

You could write set s to "" or even make s an empty string or anythign else that describes what it is this step is doing.




回答3:


Generally speaking (this is totally how I use pseudo-code, rather than being anything set by a rule) I use pseudo-code to get rid of anything boilerplate or being syntactically correct.

For instance, say I have the following Java code...

import java.io.*;
class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println (strLine);
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

My interpretation as pseudo-code would be something like...

main() {
FileInputStream fstream = new FileInputStream(fileLocation);
BufferedReader br = BufferedReader(.....fstream);
String str;
while(br.readline!=empty, str=br.readline) {
  System.out(str);
}
}

Now this code is probably no use to anyone but me - but I know I can quickly look at it, see what it is and write it if necessary. It's good for making quick notes in meetings etc, if I'm trying to write fast pseudo-code, things like exception handling aren't something I consider.



来源:https://stackoverflow.com/questions/13954276/pseudocode-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!