Java伪代码代码

时间:2014-03-14 13:10:24

标签: java pseudocode

我即将构建一个用Java编写的程序。我已经完成了它的伪代码,但是我仍然坚持使用代码而且我不知道该怎么做。那么这是我目前的进展:

伪代码:

class Customer
    Print out “Enter costumer’s name” (Pop up to answer)
    Random boolean 
    If True
      print membership type
      print current date and time
class Visit
    Print out "Have you bought anything?" (Pop up to answer) 
    if the answer is “no”
        print “Have a nice day!”
        exit program
    if else the answer is “yes”
        continue
    if else the answer is not “no” nor “yes”
        ask again
class Discount
    Print "Enter Price of Item:" (Pop up to price)
        if customer's membership type is “Premium”
            the discount to the price will be 20%
        else if customer's membership type is “Gold”
            the discount to the price will be 15%
        else if customer's membership type is “Silver”
            the discount to the price will be 10%
        else if customer's membership type “simple”
            the discount to the price will be 10%
class Main
  Variables: customer, visit, discount
  customer = new object
  Customer visit = new object
Visit discount = new object Discount
  do work with customer
  do work with visit
  do work with discount
    Print customer.name, customer.surname, discount.price, discount.final_price

代码:

import java.util.Scanner;

public class Discount
{

    public static void main(String[] args)
    {
        String cust_name;
        String cust_surname;
        String answer;
        String answer2;

        float firstPrice, rate_1, D_rate, discount, final_price, prem_disc;
        prem_disc = 0;
        final_price = 0;
        discount = 0;

        Scanner in = new Scanner(System.in);

        System.out.println("Enter Costumer's Name:");
        cust_name = in.next();
        cust_surname = in.next();

        System.out.println("Have you bought anything?");
        answer = in.next();
        if (answer.equals("no"))
        {
            System.out.println("Have a good day!");
            System.exit(0);
        } else if (!answer.equals("no"))

            System.out.println("Enter Price of Item:");
        firstPrice = in.nextFloat();

        System.out
                .println("What type of membership do you have? Premium, Gold, Silver or simple?");
        answer2 = in.next();
        if (answer2.equals("Premium"))
        {
            prem_disc = 20;
            discount = (firstPrice * 20 / 100);
            final_price = (firstPrice - discount);
        } else if (answer2.equals("Gold"))
        {
            prem_disc = 15;
            discount = (firstPrice * 15 / 100);
            final_price = (firstPrice - discount);
        } else if (answer2.equals("Silver"))
        {
            prem_disc = 10;
            discount = (firstPrice * 10 / 100);
            final_price = (firstPrice - discount);
        } else if (answer2.equals("simple"))
        {
            prem_disc = 10;
            discount = (firstPrice * 10 / 100);
            final_price = (firstPrice - discount);
        }

        System.out.println("Costumer Name:" + cust_name + " " + cust_surname
                + "\n" + "Discount Rate:" + prem_disc + "\n"
                + "Discounted Price:" + final_price + "\n");

    }
}

它正在运作,但有很多东西都没有了。 :/

1 个答案:

答案 0 :(得分:1)

一次只需一步,当完全步骤有效时,请继续执行下一步。

一个例子:

这是你的伪代码(顺便说一下,这是一个很好的开始方式):

class Visit
   Print out "Have you bought anything?" (Pop up to answer)
   if the answer is “no”
       print “Have a nice day!”
       exit program
   if else the answer is “yes”
       continue
   if else the answer is not “no” nor “yes”
       ask again

让我们实现并测试“答案是或否”功能:

/**
   <P>{@code java MyHomeworkMainClass}</P>
 **/
public class MyHomeworkMainClass  {
   public static final void main(String[] ignored)  {
      System.out.println("Visit.isUserInputYesNo(null)=" + Visit.isUserInputYesNo(null));
      System.out.println("Visit.isUserInputYesNo(\"gibberish\")=" + Visit.isUserInputYesNo("gibberish"));
      System.out.println("Visit.isUserInputYesNo(\"yes\")=" + Visit.isUserInputYesNo("yes"));
      System.out.println("Visit.isUserInputYesNo(\"no\")=" + Visit.isUserInputYesNo("no"));
   }
}
 class Visit  {
   public static final boolean isUserInputYesNo(String input)  {
      return  (input != null  &&
         (input.equals("yes")  ||  input.equals("no")));
   }
}

现在运行它,看看它是否符合您的要求:

[C:\java_code\]java MyHomeworkMainClass
Visit.isUserInputYesNo(null)=false
Visit.isUserInputYesNo("gibberish")=false
Visit.isUserInputYesNo("yes")=true
Visit.isUserInputYesNo("no")=true

确实如此。现在添加一些小的东西,直到你完成为止。对于特别困难的部件,使用自己的测试类创建一个完全独立的类可能是有益的。然后在最后,将它们合并在一起。并且从不摆脱您的测试功能,这些功能将始终用于诊断未来的问题。

祝你好运!