简体   繁体   中英

How do I output this information that I have stored?

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:

  • Whether the user is left-handed
  • Whether the user's father was at least 5 ft 10 inches tall
  • The user's age in months
  • The age in months of a sibling or friend of the user
  • The user's GPA
  • The displacement in liters of the user's car engine

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:

  1. Whether the user is left-handed
  2. Whether the user's father was at least 5 ft 10 inches tall
  3. Whether at least one of the values from a and b are true (true if either or both are true)
  4. Whether a and b are both true (false if at least one is false)
  5. Whether the truth values of a and b are the same (true if both a and b are true or if both a and b are false)
  6. Whether the user is older than his/her sibling/friend (as far as we can tell from the ages in months)
  7. Whether the user's age in months is within 12 months of the age of his/her sibling or friend. You may want to use Math.abs() for this.
  8. Whether the user's GPA is at least equal to the displacement of his/her car engine. For this item, use an else statement to print out some appropriate message if the condition is false.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM