[Java]在嵌套的IF和else语句中苦苦挣扎 [英] [Java]struggling with nested IF and else statements

查看:85
本文介绍了[Java]在嵌套的IF和else语句中苦苦挣扎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,晚上好,



我刚刚开始Java /编程两天前,我似乎很难使用嵌套的if / else语句。我在下面提供了我当前的项目。当女性工作得很好的if语句,但是男性陈述总是给出了Mr的可能性和Name + lastName的可能性。



提前谢谢!请尊重,我刚刚开始编程



  import  java.util.Scanner的; 
public class GenderGame {

public static void main(字符串 [] args){

// 系统对象
扫描仪键盘= 扫描仪(System.in);

// 变量
字符串男性= 男性;
字符串女性= 女性;
字符串结婚;


System.out.print( 你的性别是什么(男性或女):);
字符串 gender = keyboard.nextLine();
System.out.print( 名字:\t);
String name = keyboard.nextLine();
System.out.print( 姓氏:\t);
String lastName = keyboard.nextLine();
System.out.print( 年龄:\t\t);
int age = keyboard.nextInt();

if (gender.equalsIgnoreCase(Female)&& age> = 20 ){
System.out.print( 你结婚了吗, + name + ?(是或否));
已婚= keyboard.next();
if (Married.equalsIgnoreCase( yes) ))
System.out.print( 然后我会叫你太太。 + lastName + );
} else {
System.out.print( 然后我会叫你 + name + + lastName + );



if (gender.equalsIgnoreCase(男)&& age> = 20
System.out.print( 然后我将叫你先生 + lastName + );
if (gender.equalsIgnoreCase(男)&& age< 20
System.out.print( 然后我会叫你 + name + + lastName + );


}
}
}





什么我试过了:



尝试了大量的花花公子,但是无法让它起作用

解决方案

这个家庭作业的嗅觉,所以我要脱口而出,但看看你的花括号,并确保每个闭合支撑与一个左大括号相匹配,反之亦然。

import java.util.Scanner;



公共类GenderAndMarigeStatus {

//我做了一些更改,现在是okey

public static void main(String [] args){



Scanner s = new Scanner(System.in); < br $>


String m =Male;

String f =Female;

String Married;



System.out.println(你的性别(男性或女性)是什么:);

String gender = s.nextLine() ;



System.out.println (名字:);

字符串名称= s.nextLine();

System.out.println(姓氏:);

String lastName = s.nextLine();

System.out.println(Age:);

int age = s.nextInt();



if(gender.equalsIgnoreCase(m)&&年龄> = 20){

System.out.println(然后我会称你为先生+ lastName +。);

System.exit( 0);

}

if(gender.equalsIgnoreCase(m)&& age< 20){

系统。 out.println(然后我会叫你+ name ++ lastName +。);

System.exit(0);

}

if(gender.equalsIgnoreCase(f)&& age> = 20){

}



System.out.println(你结婚了,+名字+?(是或否));

已婚= s.next();



if(Married.equalsIgnoreCase(yes))

System.out.println(然后我会称你为Mrs.+ lastName +。);

else {

System.out.println(然后我会称你为+ name ++ lastName +。);



}

}

}


Hello and good evening,

I've just started Java/programming two days ago and I seem to struggle with nested if/else statements. I've provided my current 'project' down below. The if statements when female works just fine, but the male statements always gives out both the possibility with Mr and the possibility with Name + lastName.

Thanks in advance! Please be respectful, I've really just started programming

import java.util.Scanner;
public class GenderGame {

	public static void main(String[] args) {
		
		//System Objects
		Scanner keyboard = new Scanner(System.in);
		
		//Variables
		String Male = "Male";
		String Female = "Female";
		String Married;
		
		
		System.out.print("What is your gender (male or female):");
		String gender = keyboard.nextLine();
		System.out.print("Firstname:\t");
		String name = keyboard.nextLine();
		System.out.print("Last Name:\t");
		String lastName = keyboard.nextLine();
		System.out.print("Age:\t\t");
		int age = keyboard.nextInt();
		
		if (gender.equalsIgnoreCase(Female) && age >= 20) {
			System.out.print("Are you married, " + name + "? (yes or no)");
			Married = keyboard.next();
			if (Married.equalsIgnoreCase("yes"))
				System.out.print("Then I shall call you Mrs. " + lastName + ".");
			}else{
				System.out.print("Then I shall call you " + name + " " + lastName + ".");
			
	
		
		if (gender.equalsIgnoreCase(Male) && age >= 20) 
			System.out.print("Then I shall call you Mr. " + lastName + ".");
		if (gender.equalsIgnoreCase(Male) && age < 20) 
			System.out.print("Then I shall call you " + name + " " + lastName + ".");
		
	
		}
	}
}



What I have tried:

Tried a bunch of fiddeling around with the curly bracets but couldnt get it to work

解决方案

This sniffs of homework so I'm going to blurt out an answer, but look at your curly braces and make sure every closing brace is matched up with an opening brace and the other way around.


import java.util.Scanner;

public class GenderAndMarigeStatus {
// i made some changes and now is okey
public static void main(String[] args) {

Scanner s = new Scanner(System.in);

String m = "Male";
String f = "Female";
String Married;

System.out.println("What is your gender (male or female):");
String gender = s.nextLine();

System.out.println("Firstname:");
String name = s.nextLine();
System.out.println("Last Name:");
String lastName = s.nextLine();
System.out.println("Age:");
int age = s.nextInt();

if (gender.equalsIgnoreCase("m") && age >= 20) {
System.out.println("Then I shall call you Mr. " + lastName + ".");
System.exit(0);
}
if (gender.equalsIgnoreCase("m") && age < 20) {
System.out.println("Then I shall call you " + name + " " + lastName + ".");
System.exit(0);
}
if (gender.equalsIgnoreCase(f) && age >= 20) {
}

System.out.println("Are you married, " + name + "? (yes or no)");
Married = s.next();

if (Married.equalsIgnoreCase("yes"))
System.out.println("Then I shall call you Mrs. " + lastName + ".");
else {
System.out.println("Then I shall call you " + name + " " + lastName + ".");

}
}
}


这篇关于[Java]在嵌套的IF和else语句中苦苦挣扎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆