本文介绍了[Java]在嵌套的IF和else语句中苦苦挣扎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好,晚上好, 我刚刚开始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 + 。); } } } 什么我试过了: 尝试了大量的花花公子,但是无法让它起作用解决方案 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 programmingimport java.util.Scanner;public class GenderGame {public static void main(String[] args) {//System ObjectsScanner keyboard = new Scanner(System.in);//VariablesString 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 解决方案 这篇关于[Java]在嵌套的IF和else语句中苦苦挣扎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-19 20:16