java


Write a java program using Scanner to help you decide what to do this weekend


I need help with when asking the questions how do i get the question answered i think my teacher what me to use yes or no. how to i get the program to understand the yes or no and letting that equal true or false. below is the question and under that will be the code i already have.
Decision Trees
Imagine you only ever do three things at the weekend: go shopping, watch a movie, or just stay in. What you do depends on three things: the weather (good or bad); how much money you have (rich or poor) and whether your parents are visiting. You say to your yourself: if my parents are visiting, we'll go to the cinema. If they're not visiting and the weather's good and I'm rich, then I'll go shopping. If they're not visiting, and the weather's good and I'm poor, then I will go to the cinema. If they're not visiting and the weather is bad and I'm rich, I'll go to the cinema. If they're not visiting and the weather is bad and I'm poor, I'll stay in.
Create a program asking whether the parents are visiting, whether the weather is good, and whether you are rich or poor. Your program should print "go to the cinema" "go shopping" or "stay in" as appropriate.
Hint: There are two possibilities for the "Parents visiting?" question, two for the "is weather good?" question, and two for the "are you rich?" question. That gives eight possible cases:
Truth Table for Did You Ever Have to Make Up Your Mind? Are parents visiting? Is the weather good? Are you rich? What you do
y y y
y y n
y n y
y n n
n y y
n y n
n n y
n n n
public class WhatWeDoing {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N;
int Y;
boolean parentsVisiting = false;
boolean weatherGood = false;
boolean imRich = false;
//prompt user to answer questions yes or no
System.out.print("Are Parents Visiting? Y or N ");
int parents = input.nextInt();
System.out.print("Is the Weather Good? Y or N");
int weather = input.nextInt();
System.out.print("Are You Rich? Y or N");
int Rich = input.nextInt();
{
if (parentsVisiting == false && weatherGood == true && imRich == true) {
System.out.println("Go Shopping! ");
} else if (parentsVisiting == false && weatherGood == false && imRich == false) {
System.out.println("Stay in!");
} else {
System.out.println("Cinema!");
}
}
System.out.println("What you should do is: ");
}
}
You can use input.next().equals and then set your boolean accordingly.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean parentsVisiting = false;
boolean weatherGood = false;
boolean imRich = false;
System.out.print("Are Parents Visiting? Y or N ");
if (input.next().equals("Y"))
parentsVisiting = true;
System.out.print("Is the Weather Good? Y or N");
if (input.next().equals("Y"))
weatherGood = true;
System.out.print("Are You Rich? Y or N");
if (input.next().equals("Y"))
imRich = true;
System.out.println("What you should do is: ");
if (!parentsVisiting && weatherGood && imRich) {
System.out.println("Go Shopping! ");
} else if (!parentsVisiting && !weatherGood && !imRich) {
System.out.println("Stay in!");
} else {
System.out.println("Cinema!");
}
}
}
test
Are Parents Visiting? Y or N Y
Is the Weather Good? Y or NN
Are You Rich? Y or NY
Cinema!
First, you should scan 'Strings' or 'chars', as the user was required
Second, a nice way to getting the value:
//in case of 'char':<br>
Boolean a = (parentsVisiting == 'Y');
or:
//in case of 'String':<br>
Boolean a = parentsVisiting.equals("Y");
The "normal" way to do it will be using 'char' if it is just homework.
Solving it with 'String' helps with lower and uppercase, as well as with unexpected input

Related Links

How to check if JSON is nested or not in Java?
Reading a int[] from java properties file
Java getting ObjectInputStream ObjectOutputStream from a socket
Verify if a list contains an specific element using Hamcrest [duplicate]
Yet another type erasure error
Failed to write core dump , A fatal error has been detected by the Java Runtime Environment
Android: How to record on top of a music being played?
How to reduce on groups and return a sorted list with the Java 8 Stream API
How to connect to Oracle AQ using DB connection URL?
Using interface constants in constructor of implementing class
Publish Map in Redis
Android - send / receive data to online mysql database
Class mapping in dozer
How to add checkbox image using itextpdf Java library?
Deploy Storm topology via REST API
Android app Cannot Open at Api 19

Categories

HOME
server
deezer
tizen
smarty
view
onedrive
q
analysis
tizen-web-app
jxls
gps
win32gui
umd
echarts
opengl-es-2.0
medical
floating-action-button
export-to-csv
qt-creator
visual-studio-2005
ab-testing
dbext
zurb-foundation-6
android-widget
underflow
windows-error-reporting
elasticsearch-ruby
neo4j-spatial
large-file-upload
delicious-api
bluestacks
preg-match
react-chartjs
pdb
windows-dev-center
bytecode-manipulation
overlap
main
qwerty
espeak
objectlistview
pdf-reactor
arena-simulation
http-redirect
slick-3.0
qcombobox
convertapi
wso2carbon
angular-resource
network-flow
strptime
idisposable
nativeapplication
qtwebview
pillow
linode
flutterwave
gcsfuse
nxt
instant
ado.net-entity-data-model
database-optimization
nsfilemanager
merge-conflict-resolution
qpid
contact-list
holder.js
persist
essence
dlna
unity-networking
cartesian-product
teamcity-8.0
coveralls
ionic
offloading
angularjs-ng-click
fpml
websocket4net
terminfo
ceil
message-driven-bean
html5-notifications
prettify
jsctypes
ftps
file-locking
padarn
seed
plasma
pygit2
sabredav
http-unit
ocunit
wsdl-2.0
chuck
dice
psi
mongomapper
meego
multi-tier
libs
anti-piracy
text-coloring
ajax-forms

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App