Hello, I am having problems with this code. It runs fine. But I need to add more
ID: 3651197 • Letter: H
Question
Hello, I am having problems with this code. It runs fine. But I need to add more information. 1.Ask the user if they would like to purchase four chairs to go with their new table. 2.If so, add an additional $250 to the grand total. import java.util.Scanner; public class Furniture { public static void main(String[] args) { int type; int price; int chairs=0; int yes=250; int no=0; String name; Scanner input= new Scanner(System.in); System.out.print("Table types 1.Pine 2.Oak 3.Mahogany "); System.out.println("Please enter your tree type:"); type=input.nextInt(); if(type==1) { name="Pine"; price=100; } else if(type==2) { name="Oak"; price=225; } else if(type==3) { name="Mahogany"; price=310; } else { price=0; name="Invalid"; } System.out.println(name+" table price $"+price); System.out.println("Would you like to buy four chairs? Enter (yes or no)"); } }Explanation / Answer
You just need to add the following: String response=input.nextLine(); if(response.equals("yes")) { price+=250; } It gets the user's answer, checks if it is "yes", and if so will increase the price that the user has to pay. You may also want to do the following inside the if statement: name += " and four chairs"; That will concatenate " and four chairs to the end of the name.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.