-2 java if-statement class object
public class RunProg {
static int input1; //for creating new human;
static boolean Married; //for knowing if married or not
static String isMarried; //for the YES or No of Married Boolean
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("What would you like to do? \n 1-Create new Human?\n");
input1 = input.nextInt();
input.nextLine();
//Inputs for The NEW HUMAN
if (input1 == 1) {
System.out.println("Enter name:");
String Name = input.nextLine();
System.out.println("Good! Now enter the age of " + Name + ": ");
int Age = input.nextInt();
System.out.println("Well done! Now enter length: ");
int Length = input.nextInt();
input.nextLine();
System.out.println("Finally, is he/she married? ('YES' or 'NO') ");
isMarried = input.nextLine();
if (isMarried == "YES") {
Married = true;
Human h1 = new Human(Name, Age, Length, Married);
}
else if (isMarried == "NO") {
Married = false;
Human h1 = new Human(Name, Age, Length);
}
}
Run Code Online (Sandbox Code Playgroud)
如何修改此代码以使h1if-else内外都可访问,而不在if-else外面声明?这是解决对象问题的辅助功能是严格作用域的if-else语句我问的括号内如果有制作的一种方式"inside'accessible括号外(我完全理解变量的作用域;所以请不要用范围,命名约定和外部声明来打扰我
如果 - 其他阻止?
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("What would you like to do? \n 1-Create new Human?\n");
input1 = input.nextInt();
input.nextLine();
//Inputs for The NEW HUMAN
if (input1 == 1) {
System.out.println("Enter name:");
String Name = input.nextLine();
System.out.println("Good! Now enter the age of " + Name + ": ");
int Age = input.nextInt();
System.out.println("Well done! Now enter length: ");
int Length = input.nextInt();
input.nextLine();
System.out.println("Finally, is he/she married? ('YES' or 'NO') ");
isMarried = input.nextLine();
Human h1 = new Human(Name, Age, Length, "YES".equals(isMarried));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
150 次 |
| 最近记录: |