java - How to use the value returned from a method -
import java.util.scanner; public class movies { static scanner scan = new scanner(system.in); static string movies[] = {"1. it\tp200", "2. battleship island\tp300", "3. annabelle creation\tp180", "4. woke this\t100"}; static string name; static int seats=0; static int number=0; public static void getname(){ system.out.print("please enter name: "); name = scan.nextline(); system.out.println("\nwelcome movieworld, " +name); getmovietitle(); } public static int getmovietitle(){ system.out.println("\nnow showing"); (string movie : movies) { system.out.println(movie); } char choice; system.out.print("would ticket? (y/n)"); choice= scan.next().charat(0); switch(choice){ case 'y': case 'y': system.out.print("please select 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 like? "); int seat = scan.nextint(); system.out.printf("reserved # of seats %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); } } }
i trying cost, money received , , change. having trouble in using returned values in methods. after selecting how many seats wanted program exits.
this values use in main method. first number
system.out.print("please select 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;
second seats can multiply number.
public static int getseats(){ system.out.print("how many seats like? "); int seats = scan.nextint(); return seats; }
you need
break
:case 'y': system.out.print("please select movie: "); number = scan.nextint(); system.out.println("you have selected "+movies[number- 1].substring(3,movies[number-1].length())); getseats(); break;
you need assign number of seats global
seats
variable created in beginning of program, rather local variable.:public static int getseats(){ system.out.print("how many seats like? "); seats = scan.nextint(); return seats; }
update:
in updated question, you're capturing returned values of
getmovietitle()
,getseats()
mynum
,mynum2
in main method, still calling methods insidegetname()
,getmovietitle()
respectively. comment calls:public static void getname(){ system.out.print("please enter name: "); name = scan.nextline(); system.out.println("\nwelcome movieworld, " +name); //getmovietitle(); }
and
case 'y': system.out.print("please select movie: "); number = scan.nextint(); system.out.println("you have selected "+movies[number-1].substring(3,movies[number-1].length())); //getseats(); break;
Comments
Post a Comment