I have written this code so far, it is part of an assignment, but I am stuck and am not sure how to do part 2. I have completed part 1. Any suggestions on where to go from here?
Thanks!
Part 1.
Prompts the user for the following information and stores the input in appropriate variables:
Part 1. Code
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class InformationStationFinal{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String s1 = "yes";
String s2 = "no";
System.out.print("Are you left handed? Enter yes or no:");
String leftHand = input.next();
System.out.println("true");
System.out.print("Is your father at least 5ft 10 inches? Enter yes or no:");
String tall = input.next();
System.out.println("true ");
System.out.print("Enter your age in months: ");
int age = input.nextInt();
System.out.println(" ");
System.out.print("Enter the age of a sibling or friend in months: ");
int ageTwo = input.nextInt();
System.out.println(" ");
System.out.print("Enter your GPA as decimal, such as 3.58: ");
double gpa = input.nextDouble();
System.out.println(" ");
System.out.print("Enter displacement in liters of your cars engine:");
int liter = input.nextInt();
System.out.println(" ");
System.out.println("Are you left handed? " + " " + leftHand);
System.out.println("Is your father at least 5ft 10in? " + " " + tall);
System.out.println("Are you left handed or is your father at least 5ft 10?" + " " +
((leftHand.equals(s1)) || (tall.equals(s1))));
System.out.println("Are you left handed And is your father at least 5ft 10?" + " " +
((leftHand.equals(s1)) && (tall.equals(s1))));
System.out.println("Are your answers for left handed & father's height the same" + " " +
(leftHand.equals(tall)));
}
}
Part 2.
Prints out the following information, using either JOptionPane.showMessageDialog()
or System.out.println()
for the output:
Math.abs()
for this. Could someone please help me complete the second part of this question? Thank you.
For those questions you will need some conditional statements, ie:
if(leftHand.equals("yes")){
System.out.println("User is left-handed");
} else {
System.out.println("User is right-handed");
}
Sounds like you have a bunch of conditions to check. The nice thing about booleans, they lend themselves well to "if" statements . . .
Look into:
if(someCondition)
{
// Do Something
}
else
{
// Do Something Else
}
At this point, you could use a single String
to gather the results for later printing in a JOptionPane.showMessageDialog()
. I won't code it all for you, but here's an example:
String result = "";
if(leftHand.equalsIgnoreCase("yes")) {
result += "You are left handed.\n";
} else {
// Other stuff
}
// ...
JOptionPane.showMessageDialog(null, result);
EDIT : If you're not interested in using JOptionPane, then you can use a single System.out.println()
to print the entire string out as well. You just have to remember, when you're adding your answers to the String, you need a newline character.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.