In the below java code, how can I ask the user to input how many shots they would like in each of their coffee cups (Given they order more than 1)? -
import java.util.scanner; public class coffeebot { public static void main(string[] args) { scanner keyboard = new scanner (system.in); system.out.println("hello, what's name?"); string name = scanner.nextline(); system.out.println("would order coffee, "+name+(y/n)?") string response = scanner.nextline() { if response = y system.out.println("great! lets started.") system.out.println("there 6 coffee cups in stock , each costs $2.00."); system.out.println("there 8 coffee shot in stock , each costs $1.00."); system.out.println("how many cups of coffee like?"); int cups; cups = keyboard.nextint(); { if cups = 0; system.out.println("no cups, no coffee. goodbye."); system.exit(); if cups < 0; system.out.println("does not compute. system terminating."); system.exit(); if cups > 6; system.out.println("not enough stock. come later."); system.exit(); } system.out.println("how many coffee shots in cup 1?"); int shots; shots = keyboard.nextint();
for instance if user orders 2 cups, supposed ask user how many shots want in each of cups i.e. in cup1 , cup2. can please suggest how can construct code action?
your code have lots of mistakes. corrected code this:
import java.util.scanner; public class coffeebot { static scanner keyboard = new scanner (system.in); public static void main(string[] args) { system.out.println("hello, what's name?"); string name = keyboard.next(); system.out.println("would order coffee, "+name+"(y/n)?"); string response = keyboard.next(); { if (response.equalsignorecase("y")){ system.out.println("great! lets started."); system.out.println("there 6 coffee cups in stock , each costs $2.00."); system.out.println("there 8 coffee shot in stock , each costs $1.00."); system.out.println("how many cups of coffee like?"); int cups; cups = keyboard.nextint(); { if (cups == 0){ system.out.println("no cups, no coffee. goodbye."); } if (cups < 0){ system.out.println("does not compute. system terminating."); } if (cups > 6){ system.out.println("not enough stock. come later."); } } shots(cups); //this methods calls shots static class } } } public static int[] shots(int num){ int[] shots = new int[num]; for(int = 0; i<num;i++){ system.out.println("how many coffee shots in cup "+(i+1)+" ?"); shots[i] = keyboard.nextint(); } return shots; } }
what have done here created separate static class requirement. shots(cups);
call function.
the function take , integer value parameter. initiate array. array hold no of shots each cup needs.
public static int[] shots(int num){ int[] shots = new int[num]; for(int = 0; i<num;i++){ system.out.println("how many coffee shots in cup "+(i+1)+" ?"); shots[i] = keyboard.nextint(); } return shots; }
this method loop through no of cups times ask user how many shots needed , placed in array. array returned place method called. can ever wanted.