如何从Java中的函数返回值?

时间:2016-12-10 08:30:38

标签: java

我无法使用此代码,我不知道为什么。是否可以从calculateTotal方法返回值?我是编程新手,Java是我的第一个。

import java.util.Scanner;
public class ass2ques2draft
{
public static Scanner scan= new Scanner (System.in);
 public static void main (String[]args)
 {
int addOrder;
 do {
 System.out.println ("**************************");
 System.out.println ("WELCOME TO LILY BAKERY");
 System.out.println ("**************************");
 System.out.println ("Please choose your preferred cake code:");
 System.out.println (" ");
 System.out.println ("1 - New York Cheese");
 System.out.println ("2 - Black Forest Cheese");
 System.out.println ("3 - Oreo Cheese ");
 System.out.println ("4 - Chocolate Indulgence Cake");
 System.out.println ("5- Turkish Indulgence");
 System.out.println ("6- Red Velvet");
 System.out.println ("7- Fruitilicious");

 takeOrder();
 System.out.print ("Do you want to add your order? :  1 - \"Y\" /  2 -\"N\" :" );
 addOrder = scan.nextInt();
 }while (addOrder == 1);
 double totalPrice = calculateTotal( cakeChoice , quantity );

 System.out.printf ("Total price : RM %.2f %n " , totalPrice);
 System.out.print ("Please enter amount tendered : RM");
 double amountTendered =scan.nextDouble();
 double balance = amountTendered-totalPrice;

 System.out.println ("Your transaction :");
 System.out.println ("*****************" );
 System.out.printf ("Total Price : %.2f %n" , totalPrice );
 System.out.printf ("Amount Tendered : %.2f %n" , amountTendered);
 System.out.printf ("Balance : %.2f%n" , balance);
 System.out.println ("*****************************************************");
 System.out.println (" " );
 System.out.println ("$$$$$$$$$$$$--THANK YOU & PLEASE COME AGAIN !!! --$$$$$");


 }//main

 public static void takeOrder()
 {
  System.out.print("Enter your choice of cake : ");
  int cakeChoice = scan.nextInt();
  if ( cakeChoice == 1 )
  {
   System.out.print ("Enter the quantity of New York Cheese : ");
  } 

  else if ( cakeChoice == 2 )
  {
   System.out.print ("Enter the quantity of Black Forest Cheese : ");
  } 

  else if ( cakeChoice == 3 )
  {
   System.out.print ("Enter the quantity of Oreo Cheese : ");
  } 
  else if ( cakeChoice == 4 )
  {
   System.out.print ("Enter the quantity of Chocolate Indulgence Cake : ");
  }
 else if ( cakeChoice == 5 )
  {
   System.out.print ("Enter the quantity of Turkish Indulgence : ");
  } 
  else if ( cakeChoice == 6 )
  {
   System.out.print ("Enter the quantity of Red Velvet : ");
  }
  else  if  ( cakeChoice == 7 )
  {
  System.out.print ("Enter the quantity of Fruitilicious : ");  
  }
  int quantity=scan.nextInt(); 

  calculateTotal (cakeChoice , quantity );
  }//takeOrder

  public static double calculateTotal ( int cakeChoice, int quantity )
  {
   double price;

   if ( cakeChoice <5 )
   {
   price = 88*quantity;
   }
   else
   {
   price=130*quantity;
   }
 double totalPrice=0;
  totalPrice += price;
  return totalPrice;

  }//calculateTotal
  }//class

2 个答案:

答案 0 :(得分:0)

由于您是Java新手,您可能不了解约定,因此始终使用大写字母开始该类的名称。你的代码有问题。 您在哪里定义 cakeChoice&amp;数量在班级?

这些是未定义的变量。 是的,该方法可以返回总数。

在类级别声明变量cakeChoice & quantity。您已将本地变为方法 takeOrder 的这些变量。我建议以这种方式编写代码

    public class Ass2ques2draft
{
 int cakeChoice; // declare them here
 int quantity;  // 
public static Scanner scan= new Scanner (System.in);

不要在takeOrder方法中声明它们,而是立即使用它们。并且不要将它们传递给calculateTotal方法

在接受订单方法中,将其设为

  System.out.print("Enter your choice of cake : ");
  cakeChoice = scan.nextInt(); // This uses the class level member,instead of making a new local variable as cakeChoice

只需调用calculate方法,而不传递任何参数。

public static double calculateTotal (  )
  {
   double price;

   if ( cakeChoice <5 ) // use the class level variable cakeChoice
   {
   price = 88*quantity; // use the class level quantitiy
   } 
   else
   {
   price=130*quantity;
   }
 double totalPrice=0;
  totalPrice += price;
  return totalPrice;

  }//calculateTotal
  }//class

您的代码的主要问题是您每次都在每个方法中创建新的“本地”变量。第81行也没有任何重要意义。您将如何处理返回的总数?

答案 1 :(得分:0)

为了便于阅读,我绝对建议使用switch语句而不是嵌套的if

System.out.print("Enter your choice of cake : ");
int cakeChoice = scan.nextInt();
switch(cakeChoice) {
    case 1: System.out.println("Enter the quantity of xy"); break;
    case 2: System.out.println("Enter the quantity of yz"); break;
    //...
    default: System.out.println("Invalid cake order!"); takeOrder();
}
calculateTotal (cakeChoice , scan.nextInt() );

就价格而言,您可以这样做:

return (cakeChoice < 5)? (double) 88*quantity :  (double)130*quantity;

除非您使用小数价格,否则您可以将函数的返回值更改为int并移除double强制转换。