如何输出我存储的这些信息?

发布于 2024-12-29 15:22:41 字数 2684 浏览 0 评论 0原文

到目前为止,我已经编写了这段代码,它是作业的一部分,但我陷入困境,不确定如何执行第 2 部分。我已经完成第 1 部分。关于从这里开始的方向有什么建议吗?

谢谢!

第 1 部分:

提示用户输入以下信息并将输入存储在适当的变量中:

  • 用户是否是左撇子
  • 用户的父亲是否至少有 5 英尺 10 英寸高 用户
  • 的年龄(以月
  • 为单位) 兄弟姐妹或兄弟姐妹的年龄(以月为单位)用户的朋友
  • 用户的 GPA 用户
  • 汽车发动机的排量(升)

第 1 部分. 代码

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)));

    }
}

第 2 部分.

使用以下任一方式打印出以下信息JOptionPane.showMessageDialog()System.out.println() 用于输出:

  1. 用户是否是左撇子
  2. 用户的父亲是否至少有 5 英尺 10 英寸高
  3. a 和 b 中的至少一个值是否为 true(如果其中一个或两个为 true,则为 true)
  4. a 和 b 是否都为 true(如果至少一个为 false,则为 false)
  5. a 和 b 的真值是否相同 (如果 a 和 b 均为 true true 或如果 a 和 b 都为 false)
  6. 用户是否比他/她的兄弟姐妹/朋友大(据我们从月龄可以看出)
  7. 用户的月龄是否在他/她的年龄的 12 个月内/她的兄弟姐妹或朋友。您可能需要使用 Math.abs() 来实现此目的。
  8. 用户的GPA是否至少等于他/她的汽车发动机的排量。对于此项,如果条件为假,则使用 else 语句打印出一些适当的消息。

有人可以帮我完成这个问题的第二部分吗?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情独悲 2025-01-05 15:22:41

听起来你有很多条件需要检查。布尔值的好处是,它们非常适合“if”语句。 。 。

调查:

if(someCondition)
{
     // Do Something
}
else
{
     // Do Something Else
}

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
}
养猫人 2025-01-05 15:22:41

此时,您可以使用单个 String 来收集结果,以便稍后在 JOptionPane.showMessageDialog() 中打印。我不会为您全部编码,但这里有一个示例:

String result = "";
if(leftHand.equalsIgnoreCase("yes")) {
    result += "You are left handed.\n";
} else {
    // Other stuff
}
// ...
JOptionPane.showMessageDialog(null, result);

编辑:如果您对使用 JOptionPane 不感兴趣,那么您可以使用单个 System.out.println() 也可以打印整个字符串。您只需要记住,当您将答案添加到字符串时,您需要一个换行符。

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.

向地狱狂奔 2025-01-05 15:22:41

对于这些问题,您将需要一些条件语句,即:

if(leftHand.equals("yes")){
  System.out.println("User is left-handed");  
} else {
  System.out.println("User is right-handed"); 
}

For those questions you will need some conditional statements, i.e.:

if(leftHand.equals("yes")){
  System.out.println("User is left-handed");  
} else {
  System.out.println("User is right-handed"); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文