到目前为止,我已经编写了这段代码,它是作业的一部分,但我被困住了,我不知道如何做第2部分。我已经完成了第1部分。有关从何处开始的任何建议?
谢谢!
第1部分。
提示用户输入以下信息并将输入存储在适当的变量中:
第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)));
}
}
第2部分。
使用JOptionPane.showMessageDialog()
或System.out.println()
输出以下信息,打印出以下信息:
Math.abs()
。有人可以帮我完成这个问题的第二部分吗?谢谢。
答案 0 :(得分:1)
对于这些问题,您需要一些条件陈述,即:
if(leftHand.equals("yes")){
System.out.println("User is left-handed");
} else {
System.out.println("User is right-handed");
}
答案 1 :(得分:1)
听起来你有很多条件需要检查。关于布尔值的好处,他们很适合“if”语句。 。
查看:
if(someCondition)
{
// Do Something
}
else
{
// Do Something Else
}
答案 2 :(得分:1)
此时,您可以使用单个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()
打印整个字符串。你只需要记住,当你将你的答案添加到字符串时,你需要一个换行符。