如何使用方法返回的值

时间:2017-09-12 02:11:40

标签: java

    import java.util.Scanner;

public class Movies {
   static Scanner scan = new Scanner(System.in);
   static String movies[] = {"1. IT\tP200", "2. The Battleship Island\tP300", "3. Annabelle Creation\tP180", "4. Woke Up Like This\t100"};
   static String name;
   static int seats=0;
   static int number=0;
    public static void getName(){
        System.out.print("Please enter your Name: ");
        name = scan.nextLine();
        System.out.println("\nWelcome to MOVIEWORLD, " +name);
        getMovieTitle();

    }

public static int getMovieTitle(){

    System.out.println("\nNOW SHOWING");
   for (String movie : movies) {
       System.out.println(movie);
   }
   char choice;
   System.out.print("Would you like to get a ticket? (Y/N)");
   choice= scan.next().charAt(0);
    switch(choice){
        case 'y':
        case 'Y':
            System.out.print("Please select a movie: ");
            number = scan.nextInt();
            System.out.println("You have Selected "+movies[number-1].substring(3,movies[number-1].length()));
            getSeats();
            break;


        case 'n':
        case 'N':
            System.out.println("Thank you!");
            System.exit(0);
            break;
        default:
            getMovieTitle();
            break;
    }
    return number;
}

public static int getSeats(){
    System.out.print("How many seats would you like? ");
    int seat = scan.nextInt();
    System.out.printf("Reserved # of seats is %d ",seat );
    return seat;
}



public static void main(String[] args){
    System.out.println("\tMOVIEWORLD");
    System.out.println("________________________");
    getName();

    int mynum;
    mynum=getMovieTitle();
    int mynum2;
    mynum2=getSeats();
     int mov1 = 200;
     int mov2 = 300;
     int mov3 = 180;
     int mov4 = 100;
     int cost;
     int money;
     int change;
     char c;
     System.out.print("Print Receipt?(y/n)");
     c= scan.next().charAt(0);
     switch(c){
         case 'y':
         case 'Y':
     if(mynum == 1){
         cost=mov1*mynum2;
         System.out.print("Total: Php"+cost);
         System.out.print("Received cash is: Php");
         money=scan.nextInt();
         change=money-cost;
         System.out.print("Your change is: Php"+change);

     }
     else if(mynum == 2){
         cost=mov2*mynum2;
         System.out.print("Total: Php"+cost);
         System.out.print("Received cash is: Php");
         money=scan.nextInt();
         change=money-cost;
         System.out.print("Your change is: Php"+change);
     }
     else if(mynum == 3){
         cost=mov3*mynum2;
         System.out.print("Total: Php"+cost);
         System.out.print("Received cash is: Php");
         money=scan.nextInt();
         change=money-cost;
         System.out.print("Your change is: Php"+change);
     }
     else if(mynum == 4){
         cost=mov4*mynum2;
         System.out.print("Total: Php"+cost);
         System.out.print("Received cash is: Php");
         money=scan.nextInt();
         change=money-cost;
         System.out.print("Your change is: Php"+change);
     }
     break;
         case 'n':
         case 'N':
             System.out.println("Thank you");
             break;

         default:
              System.out.println("Thank you");
              System.exit(0);

}
}
}

我正在努力获得成本,收到的钱和变化。但是我在使用方法中返回的值时遇到了麻烦。选择了我想要的座位后,该程序就会退出。

这是我想在main方法中使用的值。首先是数字

 System.out.print("Please select a movie: ");
            number = scan.nextInt();
            System.out.println("You have Selected "+movies[number-1].substring(3,movies[number-1].length()));
            getSeats();


        case 'n':
        case 'N':
            System.out.println("Thank you!");
            System.exit(0);
        default:
            getMovieTitle();

    }
    return number;

其次是座位,所以我可以将它与数字相乘。

 public static int getSeats(){
    System.out.print("How many seats would you like? ");
    int seats = scan.nextInt();

    return seats;
}

1 个答案:

答案 0 :(得分:0)

  1. 您需要break

    case 'Y':
        System.out.print("Please select a movie: ");
        number = scan.nextInt();
        System.out.println("You have Selected "+movies[number-
            1].substring(3,movies[number-1].length()));
        getSeats();
        break;
    
  2. 您需要为在程序开头创建的全局seats变量分配席位数,而不是局部变量。

    public static int getSeats(){
     System.out.print("How many seats would you like? ");
     seats = scan.nextInt();
    
     return seats;
    }
    
  3. 更新

    1. 在您更新的问题中,您在主方法中使用getMovieTitle()getSeats()捕获了mynummynum2的返回值,但仍然分别在getName()getMovieTitle()内调用这些方法。只需评论这些电话:

      public static void getName(){
         System.out.print("Please enter your Name: ");
         name = scan.nextLine();
         System.out.println("\nWelcome to MOVIEWORLD, " +name);
         //getMovieTitle();
      
      }
      

      case 'Y':
          System.out.print("Please select a movie: ");
          number = scan.nextInt();
          System.out.println("You have Selected "+movies[number-1].substring(3,movies[number-1].length()));
          //getSeats();
          break;